Skip to content

Commit

Permalink
vec2.dist
Browse files Browse the repository at this point in the history
  • Loading branch information
sinisterchipmunk committed May 2, 2012
1 parent 4413cf6 commit 018e2c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gl-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,22 @@
return dest;
};

/**
* Calculates the euclidian distance between two vec2
*
* Params:
* @param {vec2} vecA First vector
* @param {vec2} vecB Second vector
*
* @returns {number} Distance between vecA and vecB
*/
vec2.dist = function (vecA, vecB) {
var x = vecB[0] - vecA[0],
y = vecB[1] - vecA[1];

return Math.sqrt(x*x + y*y);
};

/*
* Exports
*/
Expand Down
8 changes: 8 additions & 0 deletions spec/javascripts/vec2_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ describe("vec2", function() {
var vecA, vecB, result;
beforeEach(function() { vecA = [1, 2]; vecB = [3, 4]; dest = [0, 0]; });

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

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

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

0 comments on commit 018e2c1

Please sign in to comment.