Java //数组//乘法// For循环;

时间:2013-12-12 23:58:38

标签: java arrays for-loop rotational-matrices

目标:使用旋转矩阵生成旋转;

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;
    }
}
}

2 个答案:

答案 0 :(得分:1)

我认为答案有点奇怪,但很简单:你的预期输出是错误的。对我来说,你产生的输出似乎是正确的。

答案 1 :(得分:0)

您正在使用弧度,而在您的预期输出中,您使用的是度数。

更改

private double theta = Math.toRadians(90.00);

为:

private double theta = 90;

为您提供预期的输出。