Skip to content

Commit

Permalink
docs: added some missing docs for vectors (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
amyspark-ng authored Dec 5, 2024
1 parent 7427904 commit 8e9bb06
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/math/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,17 @@ export class Vec2 {
return new Vec2(arr[0], arr[1]);
}

/** An empty vector. (0, 0) */
static ZERO = new Vec2(0, 0);
/** A vector with both components of 1. (1, 1) */
static ONE = new Vec2(1, 1);
/** A vector signaling to the left. (-1, 0) */
static LEFT = new Vec2(-1, 0);
/** A vector signaling to the right. (1, 0) */
static RIGHT = new Vec2(1, 0);
/** A vector signaling up. (0, -1) */
static UP = new Vec2(0, -1);
/** A vector signaling down. (0, 1) */
static DOWN = new Vec2(0, 1);

/** Closest orthogonal direction: LEFT, RIGHT, UP, or DOWN */
Expand Down Expand Up @@ -289,6 +295,11 @@ export class Vec2 {
return x * x + y * y;
}

/**
* Get length of the vector
*
* @since v3000.0
*/
len(): number {
return Math.sqrt(this.dot(this));
}
Expand Down Expand Up @@ -453,6 +464,11 @@ export class Vec2 {
return this.x * p2.x + this.y * p2.y;
}

/**
* Get the dot product between 2 vectors.
*
* @since v3000.0
*/
static dot(v: Vec2, other: Vec2): number {
return v.x * v.x + v.y * v.y;
}
Expand All @@ -466,6 +482,11 @@ export class Vec2 {
return this.x * p2.y - this.y * p2.x;
}

/**
* Get the cross product between 2 vectors.
*
* @since v3000.0
*/
static cross(v: Vec2, other: Vec2): number {
return v.x * other.y - v.y * other.x;
}
Expand Down Expand Up @@ -588,18 +609,30 @@ export class Vec2 {
return m.multVec2(this);
}

/**
* See if one vector is equal to another.
*
* @since v3000.0
*/
eq(other: Vec2): boolean {
return this.x === other.x && this.y === other.y;
}

/** Converts the vector to a {@link Rect `Rect()`} with the vector as the origin.
* @since v3000.0.
*/
bbox(): Rect {
return new Rect(this, 0, 0);
}

/** Converts the vector to a readable string. */
toString(): string {
return `vec2(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`;
}

/** Converts the vector to an array.
* @since v3001.0
*/
toArray(): Array<number> {
return [this.x, this.y];
}
Expand Down

0 comments on commit 8e9bb06

Please sign in to comment.