2013-12-12 96 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的文档。 @埃德加博达你是对的。问题在于用于计算预期结果的方法。请添加您的答案,我会将其投票作为正确的答案。 –