Sign in to follow this  
skow

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

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
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[i].x = cos(i);
verts[i].y = sin(i);
}


Then Draw them.

DrawCircle(float size, int numVertsToDraw)
{
step = 360/numVertsToDraw;
for ( int i = 0;
i < numVertsToDraw;
i+= step )
DrawLine( verts[i] * 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

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