Skip to content

Commit

Permalink
Updated matrix pass through methods. Added more matrix unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tripp committed Dec 4, 2011
1 parent dc4e0dd commit 3292eba
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/matrix/js/Matrix.js
Expand Up @@ -363,7 +363,7 @@ Matrix.prototype = {
*/
getDeterminant: function()
{
return Y.MatrixUtil.getDeterminant.apply(this, [this.getMatrixArray()]);
return Y.MatrixUtil.getDeterminant(this.getMatrixArray());
},

/**
Expand All @@ -374,7 +374,7 @@ Matrix.prototype = {
*/
inverse: function()
{
return Y.MatrixUtil.inverse.apply(this, [this.getMatrixArray()]);
return Y.MatrixUtil.inverse(this.getMatrixArray());
},

/**
Expand All @@ -385,7 +385,7 @@ Matrix.prototype = {
*/
transpose: function()
{
return Y.MatrixUtil.transpose.apply(this, [this]);
return Y.MatrixUtil.transpose(this.getMatrixArray());
},

/**
Expand All @@ -396,7 +396,7 @@ Matrix.prototype = {
*/
decompose: function()
{
return Y.MatrixUtil.decompose.apply(this, [this.getMatrixArray()]);
return Y.MatrixUtil.decompose(this.getMatrixArray());
}
};

Expand Down
292 changes: 291 additions & 1 deletion src/matrix/tests/matrix-test.js
Expand Up @@ -10,7 +10,7 @@ var Assert = Y.Assert,
suite = new Y.Test.Suite('Y.Matrix');

suite.add(new Y.Test.Case({
name: 'Matrix tests',
name: 'MatrixUtil tests',

testGet3x3Determinant: function() {
var matrix = [
Expand Down Expand Up @@ -446,6 +446,296 @@ suite.add(new Y.Test.Case({
}
}));


suite.add(new Y.Test.Case({
name: 'Matrix tests',

testGetDeterminant: function() {
determinant = 1,
mymatrix = new Y.Matrix();
mymatrix.rotate(45);
/*
[0.70711, -0.70711, 0]
[0.70711, 0.70711, 0]
[0, 0, 1]
*/
Y.Assert.areEqual(Round(mymatrix.getDeterminant()), determinant, "The determinant should be: " + determinant + ".");

mymatrix.init();
mymatrix.translate(15, 15);
/*
[1, 0, 15]
[0, 1, 15]
[0, 0, 1]
*/

Y.Assert.areEqual(Round(mymatrix.getDeterminant()), determinant, "The determinant should be: " + determinant + ".");

mymatrix.skewX(15);
/*
[1, 0.26795, 15]
[0, 1, 15]
[0, 0, 1]
*/
Y.Assert.areEqual(Round(mymatrix.getDeterminant()), determinant, "The determinant should be: " + determinant + ".");

mymatrix.scale(33, 11);
/*
[33, 2.9474500000000003, 15]
[0, 11, 15]
[0, 0, 1]
*/
determinant = 363;
Y.Assert.areEqual(Round(mymatrix.getDeterminant()), determinant, "The determinant should be: " + determinant + ".");

mymatrix.skewY(67);
/*
[39.9437500825, 2.9474500000000003, 15]
[25.914350000000002, 11, 15]
[0, 0, 1]
*/
Y.Assert.areEqual(Round(mymatrix.getDeterminant()), determinant, "The determinant should be: " + determinant + ".");
},

testGetInverse: function() {
var result,
i,
j,
inverse,
len = 3,
mymatrix = new Y.Matrix();
mymatrix.rotate(45);
/*
[0.70711, -0.70711, 0]
[0.70711, 0.70711, 0]
[0, 0, 1]
*/
//inverse test
inverse = [
[0.707, 0.707, 0.000],
[-0.707, 0.707, 0.000],
[0.000, 0.000, 1.000]
];
result = mymatrix.inverse();

for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(inverse[i][j]), Round(result[i][j]), "The result of inverse" + (j + 1) + "," + (i +1) + " should be " + inverse[i][j] + ", not " + result[i][j]);
}
}

//clear and translate
mymatrix.init();
mymatrix.translate(15, 15);
/*
[1, 0, 15]
[0, 1, 15]
[0, 0, 1]
*/
//inverse test
inverse = [
[1.000, 0.000, -15.000],
[0.000, 1.000, -15.000],
[0.000, 0.000, 1.000]
];
result = mymatrix.inverse();
for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(inverse[i][j]), Round(result[i][j]), "The result of inverse" + (j + 1) + "," + (i +1) + " should be " + inverse[i][j] + ", not " + result[i][j]);
}
}

//add a skewX
mymatrix.skewX(15);
/*
[1, 0.26795, 15]
[0, 1, 15]
[0, 0, 1]
*/
//inverse test
inverse = [
[1.000, -0.268, -10.981],
[0.000, 1.000, -15.000],
[0.000, 0.000, 1.000]
];
result = mymatrix.inverse();

for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(inverse[i][j]), Round(result[i][j]), "The result of inverse" + (j + 1) + "," + (i +1) + " should be " + inverse[i][j] + ", not " + result[i][j]);
}
}

//add a scale
mymatrix.scale(33, 11);
/*
[33, 2.9474500000000003, 15]
[0, 11, 15]
[0, 0, 1]
*/
//inverse test
inverse = [
[0.030, -0.008, -0.333],
[0.000, 0.091, -1.364],
[0.000, 0.000, 1.000]
];

result = mymatrix.inverse();

for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(inverse[i][j]), Round(result[i][j]), "The result of inverse" + (j + 1) + "," + (i +1) + " should be " + inverse[i][j] + ", not " + result[i][j]);
}
}

//add a skewY
mymatrix.skewY(67);
/*
[39.9437500825, 2.9474500000000003, 15]
[25.914350000000002, 11, 15]
[0, 0, 1]
*/
//inverse test
inverse = [
[0.030, -0.008, -0.333],
[-0.071, 0.110, -0.580],
[0.000, 0.000, 1.000]
];

result = mymatrix.inverse();

for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(inverse[i][j]), Round(result[i][j]), "The result of inverse" + (j + 1) + "," + (i +1) + " should be " + inverse[i][j] + ", not " + result[i][j]);
}
}
},

testGetTranspose: function() {
var result,
i,
j,
transpose,
len = 3,
mymatrix = new Y.Matrix();
mymatrix.rotate(45);
/*
[0.70711, -0.70711, 0]
[0.70711, 0.70711, 0]
[0, 0, 1]
*/

transpose = [
[0.707, 0.707, 0.000],
[-0.707, 0.707, 0.000],
[0.000, 0.000, 1.000]
];
result = mymatrix.transpose();
for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(transpose[i][j]), Round(result[i][j]), "The result of transpose" + (j + 1) + "," + (i +1) + " should be " + transpose[i][j] + ", not " + result[i][j]);
}
}

//clear and translate
mymatrix.init();
mymatrix.translate(15, 15);
/*
[1, 0, 15]
[0, 1, 15]
[0, 0, 1]
*/
transpose = [
[1.000, 0.000, 0.000],
[0.000, 1.000, 0.000],
[15.000, 15.000, 1.000]
];
result = mymatrix.transpose();
for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(transpose[i][j]), Round(result[i][j]), "The result of transpose" + (j + 1) + "," + (i +1) + " should be " + transpose[i][j] + ", not " + result[i][j]);
}
}

//add a skewX
mymatrix.skewX(15);
/*
[1, 0.26795, 15]
[0, 1, 15]
[0, 0, 1]
*/
transpose = [
[1.000, 0.000, 0.000],
[0.268, 1.000, 0.000],
[15.000, 15.000, 1.000]
];
result = mymatrix.transpose();
for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(transpose[i][j]), Round(result[i][j]), "The result of transpose" + (j + 1) + "," + (i +1) + " should be " + transpose[i][j] + ", not " + result[i][j]);
}
}

//add a scale
mymatrix.scale(33, 11);
/*
[33, 2.9474500000000003, 15]
[0, 11, 15]
[0, 0, 1]
*/
transpose = [
[33.000, 0.000, 0.000],
[2.947, 11.000, 0.000],
[15.000, 15.000, 1.000]
];
result = mymatrix.transpose();
for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(transpose[i][j]), Round(result[i][j]), "The result of transpose" + (j + 1) + "," + (i +1) + " should be " + transpose[i][j] + ", not " + result[i][j]);
}
}

//add a skewY
mymatrix.skewY(67);
/*
[39.9437500825, 2.9474500000000003, 15]
[25.914350000000002, 11, 15]
[0, 0, 1]
*/
transpose = [
[39.944, 25.914, 0.000],
[2.947, 11.000, 0.000],
[15.000, 15.000, 1.000]
];
result = mymatrix.transpose();
for(i = 0; i < len; ++i)
{
for(j = 0; j < len; ++j)
{
Y.Assert.areEqual(Round(transpose[i][j]), Round(result[i][j]), "The result of transpose" + (j + 1) + "," + (i +1) + " should be " + transpose[i][j] + ", not " + result[i][j]);
}
}
}
}));

Y.Test.Runner.add(suite);

}, '@VERSION@', {requires: ['test']});
3 changes: 1 addition & 2 deletions src/matrix/tests/matrix.html
Expand Up @@ -12,8 +12,7 @@
<script>
var Y = YUI({
allowRollup: false,
filter: "raw",
//filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
modules: {
'matrix-test': {
fullpath: 'matrix-test.js',
Expand Down

0 comments on commit 3292eba

Please sign in to comment.