UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

48 lines (40 loc) 1.19 kB
import { v2_dot } from "../vec2/v2_dot.js"; /** * * @param {number[]|TypedArray|Float32Array} output * @param {number} output_offset * @param {number} ax * @param {number} ay * @param {number} bx * @param {number} by * @param {number} cx * @param {number} cy * @param {number} px reference point X * @param {number} py reference point Y */ export function triangle2d_get_barycentric( output, output_offset, ax, ay, bx, by, cx, cy, px, py ) { const v0_x = bx - ax; const v0_y = by - ay; const v1_x = cx - ax; const v1_y = cy - ay; const v2_x = px - ax; const v2_y = py - ay; const d00 = v2_dot(v0_x, v0_y, v0_x, v0_y); const d01 = v2_dot(v0_x, v0_y, v1_x, v1_y); const d11 = v2_dot(v1_x, v1_y, v1_x, v1_y); const d20 = v2_dot(v2_x, v2_y, v0_x, v0_y); const d21 = v2_dot(v2_x, v2_y, v1_x, v1_y); const denom = d00 * d11 - d01 * d01; const v = (d11 * d20 - d01 * d21) / denom; const w = (d00 * d21 - d01 * d20) / denom; const u = 1.0 - v - w; output[output_offset] = v; output[output_offset + 1] = w; output[output_offset + 2] = u; }