UNPKG

@fimbul-works/vec

Version:

A comprehensive TypeScript vector math library providing 2D, 3D, and 4D vector operations with a focus on performance and type safety.

11 lines (10 loc) 565 B
import { magnitude2D } from "./magnitude-2d.js"; /** * Clamps the magnitude of a 2D vector between a minimum and maximum value. * @param {ArrayVector2D} xy - Vector as `[x, y]` * @param {number} min - Minimum magnitude * @param {number} max - Maximum magnitude * @param {number} [m] - Optional current magnitude (default: `magnitude2D(xy)`) * @returns {ArrayVector2D} The clamped vector */ export const clamp2D = (xy, min, max, m = magnitude2D(xy)) => m > max ? [(xy[0] / m) * max, (xy[1] / m) * max] : m < min ? [(xy[0] / m) * min, (xy[1] / m) * min] : xy;