UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

29 lines (23 loc) 768 B
import { clamp } from "../../math/clamp.js"; import { v2_dot } from "./v2_dot.js"; import { v2_length } from "./v2_length.js"; /** * * @param {number} x0 first vector X * @param {number} y0 first vector Y * @param {number} x1 second vector X * @param {number} y1 second vector Y * @returns {number} in radians */ export function v2_angle_between(x0, y0, x1, y1) { const d = v2_dot(x0, y0, x1, y1); const magnitude_0 = v2_length(x0, y0); const magnitude_1 = v2_length(x1, y1); const l = magnitude_0 * magnitude_1; if (l === 0) { // collective magnitude is 0, provide arbitrary result to avoid division by 0 return 0; } const theta = clamp(d / l, -1, 1); return Math.acos(theta); }