Skip to content

Commit

Permalink
feat: add sqrLength method
Browse files Browse the repository at this point in the history
  • Loading branch information
learosema committed Dec 7, 2021
1 parent 01109f0 commit b046a74
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/vector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ describe('2D Vector arithmetics', () => {
expect(vector.length).toBe(5);
});

test('Vec sqrLength', () => {
const v = new Vec(3, 4);
expect(v.sqrLength).toBe(25);
});


test('Vec lerp test', () => {
const x = new Vec(2, 0);
const y = new Vec(0, 2);
Expand Down
8 changes: 8 additions & 0 deletions src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ export class Vec {
return Math.sqrt(this.values.map((v) => v ** 2).reduce((a, b) => a + b));
}

/**
* Calculate sqrLength
* @returns the squared length of this vector
*/
get sqrLength() {
return this.values.map((v) => v ** 2).reduce((a, b) => a + b);
}

/**
* Linear interpolation
* @param otherVec other vector to interpolate to
Expand Down

0 comments on commit b046a74

Please sign in to comment.