UNPKG

ts-useful

Version:

Functions for animation, color transitions, ecliptic, bezier, decasteljau, curves, three dimensional curves, smooth scrolling, random range, randomItem, mobius index, vectors, physics vectors, and easing.

311 lines 8.08 kB
import { Ecliptic } from '../ecliptic'; export class Coordinate { /** * @returns coordinate { x: 0, y: 0 } */ static get zero() { return { x: 0, y: 0 }; } /** * * @param coordinate coordinate | undefined */ constructor(coordinate) { /** * * @param coordinate coordinate * @returns this */ this.set = (coordinate) => { [this.x, this.y] = [coordinate.x, coordinate.y]; return this; }; /** * * @param coordinate coordinate * @returns this */ this.add = (coordinate) => { this.set(Coordinate.add(this, coordinate)); return this; }; /** * * @param amount number * @returns this */ this.addAcross = (amount) => { this.set(Coordinate.addAcross(this, amount)); return this; }; /** * * @param coordinate coordinate * @returns this */ this.subtract = (coordinate) => { this.set(Coordinate.subtract(this, coordinate)); return this; }; /** * * @param amount number * @returns this */ this.subtractAcross = (amount) => { this.set(Coordinate.subtractAcross(this, amount)); return this; }; /** * * @param coordinate coordinate * @returns this */ this.multiply = (coordinate) => { this.set(Coordinate.multiply(this, coordinate)); return this; }; /** * * @param amount number * @returns this */ this.multiplyAcross = (amount) => { this.set(Coordinate.multiplyAcross(this, amount)); return this; }; /** * * @param coordinate coordinate * @returns this */ this.divide = (coordinate) => { this.set(Coordinate.divide(this, coordinate)); return this; }; /** * * @param amount number * @returns this */ this.divideAcross = (amount) => { this.set(Coordinate.divideAcross(this, amount)); return this; }; /** * * @param target coordinate * @returns number */ this.radians = (target) => { return Coordinate.radian(this, target); }; /** * * @param radian * @returns (target: coordinate) => number */ this.offsetRadians = (radian) => (target) => { return Coordinate.offsetRadians(radian)(this, target); }; /** * * @param target coordinate * @returns number */ this.degree = (target) => { return Coordinate.degree(this, target); }; /** * * @param degree * @returns (target: coordinate) => number */ this.offsetDegrees = (degree) => (target) => { return Coordinate.offsetDegrees(degree)(this, target); }; /** * * @param coordinate coordinate * @returns boolean */ this.isEqual = (coordinate) => { return Coordinate.isEqual(this, coordinate); }; /** * * @param coordinate coordinate * @returns number */ this.compare = (coordinate) => { return Coordinate.compare(this, coordinate); }; /** * * @param coordinate * @returns number */ this.distance = (coordinate) => { return Coordinate.distance(this, coordinate); }; [this.x, this.y] = [coordinate?.x ?? 0, coordinate?.y ?? 0]; } } /** * * @param primary coordinate * @param secondary coordinate * @returns coordinate */ Coordinate.add = (primary, secondary) => { return { x: primary.x + secondary.x, y: primary.y + secondary.y }; }; /** * * @param coordinate coordinate * @param amount number * @returns coordinate */ Coordinate.addAcross = (coordinate, amount) => { return { x: coordinate.x + amount, y: coordinate.y + amount }; }; /** * * @param primary coordinate * @param secondary coordinate * @returns coordinate */ Coordinate.subtract = (primary, secondary) => { return { x: primary.x - secondary.x, y: primary.y - secondary.y }; }; /** * * @param coordinate coordinate * @param amount number * @returns coordinate */ Coordinate.subtractAcross = (coordinate, amount) => { return { x: coordinate.x - amount, y: coordinate.y - amount }; }; /** * * @param primary coordinate * @param secondary coordinate * @returns coordinate */ Coordinate.multiply = (primary, secondary) => { return { x: primary.x * secondary.x, y: primary.y * secondary.y }; }; /** * * @param coordinate coordinate * @param amount number * @returns coordinate */ Coordinate.multiplyAcross = (coordinate, amount) => { return { x: coordinate.x * amount, y: coordinate.y * amount }; }; /** * * @param primary coordinate * @param secondary coordinate * @returns coordinate */ Coordinate.divide = (primary, secondary) => { if (secondary.x === 0 || secondary.y === 0) { throw new Error(`Divide by zero in secondary - x: ${secondary.x} y: ${secondary.y}`); } return { x: primary.x / secondary.x, y: primary.y / secondary.y }; }; /** * * @param coordinate coordinate * @param amount number * @returns coordinate */ Coordinate.divideAcross = (coordinate, amount) => { if (amount === 0) { throw new Error('Cannot divide by zero'); } return { x: coordinate.x / amount, y: coordinate.y / amount }; }; /** * * @param primary coordinate * @param secondary coordinate * @returns boolean */ Coordinate.isEqual = (primary, secondary) => { return primary.x === secondary.x && primary.y === secondary.y; }; /** * * @param primary coordinate * @param secondary coordinate * @returns coordinate { x: -1 | 0 | 1, y: -1 | 0 | 1 } */ Coordinate.compare = (primary, secondary) => { const s = (a, b) => a < b ? -1 : a > b ? 1 : 0; return { x: s(primary.x, secondary.x), y: s(primary.y, secondary.y) }; }; /** * * @param primary coordinate * @param target coordinate * @returns number */ Coordinate.radian = (primary, target) => { return Ecliptic.Radian(primary, target); }; /** * * @param radian * @returns (origin: coordinate, target: coordinate) => number */ Coordinate.offsetRadians = (radian) => { const fn = (origin, target) => { return radian + Ecliptic.Radian(origin, target); }; return fn; }; /** * * @param primary coordinate * @param target coordinate * @returns number */ Coordinate.degree = (primary, target) => { return Ecliptic.Degree(primary, target); }; /** * * @param radian * @returns (origin: coordinate, target: coordinate) => number */ Coordinate.offsetDegrees = (degree) => { const fn = (origin, target) => { return degree + Ecliptic.Degree(origin, target); }; return fn; }; /** * * @param primary coordinate * @param secondary coordinate * @returns coordinate */ Coordinate.set = (primary, secondary) => { [primary.x, primary.y] = [secondary.x, secondary.y]; return primary; }; /** * * @param primary * @param secondary * @returns number */ Coordinate.distance = (primary, secondary) => { const sub = Coordinate.subtract(primary, secondary); return Math.sqrt(Math.pow(sub.x, 2) + Math.pow(sub.y, 2)); }; //# sourceMappingURL=coordinate.js.map