line-segment-model.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  12. getStraight() {
  13. const aX = this.pointA.posX;
  14. const aY = this.pointA.posY;
  15. const bX = this.pointB.posX;
  16. const bY = this.pointB.posY;
  17. const a = bY - aY;
  18. const b = aX - bX;
  19. const c = a * aX + b * aY;
  20. return [a, b, c];
  21. }
  22. getDirection() {
  23. const aX = this.pointA.posX;
  24. const aY = this.pointA.posY;
  25. const bX = this.pointB.posX;
  26. const bY = this.pointB.posY;
  27. const a = bX - aX;
  28. const b = bY - aY;
  29. return [a, b];
  30. }
  31. getIntersection(geometricObject) {
  32. switch (geometricObject.elementClass) {
  33. case ELEMENTS_CLASS.LINE_SEGMENT:
  34. return this.getIntersectionByLine(geometricObject);
  35. case ELEMENTS_CLASS.CIRCUMFERENCE:
  36. return this.getIntersectionByCircumference(geometricObject);
  37. default:
  38. break;
  39. }
  40. }
  41. getIntersectionByLine(lineSegment) {
  42. const r = this.getStraight();
  43. const s = lineSegment.getStraight();
  44. const a1 = r[0];
  45. const b1 = r[1];
  46. const c1 = r[2];
  47. const a2 = s[0];
  48. const b2 = s[1];
  49. const c2 = s[2];
  50. const determinant = a1 * b2 - a2 * b1;
  51. if (determinant == 0) {
  52. return [
  53. new IntersectionModel(
  54. Number.MAX_SAFE_INTEGER,
  55. Number.MAX_SAFE_INTEGER,
  56. undefined,
  57. this,
  58. lineSegment
  59. )
  60. ];
  61. } else {
  62. const x = (b2 * c1 - b1 * c2) / determinant;
  63. const y = (a1 * c2 - a2 * c1) / determinant;
  64. return [new IntersectionModel(x, y, undefined, this, lineSegment)];
  65. }
  66. }
  67. getIntersectionByCircumference(circumference) {
  68. const r = this.getStraight();
  69. const s = circumference.getStraight();
  70. const a1 = r[0];
  71. const b1 = r[1];
  72. const c1 = r[2];
  73. const a2 = s[0];
  74. const b2 = s[1];
  75. const c2 = s[2];
  76. const determinant = a1 * b2 - a2 * b1;
  77. if (determinant == 0) {
  78. return [
  79. new IntersectionModel(
  80. Number.MAX_SAFE_INTEGER,
  81. Number.MAX_SAFE_INTEGER,
  82. undefined,
  83. this,
  84. circumference
  85. )
  86. ];
  87. } else {
  88. const x = (b2 * c1 - b1 * c2) / determinant;
  89. const y = (a1 * c2 - a2 * c1) / determinant;
  90. return [new IntersectionModel(x, y, undefined, this, circumference)];
  91. }
  92. }
  93. insideSegment(intersecX, intersecY) {
  94. const valuesR = this.getDirection();
  95. const dirX = valuesR[0];
  96. const dirY = valuesR[1];
  97. const cInterA = dirX * intersecX + dirY * intersecY;
  98. // comparaca cv do ponto A > cv da intersec => vazio
  99. const cRA = dirX * this.pointA.posX + dirY * this.pointA.posY;
  100. if (cInterA < cRA) {
  101. return false;
  102. }
  103. // comparaca cv do ponto B < cv da intersec => vazio
  104. const cRB = dirX * this.pointB.posX + dirY * this.pointB.posY;
  105. if (cInterA > cRB) {
  106. this.posX = Number.MAX_SAFE_INTEGER;
  107. this.posY = Number.MAX_SAFE_INTEGER;
  108. return false;
  109. }
  110. return true;
  111. }
  112. }