我有一個三角形點[0,20],[15,-10],[-15,-10]。現在我想由1度,2度等旋轉二維三角形產品的奇數結果
林在another SO question提供的公式如下旋轉此三角形:
x' = cos(theta)*x - sin(theta)*y
y' = sin(theta)*x + cos(theta)*y
我的Java代碼是:
theta = 0;
x1 = 0;
y1 = 20;
x2 = 15;
y2 = -10;
x3 = -15;
y3 = -10;
System.out.println("--------------------adjusted to 0, 0 --------------------------------------------");
System.out.println("[" + x1 + ", "+ y1 + "] " + "[" + x2 + ", " + y2 + "] [" + x3 + ", " + y3 + "]");
x1 = (cos(theta) * x1) - (sin(theta) * y1);
y1 = (sin(theta) * x1) + (cos(theta) * y1);
x2 = (cos(theta) * x2) - (sin(theta) * y2);
y2 = (sin(theta) * x2) + (cos(theta) * y2);
x3 = (cos(theta) * x3) - (sin(theta) * y3);
y3 = (sin(theta) * x3) + (cos(theta) * y3);
System.out.println("-------------------- rotated --------------------------------------------");
System.out.println("[" + x1 + ", "+ y1 + "] " + "[" + x2 + ", " + y2 + "] [" + x3 + ", " + y3 + "]");
輸出到這個產生莫名其妙的結果:
--------------------adjusted to 0, 0 --------------------------------------------
[0.0, 20.0] [15.0, -10.0] [-15.0, -10.0]
-------------------- rotated --------------------------------------------
[-16.829418, -3.355421] [16.519243, 8.49744] [0.31017494, -5.1420197]
當我繪製這個,tr iangle看起來完全歪斜。
我完全誤解了如何做到這一點?
對我來說很好(使用'theta = 0'),不得不聲明每個變量爲'double'並且使用'import static java.lang.Math。*;'* but * ...還有另一個**錯誤**:在'y1'的公式中使用'x1'已經改變的值 - 'x2'和'x3'相同 –
**請發佈**正確的代碼,給出的結果是'theta = 1 '而不是'= 0'! –
爲了便於閱讀,您可能會考慮將各個類(如矩陣和向量類)與旋轉函數一起使用。類似於「對於三角形中的所有向量v,旋轉(v,度)」和「旋轉(v,度):創建旋轉矩陣rot,返回v * rot」 – Aziuth