Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: added some missing docs for vectors #550

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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