# circle algorithm

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

## Recommended Posts

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++)
{
deg += .5f;
m_ColoredVertices.Position = new Vector3(
0f);
m_ColoredVertices.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 on other sites
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 on other sites
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 on other sites
Thanks alot for the help.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633662
• Total Posts
3013229
×