2017-02-01 21 views
-1

我有一個三角形點[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看起來完全歪斜。

我完全誤解了如何做到這一點?

+1

對我來說很好(使用'theta = 0'),不得不聲明每個變量爲'double'並且使用'import static java.lang.Math。*;'* but * ...還有另一個**錯誤**:在'y1'的公式中使用'x1'已經改變的值 - 'x2'和'x3'相同 –

+0

**請發佈**正確的代碼,給出的結果是'theta = 1 '而不是'= 0'! –

+0

爲了便於閱讀,您可能會考慮將各個類(如矩陣和向量類)與旋轉函數一起使用。類似於「對於三角形中的所有向量v,旋轉(v,度)」和「旋轉(v,度):創建旋轉矩陣rot,返回v * rot」 – Aziuth

回答

2

你不能重用你的變量。

在轉換的第一行中更改x1的值後,在轉換的第二行中使用x1

看看公式/函數! x'是另一個變量,如x

嘗試使用第二組變量。更好地創建一個包含x和y的類。另一類包含三個點的三角形。然後創建一個三角形A和一個三角形B

1

我已經使用下面的程序進行測試:

double theta = 1; 
double x1 = 0; 
double y1 = 20; 
double x2 = 15; 
double y2 = -10; 
double x3 = -15; 
double y3 = -10; 

BufferedImage bim=new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB); 
Graphics2D g=(Graphics2D)bim.getGraphics(); 
Polygon pl=new Polygon();    
pl.addPoint((int)x1, (int)y1); pl.addPoint((int)x2, (int)y2); pl.addPoint((int)x3, (int)y3);     
pl.translate(100, 100);    
g.setColor(Color.red); 
g.fill(pl); 

double newx1 = (Math.cos(theta) * x1) - (Math.sin(theta) * y1); 
double newy1 = (Math.sin(theta) * x1) + (Math.cos(theta) * y1); 
double newx2 = (Math.cos(theta) * x2) - (Math.sin(theta) * y2); 
double newy2 = (Math.sin(theta) * x2) + (Math.cos(theta) * y2); 
double newx3 = (Math.cos(theta) * x3) - (Math.sin(theta) * y3); 
double newy3 = (Math.sin(theta) * x3) + (Math.cos(theta) * y3); 

Polygon pl2=new Polygon();    
pl2.addPoint((int)newx1, (int)newy1); pl2.addPoint((int)newx2, (int)newy2); pl2.addPoint((int)newx3, (int)newy3);    
pl2.translate(200, 200);     
g.setColor(Color.yellow); 
g.fill(pl2); 

.... display image bim here 

事實上當我轉換的角度它產生此

double theta = Math.toRadians(1); 
至然而弧度它產生此結果

enter image description here

enter image description here

,當我打開角度10度

double theta = Math.toRadians(10); 

enter image description here

-

你還需要確保你在兩個方程使用以前的座標,如上面更新。

最後你的多邊形需要居中,因爲它似乎是當前的多邊形;呃,你需要找到另一個問題的中心。

+0

仍然錯誤:使用'rotation''x1'來計算'y1 ' - 用更大的角度(例如60°(〜1弧度),70°甚至80°)嘗試它 - 對於像10° –

+0

這樣的較小角度而言,誤差並不明顯,但具有良好的可視化效果! –

+0

是的,謝謝它讓我注意到了新的x – gpasch