소스 검색

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

Improved indentation: space to differentiate function definition from its usage
To avoid reconstruction of existing point as intersection (e.g. Line.pointA == Circumference.radious):
- app/components/line-segment-component/models/line-segment-model.js
- app/components/intersection-component/drawers/intersection-drawer.js
- app/components/intersection-component/services/intersection-service.js
leo 2 년 전
부모
커밋
9d168cea30
1개의 변경된 파일58개의 추가작업 그리고 49개의 파일을 삭제
  1. 58 49
      src/app/components/line-segment-component/models/line-segment-model.js

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

@@ -13,7 +13,7 @@ import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
 
 export class LineSegmentModel extends GeometricObject {
 
-  constructor(pointA, pointB, label, id) {
+  constructor (pointA, pointB, label, id) {
     super(id);
     this.pointA = pointA;
     this.pointB = pointB;
@@ -21,9 +21,9 @@ export class LineSegmentModel extends GeometricObject {
     super.setClass(ELEMENTS_CLASS.LINE_SEGMENT);
     this.definitions.push(this.pointA);
     this.definitions.push(this.pointB);
-  }
+    }
 
-  getStraight() {
+  getStraight () {
     const aX = this.pointA.posX;
     const aY = this.pointA.posY;
     const bX = this.pointB.posX;
@@ -32,10 +32,10 @@ export class LineSegmentModel extends GeometricObject {
     const b = aX - bX;
     const c = a * aX + b * aY;
     return [a, b, c];
-  }
+    }
 
   // Return [(Bx-Ax, By-Ay)]
-  getDirection() {
+  getDirection () {
     const aX = this.pointA.posX;
     const aY = this.pointA.posY;
     const bX = this.pointB.posX;
@@ -43,16 +43,16 @@ export class LineSegmentModel extends GeometricObject {
     const a = bX - aX;
     const b = bY - aY;
     return [a, b];
-  }
+    }
 
 
-  getMiddlePoint() {
+  getMiddlePoint () {
     const x = (this.pointA.posX + this.pointB.posX) / 2;
     const y = (this.pointA.posY + this.pointB.posY) / 2;
     return { posX: x, posY: y };
-  }
+    }
 
-  getIntersection(geometricObject) {
+  getIntersection (geometricObject) {
     switch (geometricObject.elementClass) {
       case ELEMENTS_CLASS.LINE: // StraightLine
         return geometricObject.getIntersectionWithStraightLine(this); // Delegate to StraightLine
@@ -62,10 +62,10 @@ export class LineSegmentModel extends GeometricObject {
         return this.getIntersectionWithCircumference(geometricObject);
       default:
         break;
+      }
     }
-  }
 
-  getDeterminantByLine(lineSegment) {
+  getDeterminantByLine (lineSegment) {
     const og1 = this.getStraight();
     const og2 = lineSegment.getStraight();
     const a1 = og1[0];
@@ -74,9 +74,9 @@ export class LineSegmentModel extends GeometricObject {
     const b2 = og2[1];
     const determinant = a1 * b2 - a2 * b1;
     return determinant;
-  }
+    }
 
-  getIntersectionByLine(lineSegment) { //TODO nome deveria especificar SEGMENTO: getIntersectionWithSegment
+  getIntersectionByLine (lineSegment) { //TODO nome deveria especificar SEGMENTO: getIntersectionWithSegment
     const og1 = this.getStraight(); //r
     const og2 = lineSegment.getStraight(); //s
     const a1 = og1[0];
@@ -88,43 +88,58 @@ export class LineSegmentModel extends GeometricObject {
     const determinant = a1 * b2 - a2 * b1;
     if (determinant == 0) {
       return [new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, lineSegment, true, 0)];
-    }
+      }
     else {
       const x = (b2 * c1 - b1 * c2) / determinant;
       const y = (a1 * c2 - a2 * c1) / determinant;
       return [new IntersectionModel(x, y, undefined, this, lineSegment, true, 0)];
+      }
     }
-  }
 
   // Intersection with circunference
-  getIntersectionWithCircumference(circumference) { //TODO Nome? Melhor 'getIntersectionWithCircumference'
+  // @calledby line-model.js!getIntersectionWithCircumference(circ): return super.getIntersectionWithCircumference(circ);
+  getIntersectionWithCircumference (circumference) { //TODO Nome? Melhor 'getIntersectionWithCircumference'
     const pointA = this.pointA;
     const pointB = this.pointB;
     const center = circumference.center;
-    const radius = circumference.getRadius();
-
-    const dx = pointB.posX - pointA.posX;
-    const dy = pointB.posY - pointA.posY;
-
-    const cx = center.posX - pointA.posX;
-    const cy = center.posY - pointA.posY;
+    const radiusP = circumference.getRadius(); // is 'circumference.radius' that is a Point
+    const radius = circumference.getRadiusValue(); // is '||circumference.radius-circumference.center||
+    var existA = -1, existB = -1;
+    var aux = " (pointA.id=" + pointA.id + ", pointB.id=" + pointB.id + ", radiusP.id=" + radiusP.id + ")";
+    if (pointA.id==radiusP.id) {
+      console.log("*** line-segment-model.js!getIntersectionWithCircumference(.): PointA already in intersection!" + aux);
+      existA = 1; //pointA.og1 = this; pointA.og2 = circumference;
+      }
+    else
+    if (pointB.id==radiusP.id) {
+      console.log("*** line-segment-model.js!getIntersectionWithCircumference(.): pointA already in intersection!" + aux);
+      existB = 1; //pointB.og1 = this; pointB.og2 = circumference;
+      }
+    //D else console.log("xxx line-segment-model.js!getIntersectionWithCircumference(.): pointA or PointB NOT in intersection!" + aux);
+    //D console.trace();
+    const dx = pointB.posX - pointA.posX; // segment line pointA
+    const dy = pointB.posY - pointA.posY; // 
+    const cx = center.posX - pointA.posX; // circumference center point (center)
+    const cy = center.posY - pointA.posY; //
 
     const a = dx * dx + dy * dy; // ||B-A||^2 = (B-A)'(B-A)
     const b = dx * cx + dy * cy; // (B-A)'(C-A)
     const c = cx * cx + cy * cy - radius * radius; // ||C-A||^2 - r^2
-
     const D = b / a; // (B-A)'(C-A) / (B-A)'(B-A)
     const q = c / a; // (||C-A||^2 - r^2) / (B-A)'(B-A)
 
     const delta = D * D - q;
 
-    const PA = new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 0);
-    const PB = new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 1);
+    var PA, PB;
+    if (existA==-1) PA = new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 0);
+    else            PA = pointA; // intersection already exist (with point A)
+    if (existB==-1) PB = new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 1);
+    else            PA = pointB; // intersection already exist (with point B)
 
     if (delta < 0) { //TODO Verificar? No intersection points
       //D console.log("line-segment-model.js: getIntersectionWithCircumference(.): delta<0: " + Number.MAX_SAFE_INTEGER);
       return [PA, PB];
-    }
+      }
 
     const deltaSqrt = Math.sqrt(delta);
     const root1 = -D + deltaSqrt;
@@ -134,34 +149,28 @@ export class LineSegmentModel extends GeometricObject {
 
     if (delta == 0) { //TODO Verificar? Only one point (actually, both with the same coordinates)
       //D console.log("line-segment-model.js: getIntersectionWithCircumference(.): delta==0");
-      PA.bind(x1, y1, undefined, this, circumference, true, 0);
+      if (existA==-1)
+        PA.bind(x1, y1, undefined, this, circumference, true, 0);
       return [PA, PB];
-    }
-
-    if (delta == 0) {
-      PA.bind(x1, y1, undefined, this, circumference, true, 0);
-      return [
-        PA,
-        PB
-      ];
-    }
+      }
 
     const x2 = pointA.posX - dx * root2;
     const y2 = pointA.posY - dy * root2;
 
-    PA.bind(x1, y1, undefined, this, circumference, true, 0);
-    PB.bind(x2, y2, undefined, this, circumference, true, 1);
+    if (existA==-1) PA.bind(x1, y1, undefined, this, circumference, true, 0);
+    if (existB==-1) PB.bind(x2, y2, undefined, this, circumference, true, 1);
 
-    if (!this.insideSegment(PB.posX, PB.posY))
-      PB.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 1);
+    if (!this.insideSegment(PB.posX, PB.posY)) {
+      if (existB==-1) PB.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 1);
+      }
 
     return [PA, PB];
-  } // getIntersectionWithCircumference(circumference)
+    } // getIntersectionWithCircumference(circumference)
 
 
   // Considering de "level set" defined by direction d=B-A and intersection P,
   // if d'P<d'A or d'P>d'B, then intersection is empty
-  insideSegment(intersecX, intersecY) {
+  insideSegment (intersecX, intersecY) {
     //D console.log("line-segment-model.js: insideSegment(.)");
     const valuesR = this.getDirection(); // get d=B-A
     const dirX = valuesR[0]; // Bx-Ax
@@ -173,7 +182,7 @@ export class LineSegmentModel extends GeometricObject {
 
     if (cInterA < cRA) { // d'P < d'A
       return false;
-    }
+      }
 
     // comparacao cv do ponto B < cv da intersec =>  vazio
     const cRB = dirX * this.pointB.posX + dirY * this.pointB.posY;
@@ -182,12 +191,12 @@ export class LineSegmentModel extends GeometricObject {
       this.posX = Number.MAX_SAFE_INTEGER;
       this.posY = Number.MAX_SAFE_INTEGER;
       return false;
-    }
+      }
 
     return true;
-  }
+    }
 
-  static do(map, list) {
+  static do (map, list) {
     const id = map.get("id");
     const pointAId = map.get("param")[0];
     const pointBId = map.get("param")[1];
@@ -196,6 +205,6 @@ export class LineSegmentModel extends GeometricObject {
     const pointB = list.find(x => x.id === pointBId);
     const label = map.get("label")[0];
     return new LineSegmentModel(pointA, pointB, label, id);
-  }
+    }
 
-}
+  } // export class LineSegmentModel extends GeometricObject