line-model.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * iGeom by LInE
  3. * Geometric Object: Straight Line
  4. * www.matematica.br/igeom
  5. * ./app/components/line-component/models/line-model.js
  6. * Model to Straight Line
  7. */
  8. import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
  9. import { LineSegmentModel } from "../../line-segment-component/models/line-segment-model";
  10. import { IntersectionModel } from "../../intersection-component/models/intersection-model";
  11. const Erro = 0.00001; //TODO a definir...
  12. export class LineModel extends LineSegmentModel {
  13. constructor (pointA, pointB, label, id) {
  14. super(pointA, pointB, label, id);
  15. this.setClass(ELEMENTS_CLASS.LINE);
  16. }
  17. // Overload the Segment 'insideSegment' method (otherwise point intersection could be hiden)
  18. insideSegment (intersecX, intersecY) { //TODO Sempre verificar se esta dentro, nao parece bom...
  19. return true;
  20. }
  21. // Intersection with circunference
  22. getIntersectionWithCircumference (circ) { // circ = circunference(C,radius)
  23. // Delegate to super class Segment
  24. return super.getIntersectionByCircumference(circ);
  25. }
  26. // Intersection between Straigh Lines (SL): with sl
  27. // r := this=SL(A1,A2) e s := SL(B1,B2)
  28. // \ /
  29. // o P=(x,y)
  30. // / \
  31. // o \ o O2 (orthogonal to B2-B1)
  32. // O1 ort. to o / A2 oB1
  33. // A2-A1 O1\ / \
  34. // o oB2
  35. // / A1 \
  36. // this=r s
  37. // d1:=(A2.x-A1.x, A2.y-A1.y) => o1:=(-d1.y, d1.x) : ortogonais as retas r e s
  38. // d2:=(B2.x-B1.x, B2.y-B1.y) => o2:=(-d2.y, d2.x) :
  39. // Find P=(x,y) such that (inner product)
  40. // O1' P = O1' A1 (1)
  41. // O2' P = O2' B1 (2)
  42. // (1) => O1.x * x + O1.y * y = O1.x * A1.x + O1.y * A1.y => O2.y * (O1.x * x + O1.y * y) = O2.y * (O1.x * A1.x + O1.y * A1.y) (3)
  43. // (2) => O2.x * x + O2.y * y = O2.x * B1.x + O2.y * B1.y => O1.y * (O2.x * x + O2.y * y) = O1.y * (O2.x * B1.x + O2.y * B1.y) (4)
  44. //
  45. // (3)-(4) = O2.y * O1.x * x - O1.y * O2.x * x = O2.y * (O1.x * A1.x + O1.y * A1.y) - O1.y * (O2.x * B1.x + O2.y * B1.y)
  46. // = x * (O2.y * O1.x - O1.y * O2.x) = O2.y * (O1.x * A1.x + O1.y * A1.y) - O1.y * (O2.x * B1.x + O2.y * B1.y)
  47. // => x = ( O2.y * (O1.x * A1.x + O1.y * A1.y) - O1.y * (O2.x * B1.x + O2.y * B1.y) ) / (O2.y * O1.x - O1.y * O2.x)
  48. //
  49. // If O1.y<>0 => y = (O1.x * A1.x + O1.y * A1.y - O1.x * x) / O1.y
  50. // otherwise O2.y<>0 => y = (O2.x * B1.x + O2.y * B1.y - O2.x * x) / O2.y
  51. //
  52. // Return: .app/components/intersection-component/models/intersection-model.js
  53. getIntersectionWithStraightLine (sl) { try {//D //leo
  54. const A1 = this.pointA, A2 = this.pointB; // this = SL(A1,A2)
  55. const A3 = sl.pointA, A4 = sl.pointB; // sl = SL(A3, A4)
  56. var A1x=A1.posX, A1y=A1.posY, // A1 -> this
  57. A2x=A2.posX, A2y=A2.posY; // A2 -> this
  58. var B1x=A3.posX, B1y=A3.posY, // B1 -> sl
  59. B2x=A4.posX, B2y=A4.posY; // B2 -> sl
  60. var d1x=A2x-A1x, d1y=A2y-A1y, // d1 = A2-A1
  61. d2x=B2x-B1x, d2y=B2y-B1y, // d2 = B2-B1
  62. O1x=-d1y, O1y=d1x, // O1 = (-d1.y, d1.x) ortogonal a reta r=this
  63. O2x=-d2y, O2y=d2x, // O2 = (-d2.y, d2.x) ortogonal a reta s
  64. x, y; // P=(x,y) ponto procurado
  65. if (O2y * O1x - O1y * O2x == 0) { // "Reta: erro, divisao por zero"
  66. console.log("/app/components/line-component/models/line-model.js: erro: divisao por zero!");
  67. return null;
  68. }
  69. x = ( O2y * (O1x * A1x + O1y * A1y) - O1y * (O2x * B1x + O2y * B1y) ) / (O2y * O1x - O1y * O2x);
  70. if (Math.abs(O1y)>Erro)
  71. y = (O1x * A1x + O1y * A1y - O1x * x) / O1y;
  72. else
  73. if (Math.abs(O2y)>Erro)
  74. y = (O2x * B1x + O2y * B1y - O2x * x) / O2y;
  75. else
  76. if (O1y!=0.0)
  77. y = (O1x * A1x + O1y * A1y - O1x * x) / O1y;
  78. else
  79. if (O2y!=0.0)
  80. y = (O2x * B1x + O2y * B1y - O2x * x) / O2y;
  81. else {
  82. console.log("/app/components/line-component/models/line-model.js: erro: O2y=0!");
  83. return null;
  84. }
  85. //D console.log("line-model.js: getIntersectionWithStraightLine(sl): (" + x + "," + y + ") = " +
  86. //D "inter(r(P("+A1x+","+A1y+"),P("+A2x+","+A2y+")), s(P("+B1x+","+B1y+"),P("+B2x+","+B2y+"))");
  87. return [new IntersectionModel(x, y, undefined, this, sl, true, 0)];
  88. } catch (e) { console.log("app/components/line-component/models/line-model.js: getIntersectionWithStraightLine(.): erro!\n" + e.stack); }
  89. } // getIntersectionWithStraightLine(sl)
  90. // Intersection between Straigh Line (SL) and Segment (S)
  91. // P in S(C,D) <=> P = a C+(1-a)D, a em [0,1] then | Px = Ax+b(Bx-Ax) = a*Cx + (1-a)Dx and Py = Ay+b(By-Ay) = a*Cy + (1-a)Dy and
  92. // P in SL(A,B) <=> P = A + b(B-A) ---> | b = (a*Cx + (1-a)Dx - Ax)/(Bx-Ax)
  93. getIntersectionWithSegment (segm) {
  94. //const pointA = segm.pointA;
  95. //const pointB = segm.pointB;
  96. return segm.getIntersectionByLine(this); //TODO nome em './app/components/line-segment-component/models/line-segment-model.js ! getIntersectionByLine(.)' deveria ser 'getIntersectionWithSegment'
  97. }
  98. // Starting point to intersection started with StraightLine
  99. getIntersection (geometricObject) { try { //D //leo
  100. //D console.log("line-model.js.getIntersection: tipo=" + geometricObject.elementClass);
  101. switch (geometricObject.elementClass) { // ./app/core/enums/elements-class-enum.js: POINT=0; INTERSECTION_POINT=1; CIRCUMFERENCE=3; LINE=4; LINE_SEGMENT=6,...
  102. case ELEMENTS_CLASS.LINE:
  103. return this.getIntersectionWithStraightLine(geometricObject); //TODO melhor 'with' que 'by'
  104. case ELEMENTS_CLASS.LINE_SEGMENT:
  105. return this.getIntersectionWithStraightLine(geometricObject); //TODO melhor 'with' que 'by'
  106. case ELEMENTS_CLASS.CIRCUMFERENCE:
  107. return this.getIntersectionWithCircumference(geometricObject); //TODO melhor 'with' que 'by'
  108. default: break;
  109. }
  110. } catch (e) { console.log("app/components/line-component/models/line-model.js: getIntersection(.): erro!\n" + e.stack); }
  111. }
  112. static do (map, list) {
  113. const id = map.get("id");
  114. const pointAId = map.get("param")[0];
  115. const pointBId = map.get("param")[1];
  116. const pointA = list.find(x => x.id === pointAId);
  117. const pointB = list.find(x => x.id === pointBId);
  118. const label = map.get("label")[0];
  119. return new LineModel(pointA, pointB, label, id);
  120. }
  121. }