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.

114 lines 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vect = void 0; const coordinate_1 = require("./coordinate"); class Vect extends coordinate_1.Coordinate { /** * * @param vector IVector * @returns number */ static hypotenuse(vector) { return Vect.hypot(vector.x, vector.y); } /** * * @param target Type of IVector */ static normalize(target) { const m = target.magnitude; if (m > 0) target.divideAcross(m); } /** * * @param vector IVector * @returns number */ static magnitude(vector) { return Math.sqrt(Vect.distanceSquare(vector)); } /** * @returns number */ get abs() { return { x: Math.abs(this.x), y: Math.abs(this.y) }; } /** * @returns number */ get hypotenuse() { return Vect.hypotenuse(this); } /** * @returns number */ get magnitude() { return Vect.magnitude(this); } /** * * @param coordinate coordinate optional */ constructor(coordinate) { super(coordinate); /** * * @param vector IVector * @param force number * @returns this */ this.addForce = (vector, force) => { return Vect.addForce(this, vector, force); }; /** * * @param vector IVector * @param force number * @returns this */ this.subForce = (vector, force) => { return Vect.subForce(this, vector, force); }; /** * Normalize the vector */ this.normalize = () => { Vect.normalize(this); }; } } exports.Vect = Vect; Vect.distanceSquare = (p) => Math.pow(p.x, 2) + Math.pow(p.y, 2); // double-dog-leg hypothenuse approximation // http://forums.parallax.com/discussion/147522/dog-leg-hypotenuse-approximation Vect.hypot = (a, b) => { [a, b] = [Math.abs(a), Math.abs(b)]; const [n, x] = [Math.min(a, b), Math.max(a, b)]; return x + 3 * n / 32 + Math.max(0, 2 * n - x) / 8 + Math.max(0, 4 * n - x) / 16; }; /** * * @param main IVector * @param target IVector * @param force number * @returns IVector */ Vect.addForce = (main, target, force) => { const vh = target.hypotenuse; main.add((target.divideAcross(vh).multiplyAcross(force))); return main; }; /** * * @param main IVector * @param target IVector * @param force number * @returns IVector */ Vect.subForce = (main, target, force) => { const vh = target.hypotenuse; main.subtract((target.divideAcross(vh).multiplyAcross(force))); return main; }; //# sourceMappingURL=vect.js.map