Browse Source

Update 'src/app/components/circumference-component/models/circumference-model.js'

Implementation of intersection with StraightLine.
Added to 'getIntersection(geometricObject)': case ELEMENTS_CLASS.LINE: return geometricObject.getIntersectionWithCircumference(this);
leo 4 years ago
parent
commit
39300b5260

+ 76 - 52
src/app/components/circumference-component/models/circumference-model.js

@@ -1,8 +1,19 @@
+/*
+ * iGeom by LInE
+ * Geometric Object: Circumference
+ * Model to Circumference
+ * www.matematica.br/igeom
+ * ./app/components/line-component/models/circumference-model.js
+ * @version 2020/11/02: Implemented Line instersection
+ */
+
 import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
 import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
 import { GeometricObject } from "../../../core/models/objects/geometric-object";
 import { GeometricObject } from "../../../core/models/objects/geometric-object";
 import { IntersectionModel } from "../../intersection-component/models/intersection-model";
 import { IntersectionModel } from "../../intersection-component/models/intersection-model";
+
 export class CircumferenceModel extends GeometricObject {
 export class CircumferenceModel extends GeometricObject {
-  constructor(center, radius, id) {
+
+  constructor (center, radius, id) {
     super(id);
     super(id);
     this.center = center;
     this.center = center;
     this.radius = radius;
     this.radius = radius;
@@ -15,26 +26,47 @@ export class CircumferenceModel extends GeometricObject {
     super.setClass(ELEMENTS_CLASS.CIRCUMFERENCE);
     super.setClass(ELEMENTS_CLASS.CIRCUMFERENCE);
     this.definitions.push(this.center);
     this.definitions.push(this.center);
     this.definitions.push(this.radius);
     this.definitions.push(this.radius);
-  }
-  getRadius() {
+    }
+
+  getCenter () {
+    return this.center;
+    }
+
+  getPoint () {
+    return this.radius;
+    }
+
+  getRadius () {
+    /* Deixar mais intuitiva...
     this._coordinates[0] = this.center.posX;
     this._coordinates[0] = this.center.posX;
     this._coordinates[1] = this.center.posY;
     this._coordinates[1] = this.center.posY;
     this._coordinates[2] = this.radius.posX;
     this._coordinates[2] = this.radius.posX;
     this._coordinates[3] = this.radius.posY;
     this._coordinates[3] = this.radius.posY;
     const legA = this._coordinates[2] - this._coordinates[0];
     const legA = this._coordinates[2] - this._coordinates[0];
     const legB = this._coordinates[3] - this._coordinates[1];
     const legB = this._coordinates[3] - this._coordinates[1];
-    const radius = Math.sqrt(Math.pow(legA, 2) + Math.pow(legB, 2));
+    const radius = Math.sqrt(Math.pow(legA, 2) + Math.pow(legB, 2)); // Desnecessario!
+    return radius;
+    */
+    var Cx, Cy, Ax, Ay;
+    Cx = this._coordinates[0] = this.center.posX;
+    Cy = this._coordinates[1] = this.center.posY;
+    Ax = this._coordinates[2] = this.radius.posX;
+    Ay = this._coordinates[3] = this.radius.posY;
+    //D console.log("circumference-model.js: circ((" + Cx +","+ Cy +"), ("+Ax+","+Ay+")");
+    const dx = Ax-Cx;
+    const dy = Ay-Cy;
+    const radius = Math.sqrt(dx*dx + dy*dy);
     return radius;
     return radius;
-  }
+    }
 
 
-  getStraight() {
+  getStraight () {
     const dx = this.radius.posX - this.center.posX;
     const dx = this.radius.posX - this.center.posX;
     const dy = this.radius.posY - this.center.posY;
     const dy = this.radius.posY - this.center.posY;
     const c = Math.sqrt(dy * dy + dx * dx);
     const c = Math.sqrt(dy * dy + dx * dx);
     return [dx, dy, c];
     return [dx, dy, c];
-  }
+    }
 
 
-  getDirection() {
+  getDirection () {
     const aX = this.center.posX;
     const aX = this.center.posX;
     const aY = this.center.posY;
     const aY = this.center.posY;
     const bX = this.radius.posX;
     const bX = this.radius.posX;
@@ -42,26 +74,30 @@ export class CircumferenceModel extends GeometricObject {
     const a = bX - aX;
     const a = bX - aX;
     const b = bY - aY;
     const b = bY - aY;
     return [a, b];
     return [a, b];
-  }
-  getIntersection(geometricObject) {
+    }
+
+  getIntersection (geometricObject) {
     switch (geometricObject.elementClass) {
     switch (geometricObject.elementClass) {
+      case ELEMENTS_CLASS.LINE: // StraightLine
+        return geometricObject.getIntersectionWithCircumference(this); // Delegate to StraightLine    
       case ELEMENTS_CLASS.LINE_SEGMENT:
       case ELEMENTS_CLASS.LINE_SEGMENT:
-        return this.getIntersectionByLine(geometricObject);
+        return this.getIntersectionByLine(geometricObject); //TODO melhor 'with' que 'by'
       case ELEMENTS_CLASS.CIRCUMFERENCE:
       case ELEMENTS_CLASS.CIRCUMFERENCE:
-        return this.getIntersectionsByCircumference(geometricObject);
+        return this.getIntersectionsByCircumference(geometricObject); //TODO melhor 'with' que 'by'
       default:
       default:
         break;
         break;
+      }
     }
     }
-  }
-  distance(center) {
+
+  distance (center) {
     const dx = center.posX - this.center.posX;
     const dx = center.posX - this.center.posX;
     const dy = center.posY - this.center.posY;
     const dy = center.posY - this.center.posY;
     const dist = Math.sqrt(dy * dy + dx * dx);
     const dist = Math.sqrt(dy * dy + dx * dx);
     return dist;
     return dist;
-  }
-  getIntersectionsByCircumference(circumference) {
-    // if (this.cente().igual(cd.C()))
-    // return null; // duas circ. com mesmo raio => devolva "null" (ambiguidade!)
+    }
+
+  getIntersectionsByCircumference (circumference) {
+    //TODO if (this.cente().igual(cd.C())) return null; // duas circ. com mesmo raio => devolva "null" (ambiguidade!)
     const r1 = this.getRadius(); // raio circ. atual
     const r1 = this.getRadius(); // raio circ. atual
     const r2 = circumference.getRadius(); // raio circ. "cd"
     const r2 = circumference.getRadius(); // raio circ. "cd"
     const d = this.distance(circumference); // distancia entre os raios das circ.
     const d = this.distance(circumference); // distancia entre os raios das circ.
@@ -95,18 +131,14 @@ export class CircumferenceModel extends GeometricObject {
       new IntersectionModel(x1, y1, undefined, this, circumference, true, 0),
       new IntersectionModel(x1, y1, undefined, this, circumference, true, 0),
       new IntersectionModel(x2, y2, undefined, this, circumference, true, 1)
       new IntersectionModel(x2, y2, undefined, this, circumference, true, 1)
     ];
     ];
-  }
+    }
 
 
-  /**
-   * Get Intersection Poin By Circumference and Line Segment
-   * @param {LineSegmentModel} lineSegment 
-   */
-  getIntersectionByLine(lineSegment) {
 
 
-    const pointA = lineSegment.pointA;
-    const pointB = lineSegment.pointB;
-    const center = this.center;
-    const radius = this.getRadius();
+  /// Get Intersection Poin By Circumference and Line Segment
+  /// @param {LineSegmentModel  } lineSegment 
+  getIntersectionByLine (lineSegment) {
+    const pointA = lineSegment.pointA, pointB = lineSegment.pointB; // sl = segment(A,B)
+    const center = this.center, radius = this.getRadius();          // c0 = circunference(C,A) = circ("center,radius")
 
 
     const dx = pointB.posX - pointA.posX;
     const dx = pointB.posX - pointA.posX;
     const dy = pointB.posY - pointA.posY;
     const dy = pointB.posY - pointA.posY;
@@ -126,11 +158,8 @@ export class CircumferenceModel extends GeometricObject {
     const PB = new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, lineSegment, this, false, 1);
     const PB = new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, lineSegment, this, false, 1);
 
 
     if (delta < 0) {
     if (delta < 0) {
-      return [
-        PA,
-        PB
-      ];
-    }
+      return [PA, PB];
+      }
 
 
     const deltaSqrt = Math.sqrt(delta);
     const deltaSqrt = Math.sqrt(delta);
     const root1 = -D + deltaSqrt;
     const root1 = -D + deltaSqrt;
@@ -140,11 +169,8 @@ export class CircumferenceModel extends GeometricObject {
 
 
     if (delta == 0) {
     if (delta == 0) {
       PA.bind(x1, y1, undefined, lineSegment, this, true, 0);
       PA.bind(x1, y1, undefined, lineSegment, this, true, 0);
-      return [
-        PA,
-        PB
-      ];
-    }
+      return [PA,PB];
+      }
 
 
     const x2 = pointA.posX - dx * root2;
     const x2 = pointA.posX - dx * root2;
     const y2 = pointA.posY - dy * root2;
     const y2 = pointA.posY - dy * root2;
@@ -158,16 +184,13 @@ export class CircumferenceModel extends GeometricObject {
 
 
       if (!this.insideSegment(PB.posX, PB.posY))
       if (!this.insideSegment(PB.posX, PB.posY))
         PB.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, lineSegment, this, false, 1)
         PB.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, lineSegment, this, false, 1)
-    }
+      }
 
 
-    return [
-      PA,
-      PB
-    ];
-
-  }
+    return [PA,PB];
+    }
 
 
-  insideSegment(intersecX, intersecY) {
+  // Verify intersection using "level set"
+  insideSegment (intersecX, intersecY) {
     const valuesR = this.getDirection();
     const valuesR = this.getDirection();
     const dirX = valuesR[0];
     const dirX = valuesR[0];
     const dirY = valuesR[1];
     const dirY = valuesR[1];
@@ -178,7 +201,7 @@ export class CircumferenceModel extends GeometricObject {
 
 
     if (cInterA < cRA) {
     if (cInterA < cRA) {
       return false;
       return false;
-    }
+      }
 
 
     // comparaca cv do ponto B < cv da intersec =>  vazio
     // comparaca cv do ponto B < cv da intersec =>  vazio
     const cRB = dirX * this.radius.posX + dirY * this.radius.posY;
     const cRB = dirX * this.radius.posX + dirY * this.radius.posY;
@@ -187,12 +210,12 @@ export class CircumferenceModel extends GeometricObject {
       this.posX = Number.MAX_SAFE_INTEGER;
       this.posX = Number.MAX_SAFE_INTEGER;
       this.posY = Number.MAX_SAFE_INTEGER;
       this.posY = Number.MAX_SAFE_INTEGER;
       return false;
       return false;
-    }
+      }
 
 
     return true;
     return true;
-  }
+    }
 
 
-  static do(map, list) {
+  static do (map, list) {
     const id = map.get("id");
     const id = map.get("id");
     const centerId = map.get("param")[0];
     const centerId = map.get("param")[0];
     const radiusId = map.get("param")[1];
     const radiusId = map.get("param")[1];
@@ -202,5 +225,6 @@ export class CircumferenceModel extends GeometricObject {
     const circumference = new CircumferenceModel(center, radius, id);
     const circumference = new CircumferenceModel(center, radius, id);
     circumference.setLabel(label);
     circumference.setLabel(label);
     return circumference;
     return circumference;
-  }
-}
+    }
+
+  }