# OpenGL circle problem

Page 1 of 1

## 2 Replies - 1937 Views - Last Post: 21 May 2010 - 03:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=174502&amp;s=4df00b6f580ab55c4ee185bd4c68752c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Kain6622

• D.I.C Regular

Reputation: 19
• Posts: 266
• Joined: 18-March 10

# OpenGL circle problem

Posted 20 May 2010 - 05:00 PM

Hi. I'm working on an exercise to create a polygon circle in openGL, the only problem is that I'm unsure how i go about doing a certain part of this function, the prototype is set as follows:
``` void Draw2DCircle( float x, float y, float z, float radius, int Segments, bool edgeOnly );
```

Where the user tells the function how many segments they want in the circle, the problem I'm unsure on how to solve is how i make sure each vertic is placed correctly based on the center origin of the circle as a switch statement is not possible due to not having a constant for the segments, same goes for if statements, I've got the displacement change that need to be applied to each vertex stored in variables, but i need to update each as i go around the circle... say i had a radius of 10 with 8 segments, the angle from the center would be 45 degrees, meaning that if my first vertex after the origin was straight above so coordinates ( -10, 0 ) respectivly x and y axis, the next vertex placement would be of displacement from the previous by roughly 7 units on the x-axis and roughly 3 on the y but the next vertex would be only 3 on the x but 7 on the y. any ideas how i would go about this without knowing the amount of segments the user wants? Code so far is as follows:
```void CGfxOpenGL::Draw2DCircle( float x, float y, float z, float radius, int Segments, bool edgeOnly )
{
/* Init temp variables */
double Change_angle = 360 / Segments;

/* displacement variables */
double Tx, Ty;
float Nx, Ny;
Nx = (float)Tx;

glBegin( GL_TRIANGLE_FAN );
glVertex3f( x, y, z );          // Plot center point

for( int segs = 0; segs < Segments; segs++ )
{
/* plot vertices based on current segment */
}

glEnd();

}

```

Is This A Good Question/Topic? 0

## Replies To: OpenGL circle problem

### #2 Kain6622

• D.I.C Regular

Reputation: 19
• Posts: 266
• Joined: 18-March 10

## Re: OpenGL circle problem

Posted 21 May 2010 - 03:31 AM

anyone got any hints of how i should do this? I'm starting to think that with knowing the circle is semetrical to plot the vertices for 1 quarter of the circle and repeat the process for each section?

### #3 Kain6622

• D.I.C Regular

Reputation: 19
• Posts: 266
• Joined: 18-March 10

## Re: OpenGL circle problem

Posted 21 May 2010 - 03:53 AM

I've sort of sorted it but there is just on question that I'm unsure of, when it draws the circle it doesn't fill the gap between the last vertex and the first, how does one go about getting with to close the circle to make it whole?

updated code is as follows:
```void CGfxOpenGL::Draw2DCircle( float x, float y, float z, float radius, int Segments, bool edgeOnly )
{
/* Init temp variables */
float Change_angle = 360 / (float)Segments;

if( edgeOnly )
{}

/* displacement variables */
double Tx, Ty;
float Nx, Ny;
Nx = (float)Tx;

glBegin( GL_TRIANGLE_FAN );
glVertex3f( x, y, z );		// Plot center point

for( int segs = 0; segs < Segments; segs++ )
{
}
glEnable( GL_LINE_LOOP );

glEnd();

}

```

This post has been edited by Kain6622: 21 May 2010 - 05:32 AM