|
@@ -2,188 +2,188 @@ 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) {
|
|
|
|
- super(id);
|
|
|
|
- this.pointA = pointA;
|
|
|
|
- this.pointB = pointB;
|
|
|
|
- this.setLabel(label);
|
|
|
|
- super.setClass(ELEMENTS_CLASS.LINE_SEGMENT);
|
|
|
|
- this.definitions.push(this.pointA);
|
|
|
|
- this.definitions.push(this.pointB);
|
|
|
|
- }
|
|
|
|
- getStraight() {
|
|
|
|
- const aX = this.pointA.posX;
|
|
|
|
- const aY = this.pointA.posY;
|
|
|
|
- const bX = this.pointB.posX;
|
|
|
|
- const bY = this.pointB.posY;
|
|
|
|
- const a = bY - aY;
|
|
|
|
- const b = aX - bX;
|
|
|
|
- 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];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- 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) {
|
|
|
|
- case ELEMENTS_CLASS.LINE_SEGMENT:
|
|
|
|
- return this.getIntersectionByLine(geometricObject);
|
|
|
|
- case ELEMENTS_CLASS.CIRCUMFERENCE:
|
|
|
|
- return this.getIntersectionByCircumference(geometricObject);
|
|
|
|
- default:
|
|
|
|
- break;
|
|
|
|
|
|
+ constructor(pointA, pointB, label, id) {
|
|
|
|
+ super(id);
|
|
|
|
+ this.pointA = pointA;
|
|
|
|
+ this.pointB = pointB;
|
|
|
|
+ this.setLabel(label);
|
|
|
|
+ super.setClass(ELEMENTS_CLASS.LINE_SEGMENT);
|
|
|
|
+ this.definitions.push(this.pointA);
|
|
|
|
+ this.definitions.push(this.pointB);
|
|
}
|
|
}
|
|
- }
|
|
|
|
- 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];
|
|
|
|
- const determinant = a1 * b2 - a2 * b1;
|
|
|
|
- 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];
|
|
|
|
- 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)];
|
|
|
|
|
|
+ getStraight() {
|
|
|
|
+ const aX = this.pointA.posX;
|
|
|
|
+ const aY = this.pointA.posY;
|
|
|
|
+ const bX = this.pointB.posX;
|
|
|
|
+ const bY = this.pointB.posY;
|
|
|
|
+ const a = bY - aY;
|
|
|
|
+ const b = aX - bX;
|
|
|
|
+ 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];
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- getIntersectionByCircumference(circumference) {
|
|
|
|
|
|
|
|
- const pointA = this.pointA;
|
|
|
|
- const pointB = this.pointB;
|
|
|
|
- const center = circumference.center;
|
|
|
|
- const radius = circumference.getRadius();
|
|
|
|
|
|
+ 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) {
|
|
|
|
+ case ELEMENTS_CLASS.LINE_SEGMENT:
|
|
|
|
+ return this.getIntersectionByLine(geometricObject);
|
|
|
|
+ case ELEMENTS_CLASS.CIRCUMFERENCE:
|
|
|
|
+ return this.getIntersectionByCircumference(geometricObject);
|
|
|
|
+ default:
|
|
|
|
+ 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];
|
|
|
|
+ const determinant = a1 * b2 - a2 * b1;
|
|
|
|
+ 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];
|
|
|
|
+ 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)];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- const dx = pointB.posX - pointA.posX;
|
|
|
|
- const dy = pointB.posY - pointA.posY;
|
|
|
|
|
|
+ getIntersectionByCircumference(circumference) {
|
|
|
|
|
|
- const cx = center.posX - pointA.posX;
|
|
|
|
- const cy = center.posY - pointA.posY;
|
|
|
|
|
|
+ const pointA = this.pointA;
|
|
|
|
+ const pointB = this.pointB;
|
|
|
|
+ const center = circumference.center;
|
|
|
|
+ const radius = circumference.getRadius();
|
|
|
|
|
|
- const a = dx * dx + dy * dy;
|
|
|
|
- const b = dx * cx + dy * cy;
|
|
|
|
- const c = cx * cx + cy * cy - radius * radius;
|
|
|
|
|
|
+ const dx = pointB.posX - pointA.posX;
|
|
|
|
+ const dy = pointB.posY - pointA.posY;
|
|
|
|
|
|
- const D = b / a;
|
|
|
|
- const q = c / a;
|
|
|
|
|
|
+ const cx = center.posX - pointA.posX;
|
|
|
|
+ const cy = center.posY - pointA.posY;
|
|
|
|
|
|
- const delta = D * D - q;
|
|
|
|
|
|
+ const a = dx * dx + dy * dy;
|
|
|
|
+ const b = dx * cx + dy * cy;
|
|
|
|
+ const c = cx * cx + cy * cy - radius * radius;
|
|
|
|
|
|
- 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 D = b / a;
|
|
|
|
+ const q = c / a;
|
|
|
|
|
|
- if (delta < 0) {
|
|
|
|
- return [
|
|
|
|
- PA,
|
|
|
|
- PB
|
|
|
|
- ];
|
|
|
|
- }
|
|
|
|
|
|
+ const delta = D * D - q;
|
|
|
|
|
|
- const deltaSqrt = Math.sqrt(delta);
|
|
|
|
- const root1 = -D + deltaSqrt;
|
|
|
|
- const root2 = -D - deltaSqrt;
|
|
|
|
- const x1 = pointA.posX - dx * root1
|
|
|
|
- const y1 = pointA.posY - dy * root1;
|
|
|
|
-
|
|
|
|
- if (delta == 0) {
|
|
|
|
- PA.bind(x1, y1, undefined, this, circumference, true, 0);
|
|
|
|
- return [
|
|
|
|
- PA,
|
|
|
|
- PB
|
|
|
|
- ];
|
|
|
|
- }
|
|
|
|
|
|
+ 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 x2 = pointA.posX - dx * root2;
|
|
|
|
- const y2 = pointA.posY - dy * root2;
|
|
|
|
|
|
+ if (delta < 0) {
|
|
|
|
+ return [
|
|
|
|
+ PA,
|
|
|
|
+ PB
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
|
|
- PA.bind(x1, y1, undefined, this, circumference, true, 0);
|
|
|
|
- PB.bind(x2, y2, undefined, this, circumference, true, 1);
|
|
|
|
|
|
+ const deltaSqrt = Math.sqrt(delta);
|
|
|
|
+ const root1 = -D + deltaSqrt;
|
|
|
|
+ const root2 = -D - deltaSqrt;
|
|
|
|
+ const x1 = pointA.posX - dx * root1
|
|
|
|
+ const y1 = pointA.posY - dy * root1;
|
|
|
|
|
|
- if (this.visible) {
|
|
|
|
- if (!this.insideSegment(PA.posX, PA.posY))
|
|
|
|
- PA.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 0)
|
|
|
|
|
|
+ if (delta == 0) {
|
|
|
|
+ PA.bind(x1, y1, undefined, this, circumference, true, 0);
|
|
|
|
+ return [
|
|
|
|
+ PA,
|
|
|
|
+ PB
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
|
|
- 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
|
|
|
|
- ];
|
|
|
|
|
|
+ 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);
|
|
|
|
|
|
- insideSegment(intersecX, intersecY) {
|
|
|
|
- const valuesR = this.getDirection();
|
|
|
|
- const dirX = valuesR[0];
|
|
|
|
- const dirY = valuesR[1];
|
|
|
|
- const cInterA = dirX * intersecX + dirY * intersecY;
|
|
|
|
|
|
+ if (this.visible) {
|
|
|
|
+ if (!this.insideSegment(PA.posX, PA.posY))
|
|
|
|
+ PA.bind(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, undefined, this, circumference, false, 0)
|
|
|
|
|
|
- // comparaca cv do ponto A > cv da intersec => vazio
|
|
|
|
- const cRA = dirX * this.pointA.posX + dirY * this.pointA.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
|
|
|
|
+ ];
|
|
|
|
|
|
- if (cInterA < cRA) {
|
|
|
|
- return false;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- // comparaca cv do ponto B < cv da intersec => vazio
|
|
|
|
- const cRB = dirX * this.pointB.posX + dirY * this.pointB.posY;
|
|
|
|
|
|
+ insideSegment(intersecX, intersecY) {
|
|
|
|
+ const valuesR = this.getDirection();
|
|
|
|
+ const dirX = valuesR[0];
|
|
|
|
+ const dirY = valuesR[1];
|
|
|
|
+ const cInterA = dirX * intersecX + dirY * intersecY;
|
|
|
|
|
|
- if (cInterA > cRB) {
|
|
|
|
- this.posX = Number.MAX_SAFE_INTEGER;
|
|
|
|
- this.posY = Number.MAX_SAFE_INTEGER;
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ // 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;
|
|
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- static do(map, list) {
|
|
|
|
- const id = map.get("id");
|
|
|
|
- const pointAId = map.get("param")[0];
|
|
|
|
- const pointBId = map.get("param")[1];
|
|
|
|
- const pointA = list.find(x => x.id === pointAId);
|
|
|
|
- const pointB = list.find(x => x.id === pointBId);
|
|
|
|
- const label = map.get("label")[0];
|
|
|
|
- return new LineSegmentModel(pointA, pointB, label, id);
|
|
|
|
- }
|
|
|
|
|
|
+ if (cInterA > cRB) {
|
|
|
|
+ this.posX = Number.MAX_SAFE_INTEGER;
|
|
|
|
+ this.posY = Number.MAX_SAFE_INTEGER;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ static do(map, list) {
|
|
|
|
+ const id = map.get("id");
|
|
|
|
+ const pointAId = map.get("param")[0];
|
|
|
|
+ const pointBId = map.get("param")[1];
|
|
|
|
+ const pointA = list.find(x => x.id === pointAId);
|
|
|
|
+ const pointB = list.find(x => x.id === pointBId);
|
|
|
|
+ const label = map.get("label")[0];
|
|
|
|
+ return new LineSegmentModel(pointA, pointB, label, id);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|