From d724cc0bf6ff6d351319e6fb00f5184a04e16ac0 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Mon, 24 Nov 2025 15:46:22 +0200 Subject: chore: improve dev tasks --- packages/web/src/math/Coord.ts | 23 -------- packages/web/src/math/Ratio.test.ts | 27 ---------- packages/web/src/math/Ratio.ts | 105 ------------------------------------ packages/web/src/math/Rect.ts | 104 ----------------------------------- packages/web/src/math/index.ts | 3 -- 5 files changed, 262 deletions(-) delete mode 100644 packages/web/src/math/Coord.ts delete mode 100644 packages/web/src/math/Ratio.test.ts delete mode 100644 packages/web/src/math/Ratio.ts delete mode 100644 packages/web/src/math/Rect.ts delete mode 100644 packages/web/src/math/index.ts (limited to 'packages/web/src/math') diff --git a/packages/web/src/math/Coord.ts b/packages/web/src/math/Coord.ts deleted file mode 100644 index db7ee6d..0000000 --- a/packages/web/src/math/Coord.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** A coord on a grid whose origin is in the top left. */ -export default class Coord { - private readonly _x: number; - private readonly _y: number; - - constructor(x: number, y: number) { - this._x = x; - this._y = y; - } - - get x(): number { - return this._x; - } - - get y(): number { - return this._y; - } - - /** Get the squared distance of this point from the origin. */ - squaredDistanceFromOrigin(): number { - return this._x * this._x + this._y * this._y; - } -} diff --git a/packages/web/src/math/Ratio.test.ts b/packages/web/src/math/Ratio.test.ts deleted file mode 100644 index da6fef2..0000000 --- a/packages/web/src/math/Ratio.test.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { describe, expect, test } from "vitest"; -import Ratio from "./Ratio"; - -describe(Ratio, () => { - describe(Ratio.prototype.add, () => { - test("returns fractions in simplest form", () => { - const a = Ratio.fromInteger(0); - const b = new Ratio(1, 4); - - const c = a.add(b); - expect(c.numerator).toBe(1); - expect(c.denominator).toBe(4); - - const d = c.add(b); - expect(d.numerator).toBe(1); - expect(d.denominator).toBe(2); - - const e = d.add(b); - expect(e.numerator).toBe(3); - expect(e.denominator).toBe(4); - - const f = e.add(b); - expect(f.numerator).toBe(1); - expect(f.denominator).toBe(1); - }); - }); -}); diff --git a/packages/web/src/math/Ratio.ts b/packages/web/src/math/Ratio.ts deleted file mode 100644 index e2a1fbf..0000000 --- a/packages/web/src/math/Ratio.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { gcd } from "."; - -/** Serializable representation of a ratio. */ -export type RatioData = [numerator: number, denominator: number]; - -/** Representation of a ratio for performing fractional artithmetic. */ -export default class Ratio { - readonly #numerator: number; - readonly #denominator: number; - - get numerator(): number { - return this.#numerator; - } - - get denominator(): number { - return this.#denominator; - } - - constructor(numerator: number, denominator: number) { - if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) { - throw new TypeError( - `Ratio must have integer parts: ${numerator} / ${denominator}`, - ); - } - - if (denominator === 0) { - throw new RangeError("Ratio demnominator cannot be zero"); - } - - const divisor = gcd(numerator, denominator); - - this.#numerator = numerator / divisor; - this.#denominator = denominator / divisor; - } - - multiplyRatio(other: Ratio): Ratio { - return new Ratio( - this.numerator * other.numerator, - this.denominator * other.denominator, - ); - } - - divideRatio(other: Ratio): Ratio { - return new Ratio( - this.numerator * other.denominator, - this.denominator * other.numerator, - ); - } - - add(other: Ratio): Ratio { - return new Ratio( - this.numerator * other.denominator + other.numerator * this.denominator, - this.denominator * other.denominator, - ); - } - - subtract(other: Ratio): Ratio { - return new Ratio( - this.numerator * other.denominator - other.numerator * this.denominator, - this.denominator * other.denominator, - ); - } - - compare(other: Ratio): number { - const left = this.numerator * other.denominator; - const right = other.numerator * this.denominator; - return left < right ? -1 : left > right ? 1 : 0; - } - - equals(other: Ratio): boolean { - return this.compare(other) === 0; - } - - toNumber(): number { - return this.numerator / this.denominator; - } - - toString(): string { - return `${this.numerator}/${this.denominator}`; - } - - [Symbol.for("nodejs.util.inspect.custom")](): string { - return `Ratio { ${this.numerator}/${this.denominator} }`; - } - - static fromInteger(n: number): Ratio { - return new Ratio(n, 1); - } - - toData(): RatioData { - return [this.numerator, this.denominator]; - } - - static fromData(ratio: RatioData): Ratio { - return new Ratio(ratio[0], ratio[1]); - } - - static min(...ratios: Ratio[]): Ratio { - return ratios.reduce((a, b) => (a.compare(b) <= 0 ? a : b)); - } - - static max(...ratios: Ratio[]): Ratio { - return ratios.reduce((a, b) => (a.compare(b) >= 0 ? a : b)); - } -} diff --git a/packages/web/src/math/Rect.ts b/packages/web/src/math/Rect.ts deleted file mode 100644 index f52a2f7..0000000 --- a/packages/web/src/math/Rect.ts +++ /dev/null @@ -1,104 +0,0 @@ -import Coord from "./Coord"; - -/** A rectangle on a grid whose origin is in the top left. */ -export default class Rect { - private readonly _topLeft: Coord; - private readonly _width: number; - private readonly _height: number; - - constructor( - topLeftX: number, - topLeftY: number, - width: number, - height: number, - ) { - this._topLeft = new Coord(topLeftX, topLeftY); - this._width = width; - this._height = height; - } - - /** Width of this rectangle. */ - get width(): number { - return this._width; - } - - /** Height of this rectangle. */ - get height(): number { - return this._height; - } - - /** Coord of the top-left point of this rectangle. */ - get topLeft(): Coord { - return this._topLeft; - } - - /** Coord of the bottom-right point of this rectangle. */ - get bottomRight(): Coord { - return new Coord( - this._topLeft.x + this._width, - this._topLeft.y + this._height, - ); - } - - get center(): Coord { - return new Coord( - this.topLeft.x + (this.bottomRight.x - this.topLeft.x) / 2, - this.topLeft.y + (this.bottomRight.y - this.topLeft.y) / 2, - ); - } - - /** Determine if this rectangle contains the point at `coord`. */ - containsCoord(coord: Coord): boolean { - return ( - this.topLeft.x <= coord.x && - coord.x <= this.bottomRight.x && - this.topLeft.y <= coord.y && - coord.y <= this.bottomRight.y - ); - } - - verticallyContainsCoord(coord: Coord): boolean { - return this.topLeft.y <= coord.y && coord.y <= this.bottomRight.y; - } - - horizontallyContainsCoord(coord: Coord): boolean { - return this.topLeft.x <= coord.x && coord.x <= this.bottomRight.x; - } - - extend(other: Rect): Rect { - const topLeftX = Math.min( - this.topLeft.x, - this.bottomRight.x, - other.topLeft.x, - other.bottomRight.x, - ); - - const topLeftY = Math.min( - this.topLeft.y, - this.bottomRight.y, - other.topLeft.y, - other.bottomRight.y, - ); - - const bottomRightX = Math.max( - this.topLeft.x, - this.bottomRight.x, - other.topLeft.x, - other.bottomRight.x, - ); - - const bottomRightY = Math.max( - this.topLeft.y, - this.bottomRight.y, - other.topLeft.y, - other.bottomRight.y, - ); - - return new Rect( - topLeftX, - topLeftY, - bottomRightX - topLeftX, - bottomRightY - topLeftY, - ); - } -} diff --git a/packages/web/src/math/index.ts b/packages/web/src/math/index.ts deleted file mode 100644 index 70dbb67..0000000 --- a/packages/web/src/math/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function gcd(a: number, b: number): number { - return b === 0 ? Math.abs(a) : gcd(b, a % b); -} -- cgit v1.2.3