PDA

View Full Version : I want to make a circle



07-13-2002, 05:45 AM
i need to aproximate a circle with Ogl. and i need to draw it in any place of the screen without using the translatef command.

Miguel_dup1
07-13-2002, 06:31 AM
Well, you could use a for loop to generate your coordinates with sin and cos... Then you can simply provide the way you want to draw with glBegin( GL_LINE_STRIP ); or somtehing...

So it could be like this:

glBegin( GL_LINE_STRIP );

for( float angle = 0.0f; angle <= (2.0f*PI); angle += 0.1f )
{
x = cos(angle);
y = sin(angle);

glVertex3f( x, y , -1 ); // I dont know you coordinate system, so this -1 maybe wrong.
}

glEnd();


This should give and idea... For a better approximation, you simply increment the angle by a smaller unit...
I have not tested this 'cause i just worte it.
I hope this helps. http://www.opengl.org/discussion_boards/ubb/smile.gif

t0y
07-14-2002, 05:26 AM
Originally posted by mancha:
for( float angle = 0.0f; angle <= (2.0f*PI); angle += 0.1f )


It's better to use something like:

angle+=2.f*PI/Segments

This way all circle segments will be equal.

nexusone
07-16-2002, 05:31 AM
Also you need to have an offset varible, if you want to draw anywhere without glTranslate. I think you maybe having a problem a lot of people have using glTranslate and not also using glPop/push matrix with it, making things not translate the way you want.


Here are a few things missing from mancha's example:

ox, oy, oz = location of center of circle anywhere on screen.
r = radius of circle.

x = r * cos(angle);
y = r * sin(angle);

glVertex3f( ox - x, oy - y , oz ); // you may need to change ether ox or oy to plus instead of minus.


Originally posted by mancha:
Well, you could use a for loop to generate your coordinates with sin and cos... Then you can simply provide the way you want to draw with glBegin( GL_LINE_STRIP ); or somtehing...

So it could be like this:

glBegin( GL_LINE_STRIP );

for( float angle = 0.0f; angle <= (2.0f*PI); angle += 0.1f )
{
x = cos(angle);
y = sin(angle);

glVertex3f( x, y , -1 ); // I dont know you coordinate system, so this -1 maybe wrong.
}

glEnd();


This should give and idea... For a better approximation, you simply increment the angle by a smaller unit...
I have not tested this 'cause i just worte it.
I hope this helps. http://www.opengl.org/discussion_boards/ubb/smile.gif