Sign in to follow this  
leahayes00

Generating a Circle Mesh

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 this post


Link to post
Share on other sites
Quote:
Original post by mike25025
for 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 this post


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


N = number of points
TWOPI = 6.28319;

// reserve memory to avoid lots of memory reallocs
CirclePoints.reserve(N);

// make sure vector is empty. Do NOT do a clear()!
CirclePoints.resize(0);

POINT center; // centerpoint
POINT 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this