Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple cycle

Started by kelvindavies11 May 31, 2006 at 6:40 AM 7 replies 900+ views
Original Post
kelvindavies11
kelvindavies11
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(); }
kelvindavies11
kelvindavies11
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
AndyEsser
AndyEsser
I don't know much about C/C++ but isn't it supposed to be i==0?
Kalidor
Kalidor
Which '=' is the error on? What is the exact error? Is PI defined? Is angle declared?
delta user
delta user
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!?
bakery2k1
bakery2k1
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.
kelvindavies11
kelvindavies11
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
bin11001010
bin11001010
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.
kelvindavies11
kelvindavies11
thanks i changed it to float but still no joy

kelvin

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.