line-segment-model.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { GeometricObject } from "../../../core/models/objects/geometric-object";
  2. import { IntersectionModel } from "../../intersection-component/models/intersection-model";
  3. export class LineSegmentModel extends GeometricObject {
  4. constructor(pointA, pointB, label) {
  5. super();
  6. this.pointA = pointA;
  7. this.pointB = pointB;
  8. this.setLabel(label);
  9. }
  10. getStraight() {
  11. const aX = this.pointA.posX;
  12. const aY = this.pointA.posY;
  13. const bX = this.pointB.posX;
  14. const bY = this.pointB.posY;
  15. const a = bY - aY;
  16. const b = aX - bX;
  17. const c = a * aX + b * aY;
  18. return [a, b, c];
  19. }
  20. getIntersection(lineSegment) {
  21. const r = this.getStraight();
  22. const s = lineSegment.getStraight();
  23. const a1 = r[0];
  24. const b1 = r[1];
  25. const c1 = r[2];
  26. const a2 = s[0];
  27. const b2 = s[1];
  28. const c2 = s[2];
  29. const determinant = a1 * b2 - a2 * b1;
  30. if (determinant == 0) {
  31. return new IntersectionModel(
  32. Number.MAX_SAFE_INTEGER,
  33. Number.MAX_SAFE_INTEGER,
  34. undefined,
  35. this,
  36. lineSegment
  37. );
  38. } else {
  39. const x = (b2 * c1 - b1 * c2) / determinant;
  40. const y = (a1 * c2 - a2 * c1) / determinant;
  41. return new IntersectionModel(x, y, undefined, this, lineSegment);
  42. }
  43. }
  44. }