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;
}