2013-12-12 94 views
0

目標:使用旋轉矩陣產生旋轉;Java // Array //乘法// For循環;

private double[][] rotateX = {{1.00,0.00,0.00},{0.00,Math.cos(theta),Math.sin(-theta)},{0.00,Math.sin(theta),Math.cos(theta)}}; // Rotation matrice for x axis; 

問題定義:輸出不匹配預期;

預期輸出:

newX = 3 
newY = -2.236 
newZ = -0.002 
// Checked in MathCAD; 

輸出產生:

newX = 3.00 
newY =-2.00 
newZ = 1.0000000000000002 

代碼施加:

public class Test2 { 
private double[] target = {3.00,1.00,2.00};  // target data for matrix multiplication; 
private double[] product = {0.00,0.00,0.00};  // product of the for-loop; 

private double theta = Math.toRadians(90.00);  // Rotation angle; 

private double[][] rotateX = {{1.00,0.00,0.00}, 
     {0.00,Math.cos(theta),Math.sin(-theta)}, 
     {0.00,Math.sin(theta),Math.cos(theta)}}; // Rotation matrice for x axis; 

private void rotateAroundX(){ 
    double temp;         // temp. data storage; 

    double newX = 0.00;       // new x after for-loop; 
    double newY = 0.00;       // new y after for-loop; 
    double newZ = 0.00;       // new z after for-loop; 

    for(int i = 0; i < rotateX.length ; i ++){   // check how many items are stored in the matrix; 
     for(int j = 0; j < rotateX[i].length; j++){  // check how many elements are stored in each item; 

      temp = rotateX[i][j] * target[j];   // store product of the multiplication in temp; 

      if(i == 0){ 
       newX += temp;       // if i index is 0 finalize matrix multiplication and set newX; Ex. 1 row of rotateX - (1.00*3.00 + 0.00*1.00 + 0.00*2.00); 
      } 
      if(i == 1){         // if i index is 1 finalize matrix multiplication and set newY; 
       newY += temp; 
      } 
      if(i == 2){         // if i index is 2 finalize matrix multiplication and set newZ; 
       newZ += temp; 
      } 
     } 
    } 

    this.product[0] = newX;        // Add newX to product; 
    this.product[1] = newY;        // Add newY to product; 
    this.product[2] = newZ;        // Add newZ to product;     

} 

public void sart(){ 
    rotateAroundX();     // run test-case; 

    for (double e : product){ 
     System.out.println(e);   // output the product information; 
    } 
} 
} 
+0

當您通過調試器完成此操作時,發現了什麼? –

+0

@OliCharlesworth - 沒有預期的過程流程,但是輸出與我期望的基於MathCAD結果的預期不同。 –

回答

1

我認爲一個nswer有點奇怪,但很簡單:你的預期輸出是錯誤的。對我來說你的產出似乎是正確的。

0

您正在使用弧度,而在您期望的輸出中使用的是度數。

更改

private double theta = Math.toRadians(90.00); 

到:

private double theta = 90; 

給你預期的輸出。

+0

,這給了我一個困惑,因爲該文檔指出cos()/ sin()/ tan()函數的輸入必須以弧度表示; –

+1

也許你的預期輸出計算不正確,因爲你是正確的,數學三角函數使用弧度,我只是試過了。您是否認爲您用於預期輸出的方法使用度數,但實際上它使用的是弧度? – Blub

+0

我讀過MathCAD的文檔。 @埃德加博達你是對的。問題在於用於計算預期結果的方法。請添加您的答案,我會將其投票作爲正確的答案。 –