Polyline.Intersect.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. L.Polyline.include({
  2. // Check to see if this polyline has any linesegments that intersect.
  3. // NOTE: does not support detecting intersection for degenerate cases.
  4. intersects: function () {
  5. var points = this._originalPoints,
  6. len = points ? points.length : 0,
  7. i, p, p1;
  8. if (this._tooFewPointsForIntersection()) {
  9. return false;
  10. }
  11. for (i = len - 1; i >= 3; i--) {
  12. p = points[i - 1];
  13. p1 = points[i];
  14. if (this._lineSegmentsIntersectsRange(p, p1, i - 2)) {
  15. return true;
  16. }
  17. }
  18. return false;
  19. },
  20. // Check for intersection if new latlng was added to this polyline.
  21. // NOTE: does not support detecting intersection for degenerate cases.
  22. newLatLngIntersects: function (latlng, skipFirst) {
  23. // Cannot check a polyline for intersecting lats/lngs when not added to the map
  24. if (!this._map) {
  25. return false;
  26. }
  27. return this.newPointIntersects(this._map.latLngToLayerPoint(latlng), skipFirst);
  28. },
  29. // Check for intersection if new point was added to this polyline.
  30. // newPoint must be a layer point.
  31. // NOTE: does not support detecting intersection for degenerate cases.
  32. newPointIntersects: function (newPoint, skipFirst) {
  33. var points = this._originalPoints,
  34. len = points ? points.length : 0,
  35. lastPoint = points ? points[len - 1] : null,
  36. // The previous previous line segment. Previous line segement doesn't need testing.
  37. maxIndex = len - 2;
  38. if (this._tooFewPointsForIntersection(1)) {
  39. return false;
  40. }
  41. return this._lineSegmentsIntersectsRange(lastPoint, newPoint, maxIndex, skipFirst ? 1 : 0);
  42. },
  43. // Polylines with 2 sides can only intersect in cases where points are collinear (we don't support detecting these).
  44. // Cannot have intersection when < 3 line segments (< 4 points)
  45. _tooFewPointsForIntersection: function (extraPoints) {
  46. var points = this._originalPoints,
  47. len = points ? points.length : 0;
  48. // Increment length by extraPoints if present
  49. len += extraPoints || 0;
  50. return !this._originalPoints || len <= 3;
  51. },
  52. // Checks a line segment intersections with any line segements before its predecessor.
  53. // Don't need to check the predecessor as will never intersect.
  54. _lineSegmentsIntersectsRange: function (p, p1, maxIndex, minIndex) {
  55. var points = this._originalPoints,
  56. p2, p3;
  57. minIndex = minIndex || 0;
  58. // Check all previous line segments (beside the immediately previous) for intersections
  59. for (var j = maxIndex; j > minIndex; j--) {
  60. p2 = points[j - 1];
  61. p3 = points[j];
  62. if (L.LineUtil.segmentsIntersect(p, p1, p2, p3)) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. });