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

Started by
6 comments, last by skow 18 years, 10 months ago
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?
Advertisement
Bresenham's Line and Circle Algorithms
Dolphins - The sharks of the sea.
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..
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 );

}
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.
¨@_
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.
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)..
Killers don't end up in jailThey end up on a high-score!
Thanks guys, I'm going with a lookup table.

This topic is closed to new replies.

Advertisement