2011-10-04 84 views
0

我正在嘗試圍繞圓的圓周來轉換一個點。C#中的旋轉矩陣繪製出怪異的形狀

爲了測試我的代碼,我只需要翻譯該點併爲每個角度增量畫一條從中心到翻譯點的直線。

這應該看起來像一個自行車輪,但它看起來更像一個鏟子。奇怪的是,我是否在數學函數中使用了弧度或度數似乎是一樣的。

for (double degrees = 0; degrees <= 360; degrees += 1) 
{ 
    double radians = Math.PI * degrees/180.0; 
    Console.Write("degrees = " + degrees + " radians = " + radians); 
    double sx = 0.0; 
    double sy = -100.0; 
    sx = ((sx * Math.Cos(radians)) + (sy * Math.Sin(radians))); 
    sy = (-(sx * Math.Sin(radians)) + (sy * Math.Cos(radians))); 

    Console.WriteLine(", sx = " + sx + ", sy = " + sy); 

    g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, (int)sx+200, (int)sy+200); 
} 

回答

3

所以你數學是正確的。你使用這個矩陣來計算旋轉。

[cos(r) sin(r)] 
[-sin(r) cos(r)] 

但是你的編碼是錯的!

在使用它計算sy之前,您已將sx更改爲旋轉值!

您正在對x和y按順序執行計算,而在矩陣中,它們是同時完成的。所以,你的y座標被錯誤地計算,使用剪切座標系統,因爲它是...

你需要這樣做:

double sx = 0.0; 
double sy = -100.0; 
double nsx = ((sx * Math.Cos(radians)) + (sy * Math.Sin(radians))); 
double nsy = (-(sx * Math.Sin(radians)) + (sy * Math.Cos(radians))); 

g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, (int)nsx+200, (int)nsy+200); 
+0

gah爲什麼我沒有看到/ facepalm – amcashcow

0
double radius = 200; 

    for (double degrees = 0; degrees <= 360; degrees += 1) 
    { 
     double radians = Math.PI * degrees/180.0; 

     int sx = (int) (200 + radius * Math.Cos(radians)); 
     int sy = (int) (200 + radius * Math.Sin(radians)); 

     g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, sx, sy); 
    } 
+1

代碼的解釋改變了你這樣做將使這一個實際的答案。 –

0

我認爲這是一個純粹的數學問題。嘗試:

double radius = 100; 
double sx = radius * Math.Cos(radians); 
double sy = radius * Math.Sin(radians);