Permalink
Browse files

vec2.lerp

  • Loading branch information...
sinisterchipmunk committed May 3, 2012
1 parent dc9808e commit 5874379975b78888a807c4f7496f32b7afcba7ed
Showing with 34 additions and 0 deletions.
  1. +17 −0 gl-matrix.js
  2. +17 −0 spec/javascripts/vec2_spec.js
View
@@ -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
*/
@@ -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); });

0 comments on commit 5874379

Please sign in to comment.