/**
 * NOTE: that direction must not be normalized for correct results and instead derived from line length
 * @param {number[]|Float32Array|Float64Array} out
 * @param {number} out_offset
 * @param {number} origin_a_x
 * @param {number} origin_a_y
 * @param {number} direction_a_x
 * @param {number} direction_a_y
 * @param {number} origin_b_x
 * @param {number} origin_b_y
 * @param {number} direction_b_x
 * @param {number} direction_b_y
 * @returns {boolean} false if lines are coplanar or intersect on the wrong side
 */
export function intersect_ray_2d(
    out, out_offset,
    origin_a_x, origin_a_y, direction_a_x, direction_a_y,
    origin_b_x, origin_b_y, direction_b_x, direction_b_y
) {

    const denominator = (direction_a_x * direction_b_y - direction_a_y * direction_b_x);

    if (Math.abs(denominator) < 1e-7){
        // Parallel lines
        return false;
    }

    const oab_y = origin_a_y - origin_b_y;
    const oab_x = origin_a_x - origin_b_x;

    const t = (direction_b_x * oab_y - direction_b_y * oab_x) / denominator;

    if (t < 0.5) // Intersects on the wrong side
        return false;

    const point_x = origin_a_x + t * direction_a_x;
    const point_y = origin_a_y + t * direction_a_y;


    // if (Number.isNaN(point_x)) {
    //     debugger;
    // }


    out[out_offset] = point_x;
    out[out_offset + 1] = point_y;

    return true;
}
