const degToRad = Math.PI / 180;

function crossProduct(u, v) {
    return [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]];
}

function normal(A, B, C){
    let u = [B[0] - A[0], B[1] - A[1], B[2] - A[2]];
    let v = [C[0] - A[0], C[1] - A[1], C[2] - A[2]];
    return crossProduct(u, v);
}

function planePassingByPoint(A, B, C, P){
    let [a, b, c] = normal(A, B, C);
    let d = -(a * P[0] + b * P[1] + c * P[2]);
    return [a, b, c, d];
}

function plane(A, B, C){
    let [a, b, c] = normal(A, B, C);
    let d = -(a * A[0] + b * A[1] + c * A[2]);
    return [a, b, c, d];
}

function intersectPlaneLine(Pl, pt, theta, phi){
    let [a, b, c, d] = Pl;

    let t = -(a * pt[0] + b * pt[1] + c * pt[2] + d) 
        / (a * Math.sin(phi * degToRad) + b * Math.cos(phi * degToRad) + c * Math.cos(theta * degToRad) / Math.sin(theta * degToRad))
    let x = pt[0] + t * Math.sin(phi * degToRad);
    let y = pt[1] + t * Math.cos(phi * degToRad);
    let z = pt[2] + t * Math.cos(theta * degToRad) / Math.sin(theta * degToRad);
    return [x, y, z];
}

function distToPlane(pt, A, B, C){
    let [a, b, c, d] = plane(A, B, C);
    let dis = (a * pt[0] + b * pt[1] + c * pt[2] + d) / norm([a, b, c])
    return dis;
}

function intersect(A, B, C, P, pt, theta, phi){
    let Pl = planePassingByPoint(A, B, C, P);
    let inter = intersectPlaneLine(Pl, pt, theta, phi);
    return inter;
}

function norm(u){
    return Math.sqrt(u[0]**2+u[1]**2+u[2]**2)
}

function area(A, B, C){
    let u = [B[0] - A[0], B[1] - A[1], B[2] - A[2]];
    let v = [C[0] - A[0], C[1] - A[1], C[2] - A[2]];
    return 0.5 * norm(crossProduct(u, v));
}

function isInside(A, B, C, pt){
    S = area(A, B, C);
    let barys = [area(pt, B, C) / S, area(pt, A, C) / S, area(pt, A, B) / S]
    return barys.every(x => 0 <= x && x <= 1);
}

function isHiddenByTriangle(A, B, C, P, theta, phi){
    let A1 = intersect(A, B, C, P, A, theta, phi);
    let B1 = intersect(A, B, C, P, B, theta, phi);
    let C1 = intersect(A, B, C, P, C, theta, phi);
    // console.log("A1 :", A1);
    // console.log("B1 :", B1);
    // console.log("C1 :", C1);
    // console.log("Dist :", distToPlane(P, A1, B1, C1));
    return isInside(A1, B1, C1, P);
}
let theta = 45;
let phi = 45;
let A = [3, -1, 5];
let B = [-2, 3, 3];
let C = [-1, -4, 6];
let P = [-2.5, -0.5, 1];
console.log(area(A, B, C));
let int = intersect(A, B, C, P, B, theta, phi);
console.log(int)
console.log(isHiddenByTriangle(A, B, C, P, theta, phi))

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: