line-segment-model.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { GeometricObject } from "../../../core/models/objects/geometric-object";
  2. import { IntersectionModel } from "../../intersection-component/models/intersection-model";
  3. import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
  4. export class LineSegmentModel extends GeometricObject {
  5. constructor(pointA, pointB, label, id) {
  6. super(id);
  7. this.pointA = pointA;
  8. this.pointB = pointB;
  9. this.setLabel(label);
  10. super.setClass(ELEMENTS_CLASS.LINE_SEGMENT);
  11. this.definitions.push(this.pointA);
  12. this.definitions.push(this.pointB);
  13. }
  14. getStraight() {
  15. const aX = this.pointA.posX;
  16. const aY = this.pointA.posY;
  17. const bX = this.pointB.posX;
  18. const bY = this.pointB.posY;
  19. const a = bY - aY;
  20. const b = aX - bX;
  21. const c = a * aX + b * aY;
  22. return [a, b, c];
  23. }
  24. getDirection() {
  25. const aX = this.pointA.posX;
  26. const aY = this.pointA.posY;
  27. const bX = this.pointB.posX;
  28. const bY = this.pointB.posY;
  29. const a = bX - aX;
  30. const b = bY - aY;
  31. return [a, b];
  32. }
  33. getIntersection(geometricObject) {
  34. switch (geometricObject.elementClass) {
  35. case ELEMENTS_CLASS.LINE_SEGMENT:
  36. return this.getIntersectionByLine(geometricObject);
  37. case ELEMENTS_CLASS.CIRCUMFERENCE:
  38. return this.getIntersectionByCircumference(geometricObject);
  39. default:
  40. break;
  41. }
  42. }
  43. getIntersectionByLine(lineSegment) {
  44. const r = this.getStraight();
  45. const s = lineSegment.getStraight();
  46. const a1 = r[0];
  47. const b1 = r[1];
  48. const c1 = r[2];
  49. const a2 = s[0];
  50. const b2 = s[1];
  51. const c2 = s[2];
  52. const determinant = a1 * b2 - a2 * b1;
  53. if (determinant == 0) {
  54. return [
  55. new IntersectionModel(
  56. Number.MAX_SAFE_INTEGER,
  57. Number.MAX_SAFE_INTEGER,
  58. undefined,
  59. this,
  60. lineSegment
  61. )
  62. ];
  63. } else {
  64. const x = (b2 * c1 - b1 * c2) / determinant;
  65. const y = (a1 * c2 - a2 * c1) / determinant;
  66. return [new IntersectionModel(x, y, undefined, this, lineSegment)];
  67. }
  68. }
  69. getIntersectionByCircumference(circumference) {
  70. const r = this.getStraight();
  71. const s = circumference.getStraight();
  72. const a1 = r[0];
  73. const b1 = r[1];
  74. const c1 = r[2];
  75. const a2 = s[0];
  76. const b2 = s[1];
  77. const c2 = s[2];
  78. const determinant = a1 * b2 - a2 * b1;
  79. if (determinant == 0) {
  80. return [
  81. new IntersectionModel(
  82. Number.MAX_SAFE_INTEGER,
  83. Number.MAX_SAFE_INTEGER,
  84. undefined,
  85. this,
  86. circumference
  87. )
  88. ];
  89. } else {
  90. const x = (b2 * c1 - b1 * c2) / determinant;
  91. const y = (a1 * c2 - a2 * c1) / determinant;
  92. return [new IntersectionModel(x, y, undefined, this, circumference)];
  93. }
  94. }
  95. insideSegment(intersecX, intersecY) {
  96. const valuesR = this.getDirection();
  97. const dirX = valuesR[0];
  98. const dirY = valuesR[1];
  99. const cInterA = dirX * intersecX + dirY * intersecY;
  100. // comparaca cv do ponto A > cv da intersec => vazio
  101. const cRA = dirX * this.pointA.posX + dirY * this.pointA.posY;
  102. if (cInterA < cRA) {
  103. return false;
  104. }
  105. // comparaca cv do ponto B < cv da intersec => vazio
  106. const cRB = dirX * this.pointB.posX + dirY * this.pointB.posY;
  107. if (cInterA > cRB) {
  108. this.posX = Number.MAX_SAFE_INTEGER;
  109. this.posY = Number.MAX_SAFE_INTEGER;
  110. return false;
  111. }
  112. return true;
  113. }
  114. static do(map, list) {
  115. const id = map.get("id");
  116. const pointAId = map.get("param")[0];
  117. const pointBId = map.get("param")[1];
  118. const pointA = list.find(x => x.id === pointAId);
  119. const pointB = list.find(x => x.id === pointBId);
  120. const label = map.get("label")[0];
  121. return new LineSegmentModel(pointA, pointB, label, id);
  122. }
  123. }