import { triangle2d_get_barycentric } from "./triangle2d_get_barycentric.js";

const scratch = new Float32Array(3);

/**
 *
 * @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
 * @returns {boolean}
 */
export function triangle2d_contains_point(
    ax, ay,
    bx, by,
    cx, cy,
    px, py
) {
    triangle2d_get_barycentric(
        scratch, 0,
        ax, ay,
        bx, by,
        cx, cy,
        px, py
    );

    const u = scratch[0];
    const v = scratch[1];
    const w = scratch[2];

    return u >= 0
        && v >= 0
        && w >= 0
        && Math.abs(1 - u - v - w) < 1e-7;
}