Skip to content

Commit

Permalink
feat(vector): distance between two vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Aug 23, 2018
1 parent a0d14a0 commit 88326ad
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const X = Symbol.for('x');
const Y = Symbol.for('y');
const Z = Symbol.for('z');

function square(val) {
return val * val;
}
class AVector {
constructor(x, y, z) {
if (typeof x === 'function') {
Expand Down Expand Up @@ -96,6 +99,14 @@ class AVector {
);
}

distance(v) {
return Math.sqrt(square(this.x - v.x) + square(this.y - v.y) + square(this.z - v.z));
}

dist(v) {
return this.distance(v);
}

toArray() {
return [this.x, this.y, this.z];
}
Expand Down Expand Up @@ -128,6 +139,7 @@ cachedMethod(AVector, 'crossNormalize');
cachedMethod(AVector, 'toAngles');
cachedMethod(AVector, 'angleTo');
cachedMethod(AVector, 'rotate');
cachedMethod(AVector, 'distance');
cachedMethod(AVector, 'toArray');
cachedGetter(AVector, 'length');

Expand Down

0 comments on commit 88326ad

Please sign in to comment.