Skip to content

Commit

Permalink
vec2.lerp
Browse files Browse the repository at this point in the history
  • Loading branch information
sinisterchipmunk committed May 3, 2012
1 parent dc9808e commit 5874379
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gl-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,23 @@
return dest;
};

/**
* Performs a linear interpolation between two vec2
*
* @param {vec2} vecA First vector
* @param {vec2} vecB Second vector
* @param {Number} lerp Interpolation amount between the two inputs
* @param {vec2} [dest] vec2 receiving operation result. If not specified result is written to vecA
*
* @returns {vec2} dest if specified, vecA otherwise
*/
vec2.lerp = function (vecA, vecB, lerp, dest) {
if (!dest) { dest = vecA; }
dest[0] = vecA[0] + lerp * (vecB[0] - vecA[0]);
dest[1] = vecA[1] + lerp * (vecB[1] - vecA[1]);
return dest;
};

/*
* Exports
*/
Expand Down
17 changes: 17 additions & 0 deletions spec/javascripts/vec2_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ describe("vec2", function() {
});
});

describe("lerp", function() {
describe("with dest", function() {
beforeEach(function() { result = vec2.lerp(vecA, vecB, 0.5, dest); });
it("should return dest", function() { expect(result).toBe(dest); });
it("should set dest to correct value", function() { expect(dest).toBeEqualish([2, 3]); });
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", function() {
beforeEach(function() { result = vec2.lerp(vecA, vecB); });
it("should return vecA", function() { expect(result).toBe(vecA); });
it("should modify vecA", function() { expect(vecA).toBeEqualish([2, 3]); });
it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 4]); });
});
});

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

0 comments on commit 5874379

Please sign in to comment.