index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // index.ts
  2. import { getCoord, getCoords } from "@turf/invariant";
  3. function booleanPointOnLine(pt, line, options = {}) {
  4. const ptCoords = getCoord(pt);
  5. const lineCoords = getCoords(line);
  6. for (let i = 0; i < lineCoords.length - 1; i++) {
  7. let ignoreBoundary = false;
  8. if (options.ignoreEndVertices) {
  9. if (i === 0) {
  10. ignoreBoundary = "start";
  11. }
  12. if (i === lineCoords.length - 2) {
  13. ignoreBoundary = "end";
  14. }
  15. if (i === 0 && i + 1 === lineCoords.length - 1) {
  16. ignoreBoundary = "both";
  17. }
  18. }
  19. if (isPointOnLineSegment(
  20. lineCoords[i],
  21. lineCoords[i + 1],
  22. ptCoords,
  23. ignoreBoundary,
  24. typeof options.epsilon === "undefined" ? null : options.epsilon
  25. )) {
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31. function isPointOnLineSegment(lineSegmentStart, lineSegmentEnd, pt, excludeBoundary, epsilon) {
  32. const x = pt[0];
  33. const y = pt[1];
  34. const x1 = lineSegmentStart[0];
  35. const y1 = lineSegmentStart[1];
  36. const x2 = lineSegmentEnd[0];
  37. const y2 = lineSegmentEnd[1];
  38. const dxc = pt[0] - x1;
  39. const dyc = pt[1] - y1;
  40. const dxl = x2 - x1;
  41. const dyl = y2 - y1;
  42. const cross = dxc * dyl - dyc * dxl;
  43. if (epsilon !== null) {
  44. if (Math.abs(cross) > epsilon) {
  45. return false;
  46. }
  47. } else if (cross !== 0) {
  48. return false;
  49. }
  50. if (Math.abs(dxl) === Math.abs(dyl) && Math.abs(dxl) === 0) {
  51. if (excludeBoundary) {
  52. return false;
  53. }
  54. if (pt[0] === lineSegmentStart[0] && pt[1] === lineSegmentStart[1]) {
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. }
  60. if (!excludeBoundary) {
  61. if (Math.abs(dxl) >= Math.abs(dyl)) {
  62. return dxl > 0 ? x1 <= x && x <= x2 : x2 <= x && x <= x1;
  63. }
  64. return dyl > 0 ? y1 <= y && y <= y2 : y2 <= y && y <= y1;
  65. } else if (excludeBoundary === "start") {
  66. if (Math.abs(dxl) >= Math.abs(dyl)) {
  67. return dxl > 0 ? x1 < x && x <= x2 : x2 <= x && x < x1;
  68. }
  69. return dyl > 0 ? y1 < y && y <= y2 : y2 <= y && y < y1;
  70. } else if (excludeBoundary === "end") {
  71. if (Math.abs(dxl) >= Math.abs(dyl)) {
  72. return dxl > 0 ? x1 <= x && x < x2 : x2 < x && x <= x1;
  73. }
  74. return dyl > 0 ? y1 <= y && y < y2 : y2 < y && y <= y1;
  75. } else if (excludeBoundary === "both") {
  76. if (Math.abs(dxl) >= Math.abs(dyl)) {
  77. return dxl > 0 ? x1 < x && x < x2 : x2 < x && x < x1;
  78. }
  79. return dyl > 0 ? y1 < y && y < y2 : y2 < y && y < y1;
  80. }
  81. return false;
  82. }
  83. var turf_boolean_point_on_line_default = booleanPointOnLine;
  84. export {
  85. booleanPointOnLine,
  86. turf_boolean_point_on_line_default as default
  87. };
  88. //# sourceMappingURL=index.js.map