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

## Recommended Posts

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);
}
}


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!

##### Share on other sites
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.

##### Share on other sites
ok thanks let me try it :P

##### Share on other sites

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.

##### Share on other sites
[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

##### Share on other sites
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

##### Share on other sites
ok it makes more sense now I made a temporary vector2 but still the radius make the circle huge

##### Share on other sites
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 averaged center is the center of your circle

-me

##### Share on other sites
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"

##### Share on other sites
To give some visual output of whats the current results:

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]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

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

• Total Topics
633679
• Total Posts
3013294
×