Jump to content
  • Advertisement
Sign in to follow this  
skow

Drawing a circle w/o sin, cos, and sqrt?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to make a circle via a line strip, 2 ways I can think of doing it are a little slow for my liking. Method 1)
for(int i=0;i<NUM_LINES;i++)
{
	xcoord = RADIUS * cos(i*2*PI/NUM_LINES);
	ycoord = RADIUS * sin(i*2*PI/NUM_LINES);	
}
Method 2) for(float i = -RADIUS; i < RADIUS; i +=RADIUS/NUM_LINES) { ycoord = i; xcoord1 = sqrt(RADIUS^2 - i^2); xcoord2 = -xcoord1; } Where both xcoord1 and xcoord2 share the same ycoord. I have a feeling I’m forgetting/overlooking something, know of a faster way?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by skow
Method 1)

for(int i=0;i<NUM_LINES;i++)
{
xcoord = RADIUS * cos(i*2*PI/NUM_LINES);
ycoord = RADIUS * sin(i*2*PI/NUM_LINES);
}

You only have to calculate a quarter of the circle if you use the right number of lines: cos(0) is the same as -cos(2 * PI) and so on..

Share this post


Link to post
Share on other sites
just make the vertices once

Vector verts[360];

CreateCircle()
{
for ( i = 0; i < 360; i++ )
verts.x = cos(i);
verts.y = sin(i);
}


Then Draw them.

DrawCircle(float size, int numVertsToDraw)
{
step = 360/numVertsToDraw;
for ( int i = 0;
i < numVertsToDraw;
i+= step )
DrawLine( verts * size, vert[i+1] * size );

}

Share this post


Link to post
Share on other sites
Quote:
Original post by WanMaster
Quote:
Original post by skow
Method 1)

for(int i=0;i<NUM_LINES;i++)
{
xcoord = RADIUS * cos(i*2*PI/NUM_LINES);
ycoord = RADIUS * sin(i*2*PI/NUM_LINES);
}

You only have to calculate a quarter of the circle if you use the right number of lines: cos(0) is the same as -cos(2 * PI) and so on..


I believe you can get away with an eighth, too.

Share this post


Link to post
Share on other sites
Quote:
Original post by Snaily
Quote:
Original post by WanMaster
Quote:
Original post by skow
Method 1)

for(int i=0;i<NUM_LINES;i++)
{
xcoord = RADIUS * cos(i*2*PI/NUM_LINES);
ycoord = RADIUS * sin(i*2*PI/NUM_LINES);
}

You only have to calculate a quarter of the circle if you use the right number of lines: cos(0) is the same as -cos(2 * PI) and so on..


I believe you can get away with an eighth, too.

Yes, you are correct. Didn't think about that.

Share this post


Link to post
Share on other sites
Use a look-up table for sin/cos (can be way less than 360 if you know the max amount of lines - I use 20 for a 20px (diameter) circle and that looks fine)..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!