From 289ac9dd905b2f2672ed3eac94aebac53333a9ca Mon Sep 17 00:00:00 2001
From: Yuriy Maksymets <iurii.maksymets@gmail.com>
Date: Fri, 15 Nov 2019 01:07:01 +0000
Subject: [PATCH] Recognition refactoing

---
 src/app.js  | 36 ++++++++++++++++++++++--------------
 src/room.js |  3 ++-
 2 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/src/app.js b/src/app.js
index a848c2b..2b3f9b3 100644
--- a/src/app.js
+++ b/src/app.js
@@ -145,7 +145,9 @@ const onRoomConnect = (room_) => {
   )
 }
 
-const mp = (x, y) => [x, y, 1, "#00000033"]
+function faintPoint(x, y) {
+  return [x, y, 1, "#00000033"]
+}
 
 function attributedPoint(x, y, pressure = 0) {
   return [
@@ -156,24 +158,28 @@ function attributedPoint(x, y, pressure = 0) {
   ]
 }
 
+const INF_LINE_OFFSET = 2000
+
 function getRecognizedShapePoints(points) {
   const recognizedShape = recognizeFromPoints(points)
-  if (recognizedShape.shape === Shapes.line) {
-    console.log(recognizedShape)
-    const [x, y] = points[0]
-    const a = (recognizedShape.angle * Math.PI) / 180
-    const p1 = [x - 2000 * Math.cos(a), y + 2000 * Math.sin(a)]
-    const p2 = [x + 2000 * Math.cos(a), y - 2000 * Math.sin(a)]
-    return [p1, p2]
-  } else if (recognizedShape.shape === Shapes.rectangle) {
-    console.log(recognizedShape)
-    return recognizedShape.boundingPoints
+  if (!recognizedShape.shape) return undefined
+  switch (recognizedShape.shape) {
+    case Shapes.line: {
+      const [x, y] = points[0]
+      const a = (recognizedShape.angle * Math.PI) / 180
+      const dx = INF_LINE_OFFSET * Math.cos(a)
+      const dy = INF_LINE_OFFSET * Math.sin(a)
+      const p1 = [x - dx, y + dy]
+      const p2 = [x + dx, y - dy]
+      return [p1, p2]
+    }
+    case Shapes.rectangle: {
+      return recognizedShape.boundingPoints
+    }
   }
   return undefined
 }
 
-const LAST_RECOGNIZED_PATH_ID = "LSP"
-
 function drawIfRecognized(points, callback, notRecCallback) {
   const recognizedPoints = getRecognizedShapePoints(points)
   if (recognizedPoints) {
@@ -183,6 +189,8 @@ function drawIfRecognized(points, callback, notRecCallback) {
   }
 }
 
+const LAST_RECOGNIZED_PATH_ID = "LSP"
+
 function clearRecognizedUpcoming() {
   canvas.renderPath(LAST_RECOGNIZED_PATH_ID, [])
 }
@@ -193,7 +201,7 @@ function drawRecognizedUpcoming(points) {
     (recognizedPoints) =>
       canvas.renderPath(
         LAST_RECOGNIZED_PATH_ID,
-        recognizedPoints.map((x) => mp(...x)),
+        recognizedPoints.map((x) => faintPoint(...x)),
       ),
     clearRecognizedUpcoming,
   )
diff --git a/src/room.js b/src/room.js
index 6edba8f..af3bfbf 100644
--- a/src/room.js
+++ b/src/room.js
@@ -54,7 +54,7 @@ class Room extends EventTarget {
         [flattenErasureIntervals({ [pointID]: newIntervals })],
       )[0]
       const postJSON = JSON.stringify(combinedIntervals)
-      console.log(postJSON)
+
       if (prevJSON == postJSON) {
         return
       }
@@ -63,6 +63,7 @@ class Room extends EventTarget {
     })
   }
 
+  // TODO: Refactor duplication
   replacePath(pathID, newPoints) {
     const self = this
 
-- 
GitLab