Browse Source

fix bug in intersection

Victor Luiz Domingues 5 years ago
parent
commit
dcadbe812c

+ 11 - 0
src/app/components/intersection-component/models/intersection-model.js

@@ -10,5 +10,16 @@ export class IntersectionModel extends PointModel {
     const intersection = this.r.getIntersection(this.s);
     this.posX = parseFloat(intersection.posX.toFixed(2));
     this.posY = parseFloat(intersection.posY.toFixed(2));
+    if (!this.r.insideSegment(this.posX, this.posY)) {
+      this.posX = Number.MAX_SAFE_INTEGER;
+      this.posY = Number.MAX_SAFE_INTEGER;
+      return;
+    }
+
+    if (!this.s.insideSegment(this.posX, this.posY)) {
+      this.posX = Number.MAX_SAFE_INTEGER;
+      this.posY = Number.MAX_SAFE_INTEGER;
+      return;
+    }
   }
 }

+ 4 - 5
src/app/components/line-segment-component/drawers/line-segment-drawer.js

@@ -166,8 +166,8 @@ export class LineSegmentDrawer extends Drawer {
     LineSegmentDrawer.configureLineEvents(line);
     return line;
   }
-  static configureLineEvents(line) {
-    line.on("mouseover", function () {
+  static configureLineEvents(konvaObject) {
+    konvaObject.on("mouseover", function() {
       const selectedTool = App.getSelectedTool();
       if (
         selectedTool != undefined &&
@@ -179,13 +179,12 @@ export class LineSegmentDrawer extends Drawer {
         StageManager.getCurrentKonvaStage().draw();
       }
     });
-    line.on("mouseout", function () {
+    konvaObject.on("mouseout", function() {
       this.strokeWidth(STYLE.strokeWidth);
       this.stroke(STYLE.stroke);
       StageManager.getCurrentKonvaStage().draw();
     });
-
-    return line;
+    return konvaObject;
   }
 
   static drawKonvaLine(pointA, pointB) {

+ 34 - 0
src/app/components/line-segment-component/models/line-segment-model.js

@@ -17,6 +17,15 @@ export class LineSegmentModel extends GeometricObject {
     const c = a * aX + b * aY;
     return [a, b, c];
   }
+  getDirection() {
+    const aX = this.pointA.posX;
+    const aY = this.pointA.posY;
+    const bX = this.pointB.posX;
+    const bY = this.pointB.posY;
+    const a = bX - aX;
+    const b = bY - aY;
+    return [a, b];
+  }
   getIntersection(lineSegment) {
     const r = this.getStraight();
     const s = lineSegment.getStraight();
@@ -41,4 +50,29 @@ export class LineSegmentModel extends GeometricObject {
       return new IntersectionModel(x, y, undefined, this, lineSegment);
     }
   }
+
+  insideSegment(intersecX, intersecY) {
+    const valuesR = this.getDirection();
+    const dirX = valuesR[0];
+    const dirY = valuesR[1];
+    const cInterA = dirX * intersecX + dirY * intersecY;
+
+    // comparaca cv do ponto A > cv da intersec =>  vazio
+    const cRA = dirX * this.pointA.posX + dirY * this.pointA.posY;
+
+    if (cInterA < cRA) {
+      return false;
+    }
+
+    // comparaca cv do ponto B < cv da intersec =>  vazio
+    const cRB = dirX * this.pointB.posX + dirY * this.pointB.posY;
+
+    if (cInterA > cRB) {
+      this.posX = Number.MAX_SAFE_INTEGER;
+      this.posY = Number.MAX_SAFE_INTEGER;
+      return false;
+    }
+
+    return true;
+  }
 }