UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

42 lines (34 loc) 1.29 kB
import { v3_distance_sqr } from "../../vec3/v3_distance_sqr.js"; import { clamp01 } from "../../../math/clamp01.js"; /** * Computes shortest distance between a line segment(defined by 2 points) and a point * @param {number} x0 * @param {number} y0 * @param {number} z0 * @param {number} x1 * @param {number} y1 * @param {number} z1 * @param {number} px * @param {number} py * @param {number} pz */ export function line3_computeSegmentPointDistance_sqr(x0, y0, z0, x1, y1, z1, px, py, pz) { // get line delta const line_delta_x = x1 - x0; const line_delta_y = y1 - y0; const line_delta_z = z1 - z0; // find closest point const sp0_x = px - x0; const sp0_y = py - y0; const sp0_z = pz - z0; // const d2 = line_delta_x * line_delta_x + line_delta_y * line_delta_y + line_delta_z * line_delta_z; const d3 = line_delta_x * sp0_x + line_delta_y * sp0_y + line_delta_z * sp0_z; const t = clamp01(d3 / d2); // compute point on the line const lp_x = line_delta_x * t + x0; const lp_y = line_delta_y * t + y0; const lp_z = line_delta_z * t + z0; // compute distance from the point in question to the point on the line return v3_distance_sqr(px, py, pz, lp_x, lp_y, lp_z); }