目標:使用旋轉矩陣產生旋轉;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;
}
}
}
當您通過調試器完成此操作時,發現了什麼? –
@OliCharlesworth - 沒有預期的過程流程,但是輸出與我期望的基於MathCAD結果的預期不同。 –