2013-12-12 3 views
0

목표 : 회전 행렬을 사용하여 회전을 생성합니다.자바 // 배열 // 곱셈 // 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; 
    } 
} 
} 
+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에 대한 설명서를 읽었습니다. @ 에드거 보더 당신 말이 맞아요. 문제는 예상 된 결과를 계산하는 데 사용 된 접근법에있었습니다. 귀하의 답변을 추가하십시오. 그리고 나는 정답으로 투표 할 것입니다. –