2013-02-19 106 views
3

鉴于:角度上的点不均匀间隔

x =原点X +半径* Cos(角度);

y =原始Y +半径* 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); 
    } 
} 
+3

看到这个问题。 OP有同样的问题,你有。 http://stackoverflow.com/questions/14598954/finding-points-on-perimeter-of-a-circle/14599469#14599469 – 2013-02-19 14:41:28

回答

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函数使用Radians而不是Degre ES。

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

3)计算机图形/游戏利弊避免昂贵的功能,如正弦和余弦,而是使用增量 整数 像素为导向的方法,like Bresenham's Algorithms,那得出的结果一样好,甚至比直接的三角数学计算得更好, 更快。

+0

我还没有看到基于整数的方法对* long *时间的偏好;)现代(而不是现代)的GPU几乎要求你使用'float'。 – 2013-02-19 15:01:46

+0

@AndrewRussell对。显然,我不是我提到的那些赞成者之一。 :-)它的其余部分仍然正确吗? – RBarryYoung 2013-02-19 15:16:21

+0

#1显然是正确的。 #2很有趣 - 正如你通常使用'float'来减少数据大小以获得更好的内存性能(并且GPU几乎完全使用它)。你很少需要'double'的精确度(但是你必须能够知道你什么时候需要它)。但是C#只是提供'double'版本的trig函数。 #3我不确定是否有效的建议。你很少想要绘制一个别名的圆形轮廓(当你可以在GPU上做一个好的反锯齿粗线时),并且在现代GPU上像素点击操作相对非常缓慢! – 2013-02-19 15:29:23