Java//数组//乘法//For循环;
目标:使用旋转矩阵生成旋转Java//数组//乘法//For循环;,java,arrays,for-loop,rotational-matrices,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 M
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;
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;
}
}
}
产生的输出:
newX = 3.00
newY =-2.00
newZ = 1.0000000000000002
应用的代码:
newX = 3
newY = -2.236
newZ = -0.002
// Checked in MathCAD;
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;
}
}
}
公共类Test2{
private double[]target={3.00,1.00,2.00};//矩阵乘法的目标数据;
私有双[]乘积={0.00,0.00,0.00};//for循环的乘积;
private double theta=Math.toRadians(90.00);//旋转角度;
私人双[]旋转={{1.00,0.00,0.00},
{0.00,Math.cos(θ),Math.sin(-θ)},
{0.00,Math.sin(θ),Math.cos(θ)};//x轴的旋转矩阵;
私有void rotateAroundX(){
双温度;//温度数据存储;
double newX=0.00;//for循环后的新x;
double newY=0.00;//在for循环之后新y;
double newZ=0.00;//for循环后的新z;
对于(int i=0;i
我认为答案有点奇怪,但很简单:您预期的输出是错误的。在我看来,您生成的输出似乎是正确的。您使用的是弧度,而在预期输出中,您使用的是度
改变
private double theta = Math.toRadians(90.00);
致:
提供您期望的输出。当您使用调试器逐步完成此操作时,您发现了什么?@OliCharlesworth-没有任何东西流程如预期的那样,但是输出与基于MathCAD结果的我所期望的不同。这让我感到困惑,因为该文档说明cos()/sin()的输入/tan()函数必须以弧度为单位;也许你的预期输出计算不正确,因为你是对的,数学三角函数使用弧度,我只是尝试了一下。您是否认为用于预期输出的方法使用度,但实际上它使用弧度?我已经阅读了MathCAD的文档@埃德加波达:你说得对。问题在于用于计算预期结果的方法。请加上你的答案,我将投票选出正确答案。