@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.
14 lines (13 loc) • 743 B
JavaScript
import { magnitude3D } from "./magnitude-3d.js";
/**
* Projects one 3D vector onto the second.
* @param {ArrayVector3D} xyz1 - Vector to project
* @param {ArrayVector3D} xyz2 - Vector to project onto
* @param {number} [m1] - Optional magnitude of the vector to project (default: `magnitude3D(xyz1)`)
* @param {number} [m2] - Optional magnitude of the vector to project onto (default: `magnitude3D(xyz2)`)
* @returns {ArrayVector3D} The projected vector
*/
export const project3D = (xyz1, xyz2, m1 = magnitude3D(xyz1), m2 = magnitude3D(xyz2)) => {
const f = m1 * Math.cos(Math.acos((xyz1[0] * xyz2[0] + xyz1[1] * xyz2[1] + xyz1[2] * xyz2[2]) / (m1 * m2)));
return [(xyz2[0] / m2) * f, (xyz2[1] / m2) * f, (xyz2[2] / m2) * f];
};