# Generating a Circle Mesh

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have a basic data structure like this: struct POINT { float x, y; }; and an array like this: vector<POINT> CirclePoints; How can I generate a circle, i.e. calculate the vertex positions, when the circle has n vertices or s sides? When the circle has three sides it should be like this: (Ignore the _ symbols) ___. __._. Four sides: ___. __._. ___. Five sides: ____. __.___. ___. . Six sides: _____. . ___._____. _____. . etc... Any help would be greatly appreciated! Best Regards, Lea Hayes

##### Share on other sites
for num of sides n do add point i (x = sin(i*360/n),y = cos(i*360/n))

hope that helps

##### Share on other sites
Quote:
 Original post by mike25025for num of sides n do add point (x = sin(360/n),y = cos(360/n))hope that helps

for (i = 0; i < n; i++)
{
xi = sin(i*360/n),yi = cos(i*360/n)
}

##### Share on other sites
Actually, do this instead. I'm assuming vector is std::vector.

N = number of pointsTWOPI = 6.28319;// reserve memory to avoid lots of memory reallocsCirclePoints.reserve(N);// make sure vector is empty. Do NOT do a clear()!CirclePoints.resize(0);POINT center; // centerpointPOINT newpoint;for (i = 0; i < N; i++){  newpoint.x = center.x + radius * cosf(i * TWOPI / N);  newpoint.y = center.y + radius * sinf(i * TWOPI / N);  CirclePoints.push_back(POINT}

Two things. First, I'm using TWOPI instead of 360 since most compilers require an angle in radians when you call cos/sin. 360 is degrees, TWOPI is radians.

Second, that loop will generate a circle that is not closed. If you want enough points to close the circle (e.g., if you draw a line between points 0 and 1, 1 and 2, ..., N-1 and N, you end up with a closed circle, then change the N in the denominator of the newpoint.x/y equations to be (N-1) instead.