intersection-model.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { PointModel } from "../../point-component/models/point-model";
  2. import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
  3. import { DrawerAggregator } from "../../../core/drawers/drawer-aggregator";
  4. import { GeometricObject } from "../../../core/models/objects/geometric-object";
  5. export class IntersectionModel extends PointModel {
  6. /**
  7. *
  8. * @param {GeometricObject} r Geometric Object
  9. * @param {GeometricObject} s Geometric Object
  10. * @param {boolean} visible Visiblity of Object
  11. * @param {number} index Index position of Object ex (1)
  12. */
  13. constructor(posX, posY, label, r, s, visible, index, id) {
  14. super(posX, posY, label, id);
  15. this.r = r;
  16. this.s = s;
  17. super.setClass(ELEMENTS_CLASS.INTERSECTION_POINT);
  18. this.visible = visible;
  19. this.index = index;
  20. this.color = -65536;
  21. this.definitions = this.getDefinitions();
  22. }
  23. /**
  24. * Update properties of this Intersection
  25. * @param {DrawerAggregator} aggregator Drawer Aggregator
  26. * @param {event} event
  27. */
  28. update(aggregator, event) {
  29. const intersections = this.r.getIntersection(this.s);
  30. if (intersections.length == 1) {
  31. this.visible = true;
  32. const intersection = intersections[0];
  33. this.posX = parseFloat(intersection.posX.toFixed(2));
  34. this.posY = parseFloat(intersection.posY.toFixed(2));
  35. // this.visible = intersection.visible;
  36. if (!this.r.insideSegment(this.posX, this.posY)) {
  37. this.posX = Number.MAX_SAFE_INTEGER;
  38. this.posY = Number.MAX_SAFE_INTEGER;
  39. this.visible = false;
  40. this.definitions = this.getDefinitions();
  41. return;
  42. }
  43. if (!this.s.insideSegment(this.posX, this.posY)) {
  44. this.posX = Number.MAX_SAFE_INTEGER;
  45. this.posY = Number.MAX_SAFE_INTEGER;
  46. this.visible = false;
  47. this.definitions = this.getDefinitions();
  48. return;
  49. }
  50. return;
  51. }
  52. if (intersections.length > 1) {
  53. for (let index = 0; index < intersections.length; index++) {
  54. const intersection = intersections[index];
  55. if (this.index == index) {
  56. this.posX = parseFloat(intersection.posX.toFixed(2));
  57. this.posY = parseFloat(intersection.posY.toFixed(2));
  58. this.visible = intersection.visible;
  59. }
  60. }
  61. }
  62. this.definitions = this.getDefinitions();
  63. }
  64. getDefinitions() {
  65. return [{ id: this.r.id }, { id: this.s.id }, { id: this.index + 1 }, { id: this.posX + 5 }, { id: this.posY - 5 }, { id: this.visible ? 1 : 0 }];
  66. }
  67. bind(posX, posY, label, r, s, visible, index) {
  68. super.bind(posX, posY, label);
  69. this.r = r;
  70. this.s = s;
  71. this.visible = visible;
  72. this.index = index;
  73. this.color = -65536;
  74. this.definitions = this.getDefinitions();
  75. super.setClass(ELEMENTS_CLASS.INTERSECTION_POINT);
  76. }
  77. /**
  78. * Create new Intersection By Line of Script .geo
  79. * @param {Map} map JavaScript Map
  80. * @param {[]} list List of Generic Objects
  81. */
  82. static do(map, list) {
  83. const id = map.get("id");
  84. const rId = map.get("param")[0];
  85. const sId = map.get("param")[1];
  86. const index = map.get("param")[2] - 1;
  87. const x = (map.get("param")[3]) - 5;
  88. const y = (map.get("param")[4]) + 5;
  89. const visible = map.get("param")[5] == 1;
  90. const label = map.get("label");
  91. const r = list.find(x => x.id == rId);
  92. const s = list.find(x => x.id == sId);
  93. return new IntersectionModel(x, y, label, r, s, visible, index, id);
  94. }
  95. }