Sign in to follow this  

Drawing a 2d Circle Explanation

Recommended Posts

vertices = new VertexPositionColor[100];
for (int i = 0; i < 99; i++)
float angle = (float)(i / 100.0 * Math.PI * 2);
vertices[i].Position = new Vector3(200 + (float)Math.Cos(angle) * 100, 200 + (float)Math.Sin(angle) * 100, 0);
vertices[i].Color = Color.Black;
vertices[99] = vertices[0];

Can someone explain this code? I understand what it's doing as far as the for loop, but I'm trying to learn trig. Can someone explain it step by step? The output of this is a 2d circle. ;o

Share this post

Link to post
Share on other sites
The code is pretty straight forward. This is the most basic thing you can do with sine and cosine. The functions sin() and cos() take angles in radians; perhaps that's what was confusing you. Other than that, you should learn about trigonometry from a book, or you should have paid attention in high school.

The assignment vertices[99] = vertices[0]; seems like a bad idea. If you need the last point to be the same as the first, you need 101 points, or you need to divide the circle in 99 equal parts. As it is, there is a gap between 98 and 99.

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