Simple cycle

Started by
7 comments, last by kelvindavies11 17 years, 10 months ago
IM using the fllowing code to create a simple circle, which i intend to turn into a sphere. I knwo i could useing gluSphere however i fancy trying to do it this way. When i use the following code it gives me a syntax error on the '=', any idea why?? thanks, kelvin void cyclinder(void) { glBegin(GL_LINE_LOOP); for(int i=0;i<360;i++) { float xcoord = 1 * cos(i*2*PI/360); float ycoord = 1 * sin(i*2*PI/360); glVertex2f(xcoord,ycoord); } glEnd(); }
Advertisement
i have amended the above code so it is simpler however im still getting the syntax error on the = sign. Any ideas?

void cyclinder(void)
{

glBegin(GL_LINE_LOOP);
for(int i=0;i<360;i++)
{
angle =2*PI*i/360;
glVertex3f(cos(angle),sin(angle), 1);
}

glEnd();

}

kind regards,
kelvin
I don't know much about C/C++ but isn't it supposed to be i==0?
Which '=' is the error on? What is the exact error? Is PI defined? Is angle declared?
First of all it is a bad idea to define "float xcoord" and "float ycoord" in your for loop because the OS needs to alloc memory for those variables everytime again. This causes a strain on performance. Declare them before your for loop or make them static.

I am using vc++ and it gives no errors.

But try typecasting.

xcoord = (float) 1 * cos(i*2*PI/360);


hope it works.
^_^
Someone who uses a, euhm..., delta!?
Quote:Original post by AndyEsser
I don't know much about C/C++ but isn't it supposed to be i==0?


No, "i = 0" is correct.

Quote:
First of all it is a bad idea to define "float xcoord" and "float ycoord" in your for loop because the OS needs to alloc memory for those variables everytime again. This causes a strain on performance.


Any modern compiler will move the space "allocation" outside the loop for you. But, more importantly, since the code does not even compile for the OP, this is hardly is biggest concern right now.
the aove does compile now, however when im using the above code i draw a circle however it only draws a circle containing 8 sides not the 360 that i require for a smooth circular effect.

any ideas please,
kelvin
What is happening is i/360 is interger division. Change the line

angle =2*PI*i/360;

to

angle =2.0*PI*(float)i/360.0;

Also make sure that the variable angle is a float.

I do not have a CPP compiler in front of me but the floating point division instead of integer division will at least push you in the right direction.
thanks i changed it to float but still no joy

kelvin

This topic is closed to new replies.

Advertisement