圆角按不均匀间隔的角度

时间:2013-02-19 14:36:13

标签: c# xna geometry angle

假设:

x = originX + radius * Cos(角度);

y = originY + radius * Sin(角度);

为什么这些点不会均匀分布在圆的边缘?

结果图片:

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

2 个答案:

答案 0 :(得分:6)

Math.Cos和Math.Sin以弧度而不是度数取角度,你的代码应为:

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

答案 1 :(得分:1)

点数:

1)Math.Trig函数使用Radians,而非Degrees。

2)对于这种精确度,最好使用double代替float

3)计算机图形/游戏专业人员避免昂贵的功能,如Sin和Cos,而是使用增量整数像素导向的方法,like Bresenham's Algorithms,使结果好于或优于直接的三角数学计算,并且很多更快。