The points on the circle around the corner are not evenly distributed

Given:

x = start X + radius * Cos (angle);

y = start Y + radius * Sin (angle);

Why won't the points be evenly spaced around the edge of the circle?

Result image:

enter image description here

class Circle
{
    public Vector2 Origin { get; set; }
    public float Radius { get; set; }
    public List<Vector2> Points { get; set; }

    private Texture2D _texture;

    public Circle(float radius, Vector2 origin, ContentManager content)
    {
        Radius = radius;
        Origin = origin;
        _texture = content.Load<Texture2D>("pixel");
        Points = new List<Vector2>();

        //i = angle
        for (int i = 0; i < 360; i++)
        {
            float x = origin.X + radius * (float)Math.Cos(i);
            float y = origin.Y + radius * (float)Math.Sin(i);
            Points.Add(new Vector2(x, y));
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < Points.Count; i++)
            spriteBatch.Draw(_texture, Points[i], new Rectangle(0, 0, _texture.Width, _texture.Height), Color.Red);
    }
}

      

+3


source to share


2 answers


Math.Cos and Math.Sin take angle in radians as opposed to degrees, your code should be:



float x = origin.X + radius * (float)Math.Cos(i*Math.PI/180.0);
float y = origin.Y + radius * (float)Math.Sin(i*Math.PI/180.0);

      

+6


source


Points:

1) The Math.Trig functions use radians, not degrees.



2) For that kind of precision, you'd be better off using double

instead float

.

3) Professional computer graphics / games avoid costly functions such as Sin and Cos, and instead use incremental orientation integer The , how Breshenema algorithms that produce results as good or better than the direct trigonometric mathematical calculations and much faster.

+1


source







All Articles