# circle algorithm

## Recommended Posts

cptrnet    169
Hello, i'm trying to make a circle algorithm but Im having trouble heres what I got so far.
m_ColoredVertices = new CustomVertex.PositionColored[m_NumVertices];
m_VertexFormat = CustomVertex.PositionColored.Format;
m_VertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
m_NumVertices, D3DDevice, Usage.Dynamic | Usage.WriteOnly,
m_VertexFormat, Pool.Default);

double tempx = m_XPos;
double tempy = m_YPos;

float deg = 0;

Random r = new Random(100);

for(int i = 0; i < m_NumVertices; i++)
{
tempx = m_Radius * Math.Cos(deg);
tempy = m_Radius * Math.Sin(deg);
deg += .5f;
m_ColoredVertices[i].Position = new Vector3(
(float)tempx + (m_XPos + m_Radius),
(float)tempy + (m_YPos + m_Radius),
0f);
m_ColoredVertices[i].Color = Color.FromArgb(r.Next(255),r.Next(255),r.Next(255)).ToArgb();
}

m_VertexBuffer.SetData(m_ColoredVertices, 0, LockFlags.None);


I pass in how many subdivisions I want and thats the m_NumVertices I render it with a TriangleFan, if I set the subdivisions up really high it looks fine. But when I turn it down its messed up and doesnt finish drawing the circle completley. I am a real newbie at this so any help with this would be appreiciated, thanks

#### Share this post

##### Share on other sites
jollyjeffers    1570
Hi,

Your code for incrementing deg might well be your problem for it not completing the full circle.

Your deg += .5f line obviously increments it 0.5 per iteration, the number of iterations being the number of subdivisions you're after.

A complete circle is 2*pi radians round, or roughly 6.28; thus you'd need 13 iterations for a complete circle (12.56 to be picky [wink]). Any more than 13 iterations and you'll start duplicating your circle, any less and you'll get an incomplete circle.

To get a solution, I haven't tested it, but constructing your loop something more like:
for( int i = 0; i < m_NumVertices; i++ )    {        float angle = ((float)i / (float)m_NumVertices) * 2 * 3.14159f;        float xPosition = m_Xpos + (m_Radius * Math.Cos( angle ));        float yPosition = m_YPos + (m_Radius * Math.Sin( angle ));        //Construct your vertex here using xPosition and yPosition    }

Whatever value m_NumVertices takes should now generate a perfect circle.

hth
Jack

#### Share this post

##### Share on other sites
Guest Anonymous Poster
The Sin and Cos functions use radians not degrees. You are counting in steps of half a radian each time (deg += .5f) regardless of the number of vertices you are using. You need to go from 0 to 360 degrees in (m_iNumVertices) steps, and 360 degrees is 2*Pi radians, so if you add (2 * Math.Pi / m_iNumVertices) each time then it should work fine.

#### Share this post

##### Share on other sites
cptrnet    169
Thanks alot for the help.

## 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