circumference-model.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
  2. import { GeometricObject } from "../../../core/models/objects/geometric-object";
  3. import { IntersectionModel } from "../../intersection-component/models/intersection-model";
  4. export class CircumferenceModel extends GeometricObject {
  5. constructor(center, radius, id) {
  6. super(id);
  7. this.center = center;
  8. this.radius = radius;
  9. this._coordinates = [];
  10. this._coordinates[0] = this.center.posX;
  11. this._coordinates[1] = this.center.posY;
  12. this._coordinates[2] = this.radius.posX;
  13. this._coordinates[3] = this.radius.posY;
  14. this.color = -16776961;
  15. super.setClass(ELEMENTS_CLASS.CIRCUMFERENCE);
  16. this.definitions.push(this.center);
  17. this.definitions.push(this.radius);
  18. }
  19. getRadius() {
  20. this._coordinates[0] = this.center.posX;
  21. this._coordinates[1] = this.center.posY;
  22. this._coordinates[2] = this.radius.posX;
  23. this._coordinates[3] = this.radius.posY;
  24. const legA = this._coordinates[2] - this._coordinates[0];
  25. const legB = this._coordinates[3] - this._coordinates[1];
  26. const radius = Math.sqrt(Math.pow(legA, 2) + Math.pow(legB, 2));
  27. return radius;
  28. }
  29. getStraight() {
  30. const dx = this.radius.posX - this.center.posX;
  31. const dy = this.radius.posY - this.center.posY;
  32. const c = Math.sqrt(dy * dy + dx * dx);
  33. return [dx, dy, c];
  34. }
  35. getDirection() {
  36. const aX = this.center.posX;
  37. const aY = this.center.posY;
  38. const bX = this.radius.posX;
  39. const bY = this.radius.posY;
  40. const a = bX - aX;
  41. const b = bY - aY;
  42. return [a, b];
  43. }
  44. getIntersection(geometricObject) {
  45. switch (geometricObject.elementClass) {
  46. case ELEMENTS_CLASS.LINE_SEGMENT:
  47. return this.getIntersectionByLine(geometricObject);
  48. case ELEMENTS_CLASS.CIRCUMFERENCE:
  49. return this.getIntersectionsByCircumference(geometricObject);
  50. default:
  51. break;
  52. }
  53. }
  54. distance(center) {
  55. const dx = center.posX - this.center.posX;
  56. const dy = center.posY - this.center.posY;
  57. const dist = Math.sqrt(dy * dy + dx * dx);
  58. return dist;
  59. }
  60. getIntersectionsByCircumference(circumference) {
  61. // if (this.cente().igual(cd.C()))
  62. // return null; // duas circ. com mesmo raio => devolva "null" (ambiguidade!)
  63. const r1 = this.getRadius(); // raio circ. atual
  64. const r2 = circumference.getRadius(); // raio circ. "cd"
  65. const d = this.distance(circumference); // distancia entre os raios das circ.
  66. if (r1 + r2 < d || Math.abs(r1 - r2) > d) return null;
  67. const A = this.center; // pega o centro da circunferencia atual
  68. const B = circumference.center; // pega o centro da circunferencia "cd"
  69. const x = B.posX - A.posX;
  70. const y = B.posY - A.posY;
  71. let cos = x / Math.sqrt(x * x + y * y); //(B.posX-A.posX)/Math.sqrt(x*x + y*y); // cos a = <z,w>/(|z||w|)
  72. let sen = Math.sqrt(1 - cos * cos);
  73. if (A.posY > B.posY) sen = -sen;
  74. let u1 = B.posX - A.posX;
  75. let v1 = B.posY - A.posY;
  76. let u = u1 * cos + v1 * sen;
  77. let v = -u1 * sen + v1 * cos;
  78. const rA = this.getRadius();
  79. const rB = circumference.getRadius();
  80. u = (u * u - rB * rB + rA * rA) / (2 * u);
  81. v = Math.sqrt(rA * rA - u * u);
  82. v1 = v;
  83. let v2 = -v;
  84. let x1 = u * cos - v1 * sen;
  85. let y1 = u * sen + v1 * cos;
  86. let x2 = u * cos - v2 * sen;
  87. let y2 = u * sen + v2 * cos;
  88. x1 = A.posX + x1;
  89. y1 = A.posY + y1;
  90. x2 = A.posX + x2;
  91. y2 = A.posY + y2;
  92. return [
  93. new IntersectionModel(x1, y1, undefined, this, circumference, true, 0),
  94. new IntersectionModel(x2, y2, undefined, this, circumference, true, 1)
  95. ];
  96. }
  97. getIntersectionByLine(lineSegment) {
  98. const r = this.getStraight();
  99. const s = lineSegment.getStraight();
  100. const a1 = r[0];
  101. const b1 = r[1];
  102. const c1 = r[2];
  103. const a2 = s[0];
  104. const b2 = s[1];
  105. const c2 = s[2];
  106. const determinant = a1 * b2 - a2 * b1;
  107. if (determinant == 0) {
  108. return [
  109. new IntersectionModel(
  110. Number.MAX_SAFE_INTEGER,
  111. Number.MAX_SAFE_INTEGER,
  112. undefined,
  113. this,
  114. lineSegment
  115. )
  116. ];
  117. } else {
  118. const x = (b2 * c1 - b1 * c2) / determinant;
  119. const y = (a1 * c2 - a2 * c1) / determinant;
  120. return [new IntersectionModel(x, y, undefined, this, lineSegment)];
  121. }
  122. }
  123. insideSegment(intersecX, intersecY) {
  124. const valuesR = this.getDirection();
  125. const dirX = valuesR[0];
  126. const dirY = valuesR[1];
  127. const cInterA = dirX * intersecX + dirY * intersecY;
  128. // comparaca cv do ponto A > cv da intersec => vazio
  129. const cRA = dirX * this.center.posX + dirY * this.center.posY;
  130. if (cInterA < cRA) {
  131. return false;
  132. }
  133. // comparaca cv do ponto B < cv da intersec => vazio
  134. const cRB = dirX * this.radius.posX + dirY * this.radius.posY;
  135. if (cInterA > cRB) {
  136. this.posX = Number.MAX_SAFE_INTEGER;
  137. this.posY = Number.MAX_SAFE_INTEGER;
  138. return false;
  139. }
  140. return true;
  141. }
  142. static do(map, list) {
  143. const id = map.get("id");
  144. const centerId = map.get("param")[0];
  145. const radiusId = map.get("param")[1];
  146. const center = list.find(x => x.id === centerId);
  147. const radius = list.find(x => x.id === radiusId);
  148. const label = map.get("label")[0];
  149. const circumference = new CircumferenceModel(center, radius, id);
  150. circumference.setLabel(label);
  151. return circumference;
  152. }
  153. }