From 030f522d58cb2071d26ccb398579386c9678b80b Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Thu, 20 Apr 2023 21:08:01 -0500 Subject: [PATCH] Add '3D' --- 3D | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 3D diff --git a/3D b/3D new file mode 100644 index 0000000..29dd238 --- /dev/null +++ b/3D @@ -0,0 +1,71 @@ +```function triangleClipAgainstPlane(planeP, planeN, inTri) { + var outTri1 = Tri(), outTri2 = Tri(); + + planeN = normVector(planeN); + + var d = function(p) { + p = normVector(p); + return(dotVector(planeN, p) - dotVector(planeN, planeP)); + }; + + var inside_points = Tri(), + nInsidePointCount = 0; + var outside_points = Tri(), + nOutsidePointCount = 0; + + var d0 = d(inTri[0]); + var d1 = d(inTri[1]); + var d2 = d(inTri[2]); + + if (d0 >= 0) { + inside_points[nInsidePointCount] = inTri[0]; + nInsidePointCount++; + } else { + outside_points[nOutsidePointCount] = inTri[0]; + nOutsidePointCount++; + } + if (d1 >= 0) { + inside_points[nInsidePointCount] = inTri[1]; + nInsidePointCount++; + } else { + outside_points[nOutsidePointCount] = inTri[1]; + nOutsidePointCount++; + } + if (d2 >= 0) { + inside_points[nInsidePointCount] = inTri[2]; + nInsidePointCount++; + } else { + outside_points[nOutsidePointCount] = inTri[2]; + nOutsidePointCount++; + } + if (nInsidePointCount === 0) { + return [0]; + } + + if (nInsidePointCount === 3) { + outTri1 = inTri; + + return [1, [[outTri1[0], outTri1[1], outTri1[2]]]]; + } + + if (nInsidePointCount === 1 && nOutsidePointCount === 2) { + outTri1[0] = inside_points[0]; + + outTri1[1] = planeIntersectVector(planeP, planeN, inside_points[0], outside_points[0]); + outTri1[2] = planeIntersectVector(planeP, planeN, inside_points[0], outside_points[1]); + + return [1, [[outTri1[0], outTri1[1], outTri1[2]]]]; + } + + if (nInsidePointCount === 2 && nOutsidePointCount === 1) { + outTri1[0] = inside_points[0]; + outTri1[1] = inside_points[1]; + outTri1[2] = planeIntersectVector(planeP, planeN, inside_points[0], outside_points[0]); + + outTri2[0] = inside_points[1]; + outTri2[1] = outTri1[2]; + outTri2[2] = planeIntersectVector(planeP, planeN, inside_points[1], outside_points[0]); + + return [2, [[outTri1[0], outTri1[1], outTri1[2]], [outTri2[0], outTri2[1], outTri2[2]]]]; + } +};``` \ No newline at end of file