Skip to content

Commit

Permalink
mat2.multiplyVec2
Browse files Browse the repository at this point in the history
sinisterchipmunk committed May 3, 2012
1 parent f783ba2 commit 9d8005a
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gl-matrix.js
Original file line number Diff line number Diff line change
@@ -2682,6 +2682,23 @@
return dest;
}

/**
* Multiplies the vec2 by the given 2x2 matrix
*
* @param {mat2} matrix the 2x2 matrix to multiply against
* @param {vec2} vec the vector to multiply
* @param {vec2} [dest] an optional receiving vector. If not given, vec is used.
*
* @returns {vec2} The multiplication result
**/
mat2.multiplyVec2 = function(matrix, vec, dest) {
if (!dest) dest = vec;
var x = vec[0], y = vec[1];
dest[0] = x * matrix[0] + y * matrix[1];
dest[1] = x * matrix[2] + y * matrix[3];
return dest;
};

/*
* Exports
*/
20 changes: 20 additions & 0 deletions spec/javascripts/mat2_spec.js
Original file line number Diff line number Diff line change
@@ -102,4 +102,24 @@ describe("mat2", function() {
it("should return a", function() { expect(result).toBe(a); });
});
});

describe("multiplyVec2", function() {
beforeEach(function() { b = [5, 6]; dest = [0, 0]; });

describe("with dest", function() {
beforeEach(function() { result = mat2.multiplyVec2(a, b, dest); });
it("should set dest", function() { expect(dest).toBeEqualish([17, 39]); });
it("should return dest", function() { expect(result).toBe(dest); });
it("should not modify a", function() { expect(a).toBeEqualish([1, 2, 3, 4]); });
it("should not modify b", function() { expect(b).toBeEqualish([5, 6]); });
});

describe("without dest", function() {
beforeEach(function() { result = mat2.multiplyVec2(a, b); });
it("should not change a", function() { expect(a).toBeEqualish([1, 2, 3, 4]); });
it("should change b", function() { expect(b).toBeEqualish([17, 39]); });
it("should return b", function() { expect(result).toBe(b); });
});
});

});

0 comments on commit 9d8005a

Please sign in to comment.