-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
175 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type { Point } from './types.ts'; | ||
|
||
export class Vector { | ||
/** | ||
* Creates a zero vector (0,0) | ||
* @returns {Point} A point representing a zero vector | ||
*/ | ||
static zero(): Point { | ||
return { x: 0, y: 0 }; | ||
} | ||
|
||
/** | ||
* Subtracts vector b from vector a | ||
* @param {Point} a - The first vector | ||
* @param {Point} b - The vector to subtract | ||
* @returns {Point} The resulting vector | ||
*/ | ||
static sub(a: Point, b: Point): Point { | ||
return { x: a.x - b.x, y: a.y - b.y }; | ||
} | ||
|
||
/** | ||
* Adds two vectors together | ||
* @param {Point} a - The first vector | ||
* @param {Point} b - The second vector | ||
* @returns {Point} The sum of the two vectors | ||
*/ | ||
static add(a: Point, b: Point): Point { | ||
return { x: a.x + b.x, y: a.y + b.y }; | ||
} | ||
|
||
/** | ||
* Multiplies two vectors component-wise | ||
* @param {Point} a - The first vector | ||
* @param {Point} b - The second vector | ||
* @returns {Point} The component-wise product of the two vectors | ||
*/ | ||
static mult(a: Point, b: Point): Point { | ||
return { x: a.x * b.x, y: a.y * b.y }; | ||
} | ||
|
||
/** | ||
* Scales a vector by a scalar value | ||
* @param {Point} v - The vector to scale | ||
* @param {number} scaleFactor - The scaling factor | ||
* @returns {Point} The scaled vector | ||
*/ | ||
static scale(v: Point, scaleFactor: number): Point { | ||
return { x: v.x * scaleFactor, y: v.y * scaleFactor }; | ||
} | ||
|
||
/** | ||
* Calculates the magnitude (length) of a vector | ||
* @param {Point} v - The vector | ||
* @returns {number} The magnitude of the vector | ||
*/ | ||
static mag(v: Point): number { | ||
return Math.hypot(v.x, v.y); | ||
} | ||
|
||
/** | ||
* Returns a normalized (unit) vector in the same direction | ||
* @param {Point} v - The vector to normalize | ||
* @returns {Point} The normalized vector | ||
*/ | ||
static normalized(v: Point): Point { | ||
const magnitude = Vector.mag(v); | ||
return magnitude === 0 ? Vector.zero() : { x: v.x / magnitude, y: v.y / magnitude }; | ||
} | ||
|
||
/** | ||
* Calculates the Euclidean distance between two points | ||
* @param {Point} a - The first point | ||
* @param {Point} b - The second point | ||
* @returns {number} The distance between the points | ||
*/ | ||
static distance(a: Point, b: Point): number { | ||
return Math.hypot(a.x - b.x, a.y - b.y); | ||
} | ||
|
||
/** | ||
* Calculates the squared distance between two points | ||
* Useful for performance when comparing distances | ||
* @param {Point} a - The first point | ||
* @param {Point} b - The second point | ||
* @returns {number} The squared distance between the points | ||
*/ | ||
static distanceSquared(a: Point, b: Point): number { | ||
const dx = a.x - b.x; | ||
const dy = a.y - b.y; | ||
return dx * dx + dy * dy; | ||
} | ||
|
||
/** | ||
* Linearly interpolates between two points | ||
* @param {Point} a - The starting point | ||
* @param {Point} b - The ending point | ||
* @param {number} t - The interpolation parameter (0-1) | ||
* @returns {Point} The interpolated point | ||
*/ | ||
static lerp(a: Point, b: Point, t: number): Point { | ||
return { | ||
x: a.x + (b.x - a.x) * t, | ||
y: a.y + (b.y - a.y) * t, | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Point = { x: number; y: number }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.