Circle Thread 2

Started by
17 comments, last by Tipotas688 14 years, 1 month ago
My problem wasn't resolved in my previous post so I m trying again:

public void drawCircle(SpriteBatch spriteBatch, Texture2D blank, Vector2 position, float radius)
{
            float angle = 0;
            for (int i = 1; i <= 360; i++)
            {
                position.X = position.X + (float)Math.Cos(angle);
                position.Y = position.Y + (float)Math.Sin(angle);
                spriteBatch.Draw(blank, position, null, Color.White, angle, new Vector2(blank.Width / 2f, blank.Height / 2f), 1f, SpriteEffects.None, 1f);  
                angle = angle +MathHelper.ToRadians(1f);
            } 
}


I want to draw a circle around a triangle(its circumcircle) my code above draws a wrongly sized circle even if I do position.X = position.X + radius* (float)Math.Cos(angle); First of all is my approach of drawing a circle correct? Is there a better way than drawing 360 points? Secondly to draw the circle around the triangle I have already calculated the radius and the positions of the three points of the triangle if that helps Thanks!
Advertisement
The problem was resolved in your previous post, at least the problem that's evident in the code you have posted now. You can not update the same variable like that, as it will use the previous point as the center for the next point. Declare a new Vector2 position, which you set to position = center + radius * cos/sin each iteration. You pass center and radius to the function, and never change them. Only change the new local variable position which you use to draw at.
ok thanks let me try it :P
If you're trying to draw a circle point by point you should read this article: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm

As far as drawing a circle around the triangle I could calculate the center point of the triangle and then determine the radius based on the 3 points which define the triangle. Draw your circle with that center point and your calculated radius.
[source lang=c#]public void drawCircle(SpriteBatch spriteBatch, Texture2D blank, Vector2 position, Vector2 center, float radius)        {            float angle = 0;            for (int i = 1; i <= 360; i++)            {                position.X = center.X + radius  * (float)Math.Cos(angle);                position.Y = center.Y + radius  * (float)Math.Sin(angle);                spriteBatch.Draw(blank, position, null, Color.White, angle, new Vector2(blank.Width / 2f, blank.Height / 2f), 1f, SpriteEffects.None, 1f);                angle = angle + MathHelper.ToRadians(1f);            }         }


the radius is making the circle too big in this algorithm

@CornyKorn21 thats what I ve been trying to do, I dont know how put it in the algorithm so that the radius will make the circle appear correctly.

PS: I know that all my numbers are correct because I have a whole delaunay algorithm up and working, I just want to draw a circle :P
You need to understand scope. You need a temporary Vector2 inside the scope of the for loop:

1) remove the position argument from your function

2)
for ( blah blah ){    Vector2 position;    position.X = center.X + blah blah;    position.Y = center.X + blah blah;    blah blah}


I don't know why you're passing both position and center into your function....

-me
Also as for your triangle problem. The 3 points of the triangle are not guaranteed to fall on the circumference of any circle. The best you can hope for is a circle that encloses all the points with the furthest point lying on the circumference.

To find the necessary things:

Average the points of the triangle together, this will get you the center.
Do a distance calculation between each point and this center
The furthest distance is the radius of your circle
The averaged center is the center of your circle

-me
ok it makes more sense now I made a temporary vector2 but still the radius make the circle huge
Remember also that radius and all your points are not in pixels. it's units of space. The settings for your viewport, projection matrix and model view matrix will determine how big a unit is on screen.

So describe what you mean exactly by "too big"
To give some visual output of whats the current results:


Photobucket

Photobucket

as you can see from these two pictures delaunay works correctly therefore my data is correct. There must be something wrong with the circle drawing code...

Also if I go about this way the angle the circle is drawn is wrong like you can see in picture two...

[Edited by - Tipotas688 on March 6, 2010 5:28:05 AM]

This topic is closed to new replies.

Advertisement