From 0202ad67ae352b970ebb9c8ee3b04b7f3bebf0e9 Mon Sep 17 00:00:00 2001
From: Zachary Zhang <zacharyzhang7@gmail.com>
Date: Wed, 31 Jul 2024 14:10:39 -0400
Subject: working scribble erase

---
 src/client/views/GestureOverlay.tsx | 71 +++++++++++++++++++++++++++++++------
 1 file changed, 60 insertions(+), 11 deletions(-)

(limited to 'src/client/views/GestureOverlay.tsx')

diff --git a/src/client/views/GestureOverlay.tsx b/src/client/views/GestureOverlay.tsx
index d439c3f48..6f01d3f3e 100644
--- a/src/client/views/GestureOverlay.tsx
+++ b/src/client/views/GestureOverlay.tsx
@@ -133,30 +133,38 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
         }
     }
     isScribble(inkPoints: any) {
-        console.log(inkPoints);
         const ffView = DocumentView.allViews().find(view => view.ComponentView instanceof CollectionFreeFormView);
         const points = this._points.map(p => ({ X: p.X, Y: p.Y }));
-        const cuspArray = this.getNumberOfCusps(inkPoints);
+        console.log(points);
+        const cuspArray = this.getNumberOfCusps(points);
+        console.log(cuspArray);
         let cuspBooleanArray: boolean[] = [];
         let docsToDelete: Doc[] = [];
         const childDocs = (ffView?.ComponentView as CollectionFreeFormView).childDocs;
         childDocs.map(doc => DocumentView.getDocumentView(doc, DocumentView.getDocumentView(doc))).filter(inkView => inkView?.ComponentView instanceof InkingStroke);
-        for (let doc of childDocs) {
-            console.log((DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData.map(point => ({ X: point.X, Y: point.Y })));
-        }
         if ((ffView?.ComponentView as CollectionFreeFormView).childDocs) {
             if (cuspArray.length > 4) {
+                console.log('there are enough cusps');
                 for (let i = 0; i < cuspArray.length - 2; i++) {
                     outerLoop: for (let doc of childDocs) {
                         const point1 = cuspArray[i];
                         const point2 = cuspArray[i + 1];
                         const point3 = cuspArray[i + 2];
                         const triangleObject = { p1: { X: point1.X, Y: point1.Y }, p2: { X: point2.X, Y: point2.Y }, p3: { X: point3.X, Y: point3.Y } };
-                        //console.log(triangleObject + '     ' + (DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData);
-                        if (this.isAnyPointInTriangle(triangleObject, (DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData)) {
-                            docsToDelete.push(doc);
-                            cuspBooleanArray.push(true);
-                            break outerLoop;
+                        console.log((DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData.map(point => ({ X: point.X, Y: point.Y })));
+                        console.log(doc.title);
+                        if (doc.title === 'line') {
+                            if (this.doesLineIntersectTriangle((DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData, triangleObject)) {
+                                docsToDelete.push(doc);
+                                cuspBooleanArray.push(true);
+                                break outerLoop;
+                            }
+                        } else {
+                            if (this.isAnyPointInTriangle(triangleObject, (DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData)) {
+                                docsToDelete.push(doc);
+                                cuspBooleanArray.push(true);
+                                break outerLoop;
+                            }
                         }
                     }
                     cuspBooleanArray.push(false);
@@ -170,6 +178,9 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
         }
     }
     determineIfScribble(cuspBooleanArray: boolean[]) {
+        if (!cuspBooleanArray) {
+            return false;
+        }
         const quarterArrayLength = Math.ceil((cuspBooleanArray.length - 2) * 0.25);
         let hasObjectInFirstAndLast25 = true;
         for (let i = 0; i < quarterArrayLength; i++) {
@@ -180,6 +191,9 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
         }
         const trueCount = cuspBooleanArray.filter(value => value).length;
         const percentageTrues = trueCount / cuspBooleanArray.length;
+        if (percentageTrues > 0.75 || hasObjectInFirstAndLast25) {
+            console.log('requirements are met');
+        }
         return percentageTrues > 0.75 || hasObjectInFirstAndLast25;
     }
     isRectangleOverlap(rect1: any, rect2: any): boolean {
@@ -203,12 +217,47 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
             //console.log(point.X + '     ' + point.Y);
             //console.log(triangle);
             if (this.isPointInTriangle(point, triangle)) {
-                console.log('im true');
                 return true;
             }
         }
         return false;
     }
+    doesLineIntersectTriangle(line: any, triangle: any): boolean {
+        const edges = [
+            { start: triangle.p1, end: triangle.p2 },
+            { start: triangle.p2, end: triangle.p3 },
+            { start: triangle.p3, end: triangle.p1 },
+        ];
+
+        for (const edge of edges) {
+            if (this.doLinesIntersect(line, edge)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+    doLinesIntersect(line1: any, line2: any): boolean {
+        const A = line1[0];
+        const B = line1[line1.length - 1];
+        const { start: C, end: D } = line2;
+
+        console.log(A);
+        console.log(B);
+        console.log(C);
+        console.log(D);
+        console.log(line1);
+        const denominator = (B.X - A.X) * (D.Y - C.Y) - (B.Y - A.Y) * (D.X - C.X);
+        if (denominator === 0) return false;
+
+        const numerator1 = (A.Y - C.Y) * (D.X - C.X) - (A.X - C.X) * (D.Y - C.Y);
+        const numerator2 = (A.Y - C.Y) * (B.X - A.X) - (A.X - C.X) * (B.Y - A.Y);
+
+        const r = numerator1 / denominator;
+        const s = numerator2 / denominator;
+
+        return r >= 0 && r <= 1 && s >= 0 && s <= 1;
+    }
     @action
     onPointerUp = () => {
         console.log('pointer up');
-- 
cgit v1.2.3-70-g09d2

