tiny vector perf improvements

This commit is contained in:
Orion Reed 2024-12-05 21:55:24 -05:00
parent 3607ce237a
commit b850fc9704
1 changed files with 13 additions and 2 deletions

View File

@ -57,7 +57,7 @@ export class Vector {
* @returns {number} The magnitude of the vector
*/
static mag(v: Point): number {
return hypot(v.x, v.y);
return Math.sqrt(v.x * v.x + v.y * v.y);
}
/**
@ -80,7 +80,9 @@ export class Vector {
* @returns {number} The distance between the points
*/
static distance(a: Point, b: Point): number {
return hypot(a.x - b.x, a.y - b.y);
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
@ -179,4 +181,13 @@ export class Vector {
y: point.y - origin.y,
});
}
/**
* Calculates the squared magnitude of a vector
* @param {Point} v - The vector
* @returns {number} The squared magnitude of the vector
*/
static magSquared(v: Point): number {
return v.x * v.x + v.y * v.y;
}
}