advent22/ui/src/lib/vector2d.ts

28 lines
630 B
TypeScript
Raw Normal View History

2023-09-07 02:08:56 +00:00
export class Vector2D {
public readonly x: number;
public readonly y: number;
2023-09-07 03:29:53 +00:00
constructor();
constructor(x: number, y: number);
2023-09-07 02:08:56 +00:00
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
public plus(other: Vector2D): Vector2D {
return new Vector2D(this.x + other.x, this.y + other.y);
}
public minus(other: Vector2D): Vector2D {
return new Vector2D(this.x - other.x, this.y - other.y);
}
public scale(other: number): Vector2D {
return new Vector2D(this.x * other, this.y * other);
}
public equals(other: Vector2D): boolean {
return this.x === other.x && this.y === other.y;
}
}