intersection-model.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. this.visible = true;
  30. const intersections = this.r.getIntersection(this.s);
  31. if (intersections.length == 1) {
  32. const intersection = intersections[0];
  33. this.posX = parseFloat(intersection.posX.toFixed(2));
  34. this.posY = parseFloat(intersection.posY.toFixed(2));
  35. if (!this.r.insideSegment(this.posX, this.posY)) {
  36. this.posX = Number.MAX_SAFE_INTEGER;
  37. this.posY = Number.MAX_SAFE_INTEGER;
  38. this.visible = false;
  39. this.definitions = this.getDefinitions();
  40. return;
  41. }
  42. if (!this.s.insideSegment(this.posX, this.posY)) {
  43. this.posX = Number.MAX_SAFE_INTEGER;
  44. this.posY = Number.MAX_SAFE_INTEGER;
  45. this.visible = false;
  46. this.definitions = this.getDefinitions();
  47. return;
  48. }
  49. return;
  50. }
  51. if (intersections.length > 1) {
  52. for (let index = 0; index < intersections.length; index++) {
  53. const intersection = intersections[index];
  54. if (this.index == index) {
  55. console.info(this.posX, intersection.posX);
  56. this.posX = parseFloat(intersection.posX.toFixed(2));
  57. this.posY = parseFloat(intersection.posY.toFixed(2));
  58. }
  59. }
  60. }
  61. this.definitions = this.getDefinitions();
  62. }
  63. getDefinitions() {
  64. 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 }];
  65. }
  66. /**
  67. * Create new Intersection By Line of Script .geo
  68. * @param {Map} map JavaScript Map
  69. * @param {[]} list List of Generic Objects
  70. */
  71. static do(map, list) {
  72. const id = map.get("id");
  73. const rId = map.get("param")[0];
  74. const sId = map.get("param")[1];
  75. const index = map.get("param")[2] - 1;
  76. const x = (map.get("param")[3]) - 5;
  77. const y = (map.get("param")[4]) + 5;
  78. const visible = map.get("param")[5]
  79. const label = map.get("label");
  80. const r = list.find(x => x.id == rId);
  81. const s = list.find(x => x.id == sId);
  82. return new IntersectionModel(x, y, label, r, s, visible, index, id);
  83. }
  84. }