Skip to content

Commit

Permalink
- Started to use Require.js for the unit testing projects
Browse files Browse the repository at this point in the history
- Completely changing the structure of the project using prototype, this way allows for code assist!
- Adding new objects for Matrix22 and Matrix44
- Better naming conventions for the objects
- Fixed the return / function chaining problem, now should work better
  • Loading branch information
crashtheuniverse committed Jun 10, 2012
1 parent 1478894 commit 7b93943
Show file tree
Hide file tree
Showing 13 changed files with 391 additions and 67 deletions.
12 changes: 10 additions & 2 deletions MathEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,26 @@ MW.create = function(o) {
return new F();
};

/**
* Matrix33 helper function
* @return (Matrix33)
*/
MW.m33 = function() {
return new MW.Matrix33();
}

MW.v3 = function() {
return new MW.Vector3();
MW.m22 = function() {
return new MW.Matrix22();
}

MW.v2 = function() {
return new MW.Vector2();
}

MW.v3 = function() {
return new MW.Vector3();
}

MW.v4 = function() {
return new MW.Vector4();
}
168 changes: 168 additions & 0 deletions Matrix22.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/**
* @author CrashTheuniversE
*/

/**
* Matrix22 class
*/
MW.Matrix22 = function() {

this.rows = 2;
this.cols = 2;
this.m = new Array(4);
}

MW.Matrix22.prototype.identity = function() {
this.m[0] = 1.0; this.m[1] = 0.0;
this.m[2] = 0.0; this.m[3] = 1.0;
return this;
}

MW.Matrix22.prototype.zero = function() {
for (var i = 0; i < this.m.length; ++i) {
this.m[i] = 0.0;
}
return this;
}

MW.Matrix22.prototype.determinant = function() {
var det = this.m[0] * this.m[3] - this.m[1] * this.m[2];
return det;
}

MW.Matrix22.prototype.trace = function() {
var t = this.m[0] + this.m[3];
return t;
}

MW.Matrix22.prototype.scale = function(sx, sy) {
this.m[0] *= sx;
this.m[3] *= sy;
return this;
}

MW.Matrix22.prototype.copy = function(mtx) {
for (var i = 0; i < this.m.length; ++i) {
this.m[i] = mtx.m[i];
}
return this;
}

MW.Matrix22.prototype.transpose = function() {
for (var i = 0; i < this.rows; ++i) {
for (var j = i; j < this.cols; ++j) {
var tmp = this.m[(j * this.rows) + i];
this.m[(j * this.rows) + i] = this.m[(i * this.cols) + j];
this.m[(i * this.cols) + j] = tmp;
}
}
return this;
}

MW.Matrix22.prototype.inverse = function() {
//CTU: This is the same rule as 3x3 matrix.
// Calculate the matrix made of the cofactors
// Transpose the CoFactor Matrix
// Scalar multiply the Transpose CoFactor Matrix by the inverse of the Det

var invMtx = new MW.Matrix22();
var invDet = 1.0 / this.determinant();

invMtx.m[0] = invDet * this.m[3];
invMtx.m[1] = -invDet * this.m[1];
invMtx.m[2] = -invDet * this.m[2];
invMtx.m[3] = invDet * this.m[0];

this.copy(invMtx);
return this;
}

MW.Matrix22.prototype.equal = function(mtx) {

for (var i = (this.m.length - 1); i >= 0; --i) {
if (mtx.m[i] !== this.m[i])
return false;
}
return true;
}

MW.Matrix22.prototype.scalarMultiply = function(s) {
for (var i = (this.m.length - 1); i >= 0; --i) {
this.m[i] = this.m[i] * s;
}
return this;
}

MW.Matrix22.prototype.vectorMultiply = function(v) {

var _v = [];

for (var i = 0; i < this.rows; ++i) {
_v[i] = this.m[(i * this.columns) + 0] * v.x + this.m[(i * this.columns) + 1] * v.y;
}

var nv = new MW.Vector2();
nv.arraySet(_v);
return nv;
}

MW.Matrix22.prototype.multiply = function(rMtx) {
var pMtx = new MW.Matrix22();

for (var i = 0; i < this.rows; ++i) {
for (var j = 0; j < this.cols; ++j) {
var value = this.m[(i * this.cols) + 0] * rMtx.m[(0 * this.rows) + j] + this.m[(i * this.cols) + 1] * rMtx.m[(1 * this.rows) + j];

pMtx.m[(i * this.cols) + j] = value;
}
}
this.copy(pMtx);
return this;
}

MW.Matrix22.prototype.add = function(rMtx) {
for (var i = 0; i < this.m.length; ++i) {
this.m[i] += rMtx.m[i];
}
return this;
}

MW.Matrix22.prototype.makeScale = function(sx, sy) {
this.zero();
this.m[0] = sx; this.m[3] = sy;
return this;
}

MW.Matrix22.prototype.makeRotation = function(a) {
var ca = Math.cos(a);
var sa = Math.sin(a);
this.m[0] = ca; this.m[1] = -sa;
this.m[2] = sa; this.m[3] = ca;
return this;
}


MW.Matrix22.prototype.getColumn = function(idx) {
var c = [];
for (var i = 0; i < this.rows; ++i) {
c[i] = this.m[(i * this.cols) + idx];
}

var v = new MW.Vector2();
v.arraySet(c);

return v;
}

MW.Matrix22.prototype.setColumn = function(idx, v) {
this.m[0 * this.cols + idx] = v.x;
this.m[1 * this.cols + idx] = v.y;
return this;
}

MW.Matrix22.prototype.toString = function() {
var str = "Matrix:\n";
str += "" + this.m[0] + " " + this.m[1] + "\n";
str += "" + this.m[2] + " " + this.m[3] + "\n";
return str;
}
9 changes: 9 additions & 0 deletions Matrix33.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* The reference system is Right-Handed
* Positive angles -> counter clockwise
*/

/**
* Matrix33
*/

MW.Matrix33 = function() {

this.rows = 3;
Expand Down Expand Up @@ -345,6 +350,10 @@ MW.Matrix33 = function() {
}
}

MW.Matrix33.prototype.aux = function () {
return "Auxiliary";
}

MW.Matrix33.identity = function() {
var mtx = new MW.Matrix33();
mtx.setIdentity();
Expand Down
3 changes: 3 additions & 0 deletions Matrix44.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* @author CrashTheuniversE
*/
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@ var newMtx = mtx33.getInverse();

Will store a new matrix in newMtx which is the inverse of the mtx33, leaving mtx33 unaltered.

For debugging purposes most of the objects override the .toString() method to report something meaningful.
For debugging purposes most of the objects override the .toString() method to report something meaningful.

ToDo
=======

* Complete matrices implementation
* Include 2D geometry helpers

1 change: 0 additions & 1 deletion Vector3.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ MW.Vector3 = function(x, y, z) {
}

this.equal = function(v) {

if( Math.abs(this.x - v.x) > 0.00001 ) return false;
if( Math.abs(this.y - v.y) > 0.00001 ) return false;
if( Math.abs(this.z - v.z) > 0.00001 ) return false;
Expand Down
2 changes: 1 addition & 1 deletion compile_test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
java -jar compiler/compiler.jar --js *.js --js_output_file lib/mathjs.js

cp lib/mathjs.js test/mathjs.js
cp lib/mathjs.js test/scripts/mathjs.js

8 changes: 6 additions & 2 deletions lib/mathjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions test/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<script type="text/javascript" src="mathjs.js"></script>
<!--<script type="text/javascript" src="mathjs.js"></script>
<script type="text/javascript" src="main.js"></script>
-->
<script data-main="scripts/main" src="scripts/require.js"></script>
</head>
<body onload="main()">
<!--<body onload="main()">-->
<body>
<div>
<header>
<h1>MathJS Tests</h1>
Expand Down
56 changes: 56 additions & 0 deletions test/scripts/aux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @author CrashTheuniversE
*/
function print(str) {

var txt = document.createElement('p');
txt.innerText = str;
document.body.appendChild(txt);
}

TestSuite = function(name) {

var tests = [];
var passed = true;
var suiteName = name || "Generic";

this.addTest = function(desc, test) {
tests.push({d: desc, t: test});
}

this.print = function(str) {
var txt = document.createElement('p');
txt.innerText = str;
document.body.appendChild(txt);
}

this.printHeader = function(str) {

var txt = document.createElement('h2');
txt.innerText = str;
document.body.appendChild(txt);
}

this.runTests = function() {

this.printHeader("Running suite " + suiteName);
this.printHeader("------------------------------------");

passed = true;

for(var i = 0, count = tests.length; i < count; ++i)
{
var local = tests[i].t();
var txt = local ? "PASS" : "FAIL";
this.printHeader("Function:" + tests[i].t.name + " Description:" + tests[i].d + " :" + txt);
passed = passed & local;
}

if(passed)
this.printHeader("TEST PASSED");
else
this.printHeader("TEST FAILED");

return passed;
}
}
Loading

0 comments on commit 7b93943

Please sign in to comment.