2013-02-19 104 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