Skip to content

Commit

Permalink
vec2.multiply, vec2.divide
Browse files Browse the repository at this point in the history
  • Loading branch information
sinisterchipmunk committed May 2, 2012
1 parent be9d679 commit 19f3765
Showing 2 changed files with 70 additions and 0 deletions.
32 changes: 32 additions & 0 deletions gl-matrix.js
Original file line number Diff line number Diff line change
@@ -2303,6 +2303,38 @@
dest[1] = vecA[1] - vecB[1];
return dest;
};

/**
* Multiplies vecA with vecB. If dest is given, the result
* is stored there. Otherwise, the result is stored in vecB.
*
* @param {vec2} vecA the first operand
* @param {vec2} vecB the second operand
* @param {vec2} dest the optional receiving vector
* @returns {vec2} dest
*/
vec2.multiply = function(vecA, vecB, dest) {
if (!dest) dest = vecB;
dest[0] = vecA[0] * vecB[0];
dest[1] = vecA[1] * vecB[1];
return dest;
};

/**
* Divides vecA by vecB. If dest is given, the result
* is stored there. Otherwise, the result is stored in vecB.
*
* @param {vec2} vecA the first operand
* @param {vec2} vecB the second operand
* @param {vec2} dest the optional receiving vector
* @returns {vec2} dest
*/
vec2.divide = function(vecA, vecB, dest) {
if (!dest) dest = vecB;
dest[0] = vecA[0] / vecB[0];
dest[1] = vecA[1] / vecB[1];
return dest;
};

/*
* Exports
38 changes: 38 additions & 0 deletions spec/javascripts/vec2_spec.js
Original file line number Diff line number Diff line change
@@ -41,6 +41,44 @@ describe("vec2", function() {
});
});

describe("multiply", function() {
describe("with dest vec2", function() {
beforeEach(function() { result = vec2.multiply(vecA, vecB, dest); });

it("should place values into dest", function() { expect(dest).toBeEqualish([3, 8]); });
it("should return dest", function() { expect(result).toBe(dest); });
it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2]); });
it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 4]); });
});

describe("without dest vec2", function() {
beforeEach(function() { result = vec2.multiply(vecA, vecB); });

it("should place values into vecB", function() { expect(vecB).toBeEqualish([3, 8]); });
it("should return vecB", function() { expect(result).toBe(vecB); });
it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2]); });
});
});

describe("divide", function() {
describe("with dest vec2", function() {
beforeEach(function() { result = vec2.divide(vecA, vecB, dest); });

it("should place values into dest", function() { expect(dest).toBeEqualish([0.333333, 0.5]); });
it("should return dest", function() { expect(result).toBe(dest); });
it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2]); });
it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 4]); });
});

describe("without dest vec2", function() {
beforeEach(function() { result = vec2.divide(vecA, vecB); });

it("should place values into vecB", function() { expect(vecB).toBeEqualish([0.333333, 0.5]); });
it("should return vecB", function() { expect(result).toBe(vecB); });
it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2]); });
});
});

describe("create", function() {
describe("with vec", function() {
it("should clone vec's contents", function() {

0 comments on commit 19f3765

Please sign in to comment.