# redraw loop drawing multiple objects

Hello! I am so confused…I have a very simple loop that I would really like to be a while loop…however when I do use while, I get nothing…so I started working with a for loop and I do get a picture! But I am not getting the behavior I want.

Ideally I would like:

while (xPos != 0.0){
draw a cube at xPos, yPos
decrement x
decrement y as a function of x
}

//i am starting out the cube at (.75, .75)

and here is my code that will draw a cube–but something really strange is happening, as the cube gets closer to the origin, more and more cubes start appearing then they all kind of meet in the middle and then escape at opposite diagonal ends of the screen?

I have no idea what’s going on ``````	int i = 0;

//while (xPos != 0.0) {

for (i; i < 200; i++) {
glTranslated(xPos, yPos, 0.0);
glutWireCube(.25);
xPos-=0.00005;	//move in a negative direction along the xaxis
yPos = (startYPos*xPos/startXPos);
glutSwapBuffers();
glutPostRedisplay();
}
``````

When using real numbers never do exact tests like xPos != 0.0 because precision might result in a number like 0.00000001 or -0.00000001 instead of 0.0. It is good programming practice do use inequality tests rather than exact tests when the results would have the same meaning.
So try

``````
while ( xPos > 0)

``````

If you need to do tests against an exact number like 0.0 with reals do something like

``````
while  (! absf(xPos  < 0.00001))

``````

Are you intending them to be spaced evenly along the x-axis every 0.00005 units, because that’s not what will happen since you are translating each time with glTranslate + adjusting the amount you are translating by too.

The first few values of x that the cube will be drawn at will be:
0.75
0.75 + (0.75-0.00005) =
0.75 + (0.75-0.00005) + (0.75-20.00005)
0.75 + (0.75-0.00005) + (0.75-2
0.00005) + (0.75-3*0.00005)

x won’t start to decrease till 15000 iterations of the loop, when the amount being added each time will finally become negative (since 15000*0.00005 = 0.75).

You either want to translate by a fixed amount:

``````glTranslate(-0.00005, delta_y, 0)
``````

but this would require calculating the change in y at each step, or make sure the modelview matrix is reset each time instead of each translation being added, with glLoadIdentity() or glPushMatrix()/glPopMatrix() :

``````
glPushMatrix();
glTranslated(xPos, yPos, 0.0);
... draw ...
glPopMatrix();
``````