UNPKG

@mousepox/math

Version:

Math-related objects and utilities

52 lines (51 loc) 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapRadians = exports.lerp = exports.sign = exports.lerpAngle = exports.manhattanDistance = exports.distance = exports.clamp = exports.approximately = exports.Tau = exports.GoldenRatio = exports.DirectionMask = void 0; var DirectionMask; (function (DirectionMask) { DirectionMask[DirectionMask["None"] = 0] = "None"; DirectionMask[DirectionMask["North"] = 1] = "North"; DirectionMask[DirectionMask["East"] = 2] = "East"; DirectionMask[DirectionMask["South"] = 4] = "South"; DirectionMask[DirectionMask["West"] = 8] = "West"; DirectionMask[DirectionMask["NorthEast"] = 16] = "NorthEast"; DirectionMask[DirectionMask["SouthEast"] = 32] = "SouthEast"; DirectionMask[DirectionMask["SouthWest"] = 64] = "SouthWest"; DirectionMask[DirectionMask["NorthWest"] = 128] = "NorthWest"; })(DirectionMask = exports.DirectionMask || (exports.DirectionMask = {})); exports.GoldenRatio = (Math.sqrt(5) + 1) / 2; exports.Tau = Math.PI * 2; function approximately(a, b, threshold = Number.EPSILON) { return Math.abs(a - b) < threshold; } exports.approximately = approximately; function clamp(value, min, max) { return Math.min(Math.max(value, min), max); } exports.clamp = clamp; function distance(x1, y1, x2, y2) { return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2)); } exports.distance = distance; function manhattanDistance(x1, y1, x2, y2) { return Math.abs(x1 - x2) + Math.abs(y1 - y2); } exports.manhattanDistance = manhattanDistance; function lerpAngle(a, b, t) { const da = (b - a) % exports.Tau; const n = ((2 * da) % exports.Tau) - da; return a + n * t; } exports.lerpAngle = lerpAngle; function sign(n) { return n > 0 ? 1 : n === 0 ? 0 : -1; } exports.sign = sign; function lerp(a, b, t) { return a + (b - a) * t; } exports.lerp = lerp; function wrapRadians(radians) { return ((radians % exports.Tau) + exports.Tau) % exports.Tau; } exports.wrapRadians = wrapRadians;