Selaa lähdekoodia

intersection-circumference

Victor Domingues 5 vuotta sitten
vanhempi
commit
54e8bdae19

+ 62 - 29
src/app/components/circumference-component/models/circumference-model.js

@@ -1,4 +1,3 @@
-import { LineSegmentModel } from "../../line-segment-component/models/line-segment-model";
 import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
 import { GeometricObject } from "../../../core/models/objects/geometric-object";
 import { IntersectionModel } from "../../intersection-component/models/intersection-model";
@@ -27,14 +26,10 @@ export class CircumferenceModel extends GeometricObject {
     }
 
     getStraight() {
-        const aX = this.center.posX;
-        const aY = this.center.posY;
-        const bX = this.radius.posX;
-        const bY = this.radius.posY;
-        const a = bY - aY;
-        const b = aX - bX;
-        const c = a * aX + b * aY;
-        return [a, b, c];
+        const dx = this.radius.posX - this.center.posX;
+        const dy = this.radius.posY - this.center.posY;
+        const c = Math.sqrt((dy * dy) + (dx * dx));
+        return [dx, dy, c];
     }
 
     getDirection() {
@@ -57,31 +52,69 @@ export class CircumferenceModel extends GeometricObject {
         }
     }
     getIntersectionByCircumference(circumference) {
-        const rM = new LineSegmentModel(this.center, this.radius);
-        const sM = new LineSegmentModel(circumference.center, circumference.radius);
-        const r = rM.getStraight();
-        const s = sM.getStraight();
-        const a1 = r[0];
-        const b1 = r[1];
-        const c1 = r[2];
-        const a2 = s[0];
-        const b2 = s[1];
-        const c2 = s[2];
-        const determinant = a1 * b2 - a2 * b1;
-        if (determinant == 0) {
-            return new IntersectionModel(
+
+        const c0 = this.getStraight();
+
+        if (c0[2] > (this.radius + circumference.radius)) {
+            return [new IntersectionModel(
                 Number.MAX_SAFE_INTEGER,
                 Number.MAX_SAFE_INTEGER,
                 undefined,
                 this,
-                circumference
-            );
-        } else {
-            const x = (b2 * c1 - b1 * c2) / determinant;
-            const y = (a1 * c2 - a2 * c1) / determinant;
-            return new IntersectionModel(x, y, undefined, this, circumference);
+                circumference,
+                false
+            ), new IntersectionModel(
+                Number.MAX_SAFE_INTEGER,
+                Number.MAX_SAFE_INTEGER,
+                undefined,
+                this,
+                circumference,
+                false
+            )];
+        }
+        if (c0[2] < this.radius + circumference.radius) {
+            return [new IntersectionModel(
+                Number.MAX_SAFE_INTEGER,
+                Number.MAX_SAFE_INTEGER,
+                undefined,
+                this,
+                circumference,
+                false
+            ), new IntersectionModel(
+                Number.MAX_SAFE_INTEGER,
+                Number.MAX_SAFE_INTEGER,
+                undefined,
+                this,
+                circumference,
+                false
+            )];
         }
-        return
+
+        const distance = ((this.radius * this.radius) - (circumference.radius * circumference.radius) + (c0[2] * c0[2])) / (2.0 * c0[2]);
+        const aX = this.center.posX + (c0[0] * distance / c0[2]);
+        const aY = this.center.posY + (c0[1] * distance / c0[2]);
+        const distance2 = Math.sqrt((this.radius * this.radius) - (distance * distance));
+        const bX = -c0[0] * (distance2 / c0[2]);
+        const bY = c0[1] * (distance2 / c0[2]);
+        const intersectionAX = aX + bX;
+        const intersectionBX = aX - bX;
+        const intersectionAY = aY + bY;
+        const intersectionBY = aY - bY;
+        return [new IntersectionModel(
+            intersectionAX,
+            intersectionAY,
+            undefined,
+            this,
+            circumference,
+            false
+        ), new IntersectionModel(
+            intersectionBX,
+            intersectionBY,
+            undefined,
+            this,
+            circumference,
+            false
+        )];
     }
 
 

+ 16 - 13
src/app/components/intersection-component/drawers/intersection-drawer.js

@@ -61,21 +61,24 @@ export class IntersectionDrawer extends Drawer {
     return objects.getByKonvaObject(konvaObject)[0];
   }
   drawPoint() {
-    const intersectionPoint = this.aggregatorA.genericObject.getIntersection(
+    const intersectionPoints = this.aggregatorA.genericObject.getIntersection(
       this.aggregatorB.genericObject
     );
-    intersectionPoint.update();
-    const point = PointDrawer.drawPoint(intersectionPoint, false, false, true);
-    const aggregator = new DrawerAggregator(
-      this,
-      intersectionPoint,
-      point.konvaObject,
-      ELEMENTS_CLASS.INTERSECTION_POINT
-    );
-    this.point = point.geometricObject;
-    super.addAggregator(aggregator);
-    this.aggregatorB.addAggregator(aggregator);
-    this.aggregatorA.addAggregator(aggregator);
+    for (let index = 0; index < intersectionPoints.length; index++) {
+      const intersectionPoint = intersectionPoints[index];
+      intersectionPoint.update();
+      const point = PointDrawer.drawPoint(intersectionPoint, false, false, true);
+      const aggregator = new DrawerAggregator(
+        this,
+        intersectionPoint,
+        point.konvaObject,
+        ELEMENTS_CLASS.INTERSECTION_POINT
+      );
+      this.point = point.geometricObject;
+      super.addAggregator(aggregator);
+      this.aggregatorB.addAggregator(aggregator);
+      this.aggregatorA.addAggregator(aggregator);
+    }
   }
   isValidObject(konvaObject) {
     switch (konvaObject.attrs.class) {

+ 19 - 16
src/app/components/intersection-component/models/intersection-model.js

@@ -2,29 +2,32 @@ import { PointModel } from "../../point-component/models/point-model";
 import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
 
 export class IntersectionModel extends PointModel {
-  constructor(posX, posY, label, r, s) {
+  constructor(posX, posY, label, r, s, visible) {
     super(posX, posY, label);
     this.r = r;
     this.s = s;
     super.setClass(ELEMENTS_CLASS.INTERSECTION_POINT);
+    this.visible = visible;
   }
 
   update(aggregator, event) {
-    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;
-      this.visible = false;
-      return;
-    }
-
-    if (!this.s.insideSegment(this.posX, this.posY)) {
-      this.posX = Number.MAX_SAFE_INTEGER;
-      this.posY = Number.MAX_SAFE_INTEGER;
-      this.visible = false;
-      return;
+    const intersections = this.r.getIntersection(this.s);
+    for (let index = 0; index < intersections.length; index++) {
+      const intersection = intersections[index];
+      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;
+        this.visible = false;
+        return;
+      }
+      if (!this.s.insideSegment(this.posX, this.posY)) {
+        this.posX = Number.MAX_SAFE_INTEGER;
+        this.posY = Number.MAX_SAFE_INTEGER;
+        this.visible = false;
+        return;
+      }
     }
     this.visible = true;
   }

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

@@ -50,17 +50,17 @@ export class LineSegmentModel extends GeometricObject {
     const c2 = s[2];
     const determinant = a1 * b2 - a2 * b1;
     if (determinant == 0) {
-      return new IntersectionModel(
+      return [new IntersectionModel(
         Number.MAX_SAFE_INTEGER,
         Number.MAX_SAFE_INTEGER,
         undefined,
         this,
         lineSegment
-      );
+      )];
     } else {
       const x = (b2 * c1 - b1 * c2) / determinant;
       const y = (a1 * c2 - a2 * c1) / determinant;
-      return new IntersectionModel(x, y, undefined, this, lineSegment);
+      return [new IntersectionModel(x, y, undefined, this, lineSegment)];
     }
   }
 
@@ -75,17 +75,17 @@ export class LineSegmentModel extends GeometricObject {
     const c2 = s[2];
     const determinant = a1 * b2 - a2 * b1;
     if (determinant == 0) {
-      return new IntersectionModel(
+      return [new IntersectionModel(
         Number.MAX_SAFE_INTEGER,
         Number.MAX_SAFE_INTEGER,
         undefined,
         this,
         circumference
-      );
+      )];
     } else {
       const x = (b2 * c1 - b1 * c2) / determinant;
       const y = (a1 * c2 - a2 * c1) / determinant;
-      return new IntersectionModel(x, y, undefined, this, circumference);
+      return [new IntersectionModel(x, y, undefined, this, circumference)];
     }
   }