|
@@ -1,8 +1,19 @@
|
|
|
|
+/*
|
|
|
|
+ * iGeom by LInE
|
|
|
|
+ * Geometric Object: Segment
|
|
|
|
+ * Manage intersection of Segment with: Sements, Circumference
|
|
|
|
+ * www.matematica.br/igeom
|
|
|
|
+ * .app/components/line-segment-component/models/line-segment-model.js
|
|
|
|
+ * @version 2020/11/02: Implemented Line instersection
|
|
|
|
+ */
|
|
|
|
+
|
|
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";
|
|
import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
|
|
import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
|
|
|
|
+
|
|
export class LineSegmentModel extends GeometricObject {
|
|
export class LineSegmentModel extends GeometricObject {
|
|
- constructor(pointA, pointB, label, id) {
|
|
|
|
|
|
+
|
|
|
|
+ constructor (pointA, pointB, label, id) {
|
|
super(id);
|
|
super(id);
|
|
this.pointA = pointA;
|
|
this.pointA = pointA;
|
|
this.pointB = pointB;
|
|
this.pointB = pointB;
|
|
@@ -10,8 +21,9 @@ export class LineSegmentModel extends GeometricObject {
|
|
super.setClass(ELEMENTS_CLASS.LINE_SEGMENT);
|
|
super.setClass(ELEMENTS_CLASS.LINE_SEGMENT);
|
|
this.definitions.push(this.pointA);
|
|
this.definitions.push(this.pointA);
|
|
this.definitions.push(this.pointB);
|
|
this.definitions.push(this.pointB);
|
|
- }
|
|
|
|
- getStraight() {
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getStraight () {
|
|
const aX = this.pointA.posX;
|
|
const aX = this.pointA.posX;
|
|
const aY = this.pointA.posY;
|
|
const aY = this.pointA.posY;
|
|
const bX = this.pointB.posX;
|
|
const bX = this.pointB.posX;
|
|
@@ -20,8 +32,10 @@ export class LineSegmentModel extends GeometricObject {
|
|
const b = aX - bX;
|
|
const b = aX - bX;
|
|
const c = a * aX + b * aY;
|
|
const c = a * aX + b * aY;
|
|
return [a, b, c];
|
|
return [a, b, c];
|
|
- }
|
|
|
|
- getDirection() {
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Return [(Bx-Ax, By-Ay)]
|
|
|
|
+ getDirection () {
|
|
const aX = this.pointA.posX;
|
|
const aX = this.pointA.posX;
|
|
const aY = this.pointA.posY;
|
|
const aY = this.pointA.posY;
|
|
const bX = this.pointB.posX;
|
|
const bX = this.pointB.posX;
|
|
@@ -29,56 +43,61 @@ export class LineSegmentModel 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) {
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ 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) {
|
|
switch (geometricObject.elementClass) {
|
|
switch (geometricObject.elementClass) {
|
|
|
|
+ case ELEMENTS_CLASS.LINE: // StraightLine
|
|
|
|
+ return geometricObject.getIntersectionWithStraightLine(this); // Delegate to StraightLine
|
|
case ELEMENTS_CLASS.LINE_SEGMENT:
|
|
case ELEMENTS_CLASS.LINE_SEGMENT:
|
|
- return this.getIntersectionByLine(geometricObject);
|
|
|
|
|
|
+ return this.getIntersectionByLine(geometricObject); //TODO nome deveria especificar SEGMENTO: this.getIntersectionWithSegment
|
|
case ELEMENTS_CLASS.CIRCUMFERENCE:
|
|
case ELEMENTS_CLASS.CIRCUMFERENCE:
|
|
return this.getIntersectionByCircumference(geometricObject);
|
|
return this.getIntersectionByCircumference(geometricObject);
|
|
default:
|
|
default:
|
|
break;
|
|
break;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
- getDeterminantByLine(lineSegment) {
|
|
|
|
- const r = this.getStraight();
|
|
|
|
- const s = lineSegment.getStraight();
|
|
|
|
- const a1 = r[0];
|
|
|
|
- const b1 = r[1];
|
|
|
|
- const a2 = s[0];
|
|
|
|
- const b2 = s[1];
|
|
|
|
|
|
+
|
|
|
|
+ getDeterminantByLine (lineSegment) {
|
|
|
|
+ const og1 = this.getStraight();
|
|
|
|
+ const og2 = lineSegment.getStraight();
|
|
|
|
+ const a1 = og1[0];
|
|
|
|
+ const b1 = og1[1];
|
|
|
|
+ const a2 = og2[0];
|
|
|
|
+ const b2 = og2[1];
|
|
const determinant = a1 * b2 - a2 * b1;
|
|
const determinant = a1 * b2 - a2 * b1;
|
|
return determinant;
|
|
return determinant;
|
|
- }
|
|
|
|
- getIntersectionByLine(lineSegment) {
|
|
|
|
- const r = this.getStraight();
|
|
|
|
- const s = lineSegment.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];
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getIntersectionByLine (lineSegment) { //TODO nome deveria especificar SEGMENTO: getIntersectionWithSegment
|
|
|
|
+ const og1 = this.getStraight(); //r
|
|
|
|
+ const og2 = lineSegment.getStraight(); //s
|
|
|
|
+ const a1 = og1[0];
|
|
|
|
+ const b1 = og1[1];
|
|
|
|
+ const c1 = og1[2];
|
|
|
|
+ const a2 = og2[0];
|
|
|
|
+ const b2 = og2[1];
|
|
|
|
+ const c2 = og2[2];
|
|
const determinant = a1 * b2 - a2 * b1;
|
|
const determinant = a1 * b2 - a2 * b1;
|
|
if (determinant == 0) {
|
|
if (determinant == 0) {
|
|
- return [
|
|
|
|
- new IntersectionModel(
|
|
|
|
- Number.MAX_SAFE_INTEGER,
|
|
|
|
- Number.MAX_SAFE_INTEGER,
|
|
|
|
- undefined,
|
|
|
|
- this,
|
|
|
|
- lineSegment
|
|
|
|
- )
|
|
|
|
- ];
|
|
|
|
- } else {
|
|
|
|
|
|
+ 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 x = (b2 * c1 - b1 * c2) / determinant;
|
|
const y = (a1 * c2 - a2 * c1) / determinant;
|
|
const y = (a1 * c2 - a2 * c1) / determinant;
|
|
- return [new IntersectionModel(x, y, undefined, this, lineSegment)];
|
|
|
|
|
|
+ return [new IntersectionModel(x, y, undefined, this, lineSegment, true, 0)];
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
-
|
|
|
|
- getIntersectionByCircumference(circumference) {
|
|
|
|
|
|
|
|
|
|
+ // Intersection with circunference
|
|
|
|
+ getIntersectionByCircumference (circumference) { //TODO Nome? Melhor 'getIntersectionWithCircumference'
|
|
const pointA = this.pointA;
|
|
const pointA = this.pointA;
|
|
const pointB = this.pointB;
|
|
const pointB = this.pointB;
|
|
const center = circumference.center;
|
|
const center = circumference.center;
|
|
@@ -90,24 +109,22 @@ export class LineSegmentModel extends GeometricObject {
|
|
const cx = center.posX - pointA.posX;
|
|
const cx = center.posX - pointA.posX;
|
|
const cy = center.posY - pointA.posY;
|
|
const cy = center.posY - pointA.posY;
|
|
|
|
|
|
- const a = dx * dx + dy * dy;
|
|
|
|
- const b = dx * cx + dy * cy;
|
|
|
|
- const c = cx * cx + cy * cy - radius * radius;
|
|
|
|
|
|
+ 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;
|
|
|
|
- const q = c / a;
|
|
|
|
|
|
+ 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 delta = D * D - q;
|
|
|
|
|
|
const PA = new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 0);
|
|
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);
|
|
const PB = new IntersectionModel(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 1);
|
|
|
|
|
|
- if (delta < 0) {
|
|
|
|
- return [
|
|
|
|
- PA,
|
|
|
|
- PB
|
|
|
|
- ];
|
|
|
|
- }
|
|
|
|
|
|
+ if (delta < 0) { //TODO Verificar? No intersection points
|
|
|
|
+ //D console.log("line-segment-model.js: getIntersectionByCircumference(.): delta<0: " + Number.MAX_SAFE_INTEGER);
|
|
|
|
+ return [PA,PB];
|
|
|
|
+ }
|
|
|
|
|
|
const deltaSqrt = Math.sqrt(delta);
|
|
const deltaSqrt = Math.sqrt(delta);
|
|
const root1 = -D + deltaSqrt;
|
|
const root1 = -D + deltaSqrt;
|
|
@@ -115,13 +132,11 @@ export class LineSegmentModel extends GeometricObject {
|
|
const x1 = pointA.posX - dx * root1
|
|
const x1 = pointA.posX - dx * root1
|
|
const y1 = pointA.posY - dy * root1;
|
|
const y1 = pointA.posY - dy * root1;
|
|
|
|
|
|
- if (delta == 0) {
|
|
|
|
|
|
+ if (delta == 0) { //TODO Verificar? Only one point (actually, both with the same coordinates)
|
|
|
|
+ //D console.log("line-segment-model.js: getIntersectionByCircumference(.): delta==0");
|
|
PA.bind(x1, y1, undefined, this, circumference, true, 0);
|
|
PA.bind(x1, y1, undefined, this, circumference, 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;
|
|
@@ -134,41 +149,41 @@ export class LineSegmentModel extends GeometricObject {
|
|
PA.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 0)
|
|
PA.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 0)
|
|
|
|
|
|
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, this, circumference, false, 1)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return [
|
|
|
|
- PA,
|
|
|
|
- PB
|
|
|
|
- ];
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- insideSegment(intersecX, intersecY) {
|
|
|
|
- const valuesR = this.getDirection();
|
|
|
|
- const dirX = valuesR[0];
|
|
|
|
- const dirY = valuesR[1];
|
|
|
|
|
|
+ PB.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 1);
|
|
|
|
+ }
|
|
|
|
+ return [PA,PB];
|
|
|
|
+ } // getIntersectionByCircumference(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) {
|
|
|
|
+ //D console.log("line-segment-model.js: insideSegment(.)");
|
|
|
|
+ const valuesR = this.getDirection(); // get d=B-A
|
|
|
|
+ const dirX = valuesR[0]; // Bx-Ax
|
|
|
|
+ const dirY = valuesR[1]; // By-Ay
|
|
const cInterA = dirX * intersecX + dirY * intersecY;
|
|
const cInterA = dirX * intersecX + dirY * intersecY;
|
|
|
|
|
|
- // comparaca cv do ponto A > cv da intersec => vazio
|
|
|
|
|
|
+ // comparacao cv do ponto A > cv da intersec => vazio
|
|
const cRA = dirX * this.pointA.posX + dirY * this.pointA.posY;
|
|
const cRA = dirX * this.pointA.posX + dirY * this.pointA.posY;
|
|
|
|
|
|
- if (cInterA < cRA) {
|
|
|
|
|
|
+ if (cInterA < cRA) { // d'P < d'A
|
|
return false;
|
|
return false;
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- // comparaca cv do ponto B < cv da intersec => vazio
|
|
|
|
|
|
+ // comparacao cv do ponto B < cv da intersec => vazio
|
|
const cRB = dirX * this.pointB.posX + dirY * this.pointB.posY;
|
|
const cRB = dirX * this.pointB.posX + dirY * this.pointB.posY;
|
|
|
|
|
|
- if (cInterA > cRB) {
|
|
|
|
|
|
+ if (cInterA > cRB) { // d'P > d'B
|
|
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 pointAId = map.get("param")[0];
|
|
const pointAId = map.get("param")[0];
|
|
const pointBId = map.get("param")[1];
|
|
const pointBId = map.get("param")[1];
|
|
@@ -176,6 +191,6 @@ export class LineSegmentModel extends GeometricObject {
|
|
const pointB = list.find(x => x.id === pointBId);
|
|
const pointB = list.find(x => x.id === pointBId);
|
|
const label = map.get("label")[0];
|
|
const label = map.get("label")[0];
|
|
return new LineSegmentModel(pointA, pointB, label, id);
|
|
return new LineSegmentModel(pointA, pointB, label, id);
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ }
|