Thread: Simple lines_to_svg file not producing correct output

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    2

    Simple lines_to_svg file not producing correct output

    The program below is a simple assignment to produce shrinking lines. I do not understand the issue that I am having. I am getting the initial line of (10, 0, 10, 400). And struct line translate is moving the x values by the desired amount (10, 20,... 390). But the struct line scale isn't working, and I don't know why. As it stands, when I compile it, it returns y values of 0. If I change the formula in struct scale to a simple addition it works. I have marked the area that is causing the problem in red. Any help would be appreciated....

    To clarify, the object is to make a line move to the right by 10, and scale by 90%, with each successive line centred on the horizontal midpoint.



    Code:
    #include<stdio.h>
    
    
    
    
    struct point 
        {
        float x,y;
        };
    
    
    struct line 
        {
        struct point p0,p1;
        };
    
    
    
    
    struct line translate(struct line line, float delta_x)
        {
        struct line line0;
            line0.p0.x = line.p0.x + delta_x;
            line0.p1.x = line.p1.x + delta_x;
                    
        return line0;    
    
    
        }
    
    
    struct line scale(struct line line, float factor)
        {
        struct line line0;
            line0.p0.y = line0.p0.y + (line0.p1.y * ((1 - factor)/2));
            line0.p1.y = line0.p1.y - (line0.p1.y * ((1 - factor)/2));
            
        return line0;    
    
    
        }
    
    
    int main()
        {
        float origin, canvas, delta_x, factor;
    
    
        origin = 0.0;
        canvas = 400.0;
        delta_x = 10.0;
        factor = 0.90;
    
    
        printf("%.1f %.1f\n", canvas, canvas);
    
    
        struct line line;
    
    
        line.p0.x = 10.0;
        line.p0.y = origin;
        line.p1.x = 10.0;
        line.p1.y = canvas;
    
    
        printf("%f %f %f %f\n", line.p0.x, line.p0.y, line.p1.x, line.p1.y);
    
    
        struct line line0;
    
    
        line0.p0.x = line.p0.x;
        line0.p0.y = line.p0.y;
        line0.p1.x = line.p1.x;
        line0.p1.y = line.p1.y;
    
    
        while (line0.p0.x < canvas - line.p0.x)
            {    
            line0 = translate(line0, delta_x); 
            line0 = scale(line0, 0.90);
    
    
            printf("%f %f %f %f\n", 
            line0.p0.x, line0.p0.y, line0.p1.x, line0.p1.y);
            }
    
    
        return 0;
    }
    Last edited by canuxforryan; 01-29-2014 at 08:21 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Shouldn't you be using p0 on the first one instead of p1? EDIT: Oh, are you trying to move each the same amount. But: You're using the completely uninitialized line0, instead of the line that you passed into the function.
    Last edited by tabstop; 01-29-2014 at 08:18 PM.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    2
    The program works to move each line to the right by 10, but the scaling isn't happening.... the outputs look like this:

    400.0 400.0
    10.0 0.0 10.0 400.0
    20.0 0.0 20.0 0.0
    30.0 0.0 30.0 0.0
    40.0 0.0 40.0 0.0
    -
    -
    -
    390.0 0.0 390.0 0.0

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    struct line translate(struct line line, float delta_x)
        {
        struct line line0;
            line0.p0.x = line.p0.x + delta_x;
            line0.p1.x = line.p1.x + delta_x;
                    
        return line0;    
    
    
        }
    Here's your question.
    What is the value of line0.p0.y (and line0.p1.y) when this function returns?

    Is it by any chance line.p0.y ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2013, 09:32 AM
  2. Program Not Producing Output
    By lebronlin in forum C Programming
    Replies: 2
    Last Post: 04-28-2011, 12:06 PM
  3. Producing a Table with Data from File
    By Vulpecula in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2008, 09:10 PM
  4. output to file (extremely simple but...)
    By kantze in forum C++ Programming
    Replies: 6
    Last Post: 04-12-2007, 03:24 PM
  5. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 02:57 PM