2016-12-10 8 views
-1

벡터를 변환하는 데 createTranslation 함수를 사용하려고합니다. 이것은 재스민 검사를 통과해야합니다.매트릭스 번역

내 행렬 객체는 :

/*global it, describe, expect, Vector, Matrix*/ 
/// <reference path="../js/vector.js"/> 
/// <reference path="../js/matrix.js"/> 
/// <reference path="../Scripts/jasmine.js"/> 

var Matrix = (function() { 
function Matrix(pX0, pX1, pX2, pY0, pY1, pY2, pZ0, pZ1, pZ2) { 

    this.matrix = [ 
     [pX0, pX1, pX2], 
     [pY0, pY1, pY2], 
     [pZ0, pZ1, pZ2] 
    ]; 
} 
Matrix.prototype.getX0 = function() { 
    return this.mX0; 
}; 
Matrix.prototype.setX0 = function (pX0) { 
    this.mX0 = pX0; 
}; 
Matrix.prototype.getX1 = function() { 
    return this.mX1; 
}; 
Matrix.prototype.setX1 = function (pX1) { 
    this.mX1 = pX1; 
}; 
Matrix.prototype.getX2 = function() { 
    return this.mX2; 
}; 
Matrix.prototype.setX2 = function (pX2) { 
    this.mX2 = pX2; 
}; 


Matrix.prototype.getY0 = function() { 
    return this.mY0; 
}; 
Matrix.prototype.setY0 = function (pY0) { 
    this.mY0 = pY0; 
}; 
Matrix.prototype.getY1 = function() { 
    return this.mY1; 
}; 
Matrix.prototype.setY1 = function (pY1) { 
    this.mY1 = pY1; 
}; 
Matrix.prototype.getY2 = function() { 
    return this.mY2; 
}; 
Matrix.prototype.setY2 = function (pY2) { 
    this.mY2 = pY2; 
}; 


Matrix.prototype.getZ0 = function() { 
    return this.mZ0; 
}; 
Matrix.prototype.setZ0 = function (pZ0) { 
    this.mZ0 = pZ0; 
}; 
Matrix.prototype.getZ1 = function() { 
    return this.mZ0; 
}; 
Matrix.prototype.setZ1 = function (pZ1) { 
    this.mZ1 = pZ1; 
}; 
Matrix.prototype.getZ2 = function() { 
    return this.mZ2; 
}; 
Matrix.prototype.setZ2 = function (pZ2) { 
    this.mZ2 = pZ2; 
}; 
Matrix.prototype.getElement = function (pRow, pColumn) { 
    return this.matrix[pRow][pColumn]; 
}; 

Matrix.createIdentity = function() { 
    return new Matrix(1, 0, 0, 0, 1, 0, 0, 0, 1); 
}; 
Matrix.createTranslation = function (translationVector) { 

    return new Matrix(1, 0, translationVector.getX(), 
     0, 1, translationVector.getY(), 0, 0, 1); 
}; 
Matrix.createScale = function (scaleVector) { 

    return new Matrix(scaleVector.getX(), 0, 0, 
     0, scaleVector.getY(), 0, 0, 0, 1); 
}; 
Matrix.createRotation = function (rotation) { 

    return new Matrix(Math.cos(rotation), -Math.sin(rotation), 0, 
     Math.sin(rotation), Math.cos(rotation), 0, 0, 0, 1); 
}; 

Matrix.prototype.multiplyVector = function (vector) { 
    vector = new Vector(); 
    return new Vector(this.getX0() * vector.getX(), 
     this.getX1() * vector.getY(), this.getX2() * vector.getZ(), 
     this.getY0() * vector.getX(), 
     this.getY1() * vector.getY(), this.getY2() * vector.getZ(), 
     this.getZ0() * vector.getX(), 
     this.getZ1() * vector.getY(), this.getZ2() * vector.getZ()); 
}; 

Matrix.prototype.multiply = function (secMatrix) { 
    secMatrix = this.matrix(); 
    return new Matrix(this.getX0 * secMatrix.getX0, 
     this.getX1 * secMatrix.getY0, 
     this.getX2 * secMatrix.getZ0, 
     this.getY0 * secMatrix.getX1, 
     this.getY1 * secMatrix.getY1, 
     this.getY2 * secMatrix.getZ1, 
     this.getZ0 * secMatrix.getX2, 
     this.getZ1 * secMatrix.getY2, 
     this.getZ2 * secMatrix.getZ2); 

}; 
//Matrix.prototype.multiplyVector = function (translationVector, 
//secondVector, vector) { 

// return new Vector(this.getX0 * Matrix.createTranslation(translationVector), 
//  this.getX1 * Matrix.createTranslation(translationVector), 
//  this.getX2 * Matrix.createTranslation(translationVector), 
//  this.getY0 * Matrix.createTranslation(translationVector), 
//  this.getY1 * Matrix.createTranslation(translationVector), 
//  this.getY2 * Matrix.createTranslation(translationVector), 
//  this.getZ0 * Matrix.createTranslation(translationVector), 
//  this.getZ1 * Matrix.createTranslation(translationVector), 
//  this.getZ2 * Matrix.createTranslation(translationVector)); 
//}; 
Matrix.prototype.multiplyVectors = function (translationVector, 
    vector, secondVector) { 

    return new Vector(vector.getX() * Matrix.createTranslation(translationVector), 
     vector.getY() * Matrix.createTranslation(translationVector), 
     vector.getZ() * Matrix.createTranslation(translationVector), 
     secondVector = this.multiplyVector(vector)); 
}; 
//Matrix.prototype.multiply = function() { 
// return new Matrix(); 
//}; 

return Matrix; 

}()); 

댓글 부품이 날이를 달성하기 위해 여러 가지 방법을 시도하고있다.

이 개체는 다음 테스트를 통과해야합니다.

describe("Multiply vector", function() { 
    describe("Translation", function() { 
     var vector, translationVector, matrix, secondVector; 
     vector = new Vector(30, 40, 1); 
     translationVector = new Vector(10, 20, 1); 
     matrix = Matrix.createTranslation(translationVector); 
     secondVector = matrix.multiplyVector(vector); 
     it("X Set", function() { 
      expect(secondVector.getX()).toEqual(40); 
     }); 

     it("Y Set", function() { 
      expect(secondVector.getY()).toEqual(60); 
     }); 

     it("Z Set", function() { 
      expect(secondVector.getZ()).toEqual(1); 
     }); 
    }); 
    describe("Rotation", function() { 
     var vector, rotation, matrix, secondVector; 
     vector = new Vector(30, 40, 1); 
     rotation = Math.PI/2; 
     matrix = Matrix.createRotation(rotation); 
     secondVector = matrix.multiplyVector(vector); 
     it("X Set", function() { 
      expect(secondVector.getX()).toBeCloseTo(-40, 1); 
     }); 

     it("Y Set", function() { 
      expect(secondVector.getY()).toBeCloseTo(30, 1); 
     }); 

     it("Z Set", function() { 
      expect(secondVector.getZ()).toBeCloseTo(1, 1); 
     }); 
    }); 
    describe("Scale", function() { 
     var vector, scaleVector, matrix, secondVector; 
     vector = new Vector(30, 40, 1); 
     scaleVector = new Vector(2, 2, 1); 
     matrix = Matrix.createScale(scaleVector); 
     secondVector = matrix.multiplyVector(vector); 
     it("X Set", function() { 
      expect(secondVector.getX()).toEqual(60); 
     }); 

     it("Y Set", function() { 
      expect(secondVector.getY()).toEqual(80); 
     }); 

     it("Z Set", function() { 
      expect(secondVector.getZ()).toEqual(1); 
     }); 
    }); 
}); 

회전에 대한 다른 테스트가 있지만, 일단 이것을 파악하면 나머지는 따라옵니다.

나는 내가해야 할 일이 확실하지 않으므로 각 언어에 대해 더 많은 기능을 생성 할 수 있습니까? 아니면 multiplyVector 함수로 변환 할 수 있습니까?

편집 :

Matrix.prototype.multiplyVector = function (translationVector) { 

    return new Vector(Matrix.createTranslation().getX0 *  translationVector.getX() + 
      Matrix.createTranslation().getX1 * translationVector.getY() + 
      Matrix.createTranslation().getX2 * translationVector.getZ(), 
      Matrix.createTranslation().getY0 * translationVector.getX() + 
      Matrix.createTranslation().getY1 * translationVector.getY() + 
      Matrix.createTranslation().getY2 * translationVector.getZ(), 
      Matrix.createTranslation().getZ0 * translationVector.getX() + 
      Matrix.createTranslation().getZ1 * translationVector.getY() + 
      Matrix.createTranslation().getZ2 * translationVector.getZ()); 

}; 

나는 이런 식으로 그것을 할 시도했지만 여전히 작동하지 않습니다.

+0

'multiplyVector' 함수는 행렬에 벡터를 곱하는 것입니다. 그렇지 않습니까? 이 숨겨진 모든 기능을 그 안에 포함해야합니까? 실제 문제는 무엇입니까? – Abion47

+0

나는 그뿐만 아니라 모든 테스트와 마찬가지로 multipllyVector와 같은 함수의 이름을 포함하고 multiplyVectorTranslate는 포함하지 않는다고 생각합니다. 각 Translate, rotate 및 scale에 대해 새로운 함수가 필요한지 궁금 해서요. 벡터를 곱하고 단일 MultiplyVector 함수에서 translate, rotate 및 scale 연산을 포함하는 것이 가능합니다. – Quad117

+1

'multiplyVector '를 호출하는 행렬에는 이미 translation/rotation/scale 데이터가 있습니다. 변환 행렬에 벡터를 곱하는 것은 벡터에 변환을 적용하는 방법입니다. 함수 내에서 변환을 다시 생성하면 잘못된 결과가 나타납니다. – Abion47

답변

1

편집에서 코드가 올바르게 작동하지만 의견에서 말했듯이 벡터를 곱한 행렬에 이미 변형 정보가 있습니다. 다시 생성 할 필요가 없을뿐만 아니라 그렇게하면 변환 데이터가 잘못된 출력을 얻을 수있는 지점으로 변경됩니다. 제공된 qxz

자원 :

enter image description here

은 열 벡터에 의해 행렬 곱셈 정확한 방법이다. 코드로 구현하려면 다음을 수행하십시오.

Matrix.prototype.multiplyVector = function(vector) { 
    return new Vector(
     this.getX0() * vector.getX() + this.getX1() * vector.getY() + this.getX2() * vector.getZ(), 
     this.getY0() * vector.getX() + this.getY1() * vector.getY() + this.getY2() * vector.getZ(), 
     this.getZ0() * vector.getX() + this.getZ1() * vector.getY() + this.getZ2() * vector.getZ() 
    ); 
}