在圆上以非均匀角度划分的点

3

给定:

x = originX + radius * Cos(angle);

y = originY + radius * Sin(angle);

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

结果的图像:

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
看这个问题,提问者有和你一样的问题。http://stackoverflow.com/questions/14598954/finding-points-on-perimeter-of-a-circle/14599469#14599469 - Nico Schertler
2个回答

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)Math.Trig函数使用弧度,而不是角度。

2)对于这种精度,最好使用double而不是float

3)计算机图形/游戏专业人员避免使用昂贵的函数,如Sin和Cos,而是使用增量整数像素导向方法,例如Bresenham算法, 这些方法给出的结果与直接三角函数数学计算一样好甚至更好,并且速度更快


我已经很久没有看到过偏好整数为基础的方法了 ;) 现代(和不那么现代)的GPU几乎要求您使用float - Andrew Russell
@AndrewRussell 没错,显然我不是我提到的那些专家之一。 :-) 其他部分仍然正确吗? - RBarryYoung
#1 显然是正确的。#2 很有趣 - 因为通常使用float来减少数据大小以获得更好的内存性能(而GPU几乎完全使用它)。你很少需要 double 的精度(但那时你必须能够知道何时需要它)。但是,C# 仅提供三角函数的double版本。 #3 我不确定这是否仍然是有效的建议。当您可以在GPU上做一个漂亮的反锯齿厚线圆形轮廓时,很少想绘制一个带锯齿的圆形轮廓,并且在现代GPU上,像素插入操作相对非常慢! - Andrew Russell
@AndrewRussell 没错。 - RBarryYoung
在这个示例用例中,甚至想到优化都是不可思议的。使用sin和cos。 - Fattie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接