繁体   English   中英

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

[英]Java // Array // Multiplication // For-loop;

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

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

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

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

更改

private double theta = Math.toRadians(90.00);

至:

private double theta = 90;

给您预期的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM