index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { getCoords, getType } from '@turf/invariant';
  2. import { featureCollection, point } from '@turf/helpers';
  3. import calcBbox from '@turf/bbox';
  4. import explode from '@turf/explode';
  5. import nearestPoint from '@turf/nearest-point';
  6. /**
  7. * Finds the tangents of a {@link Polygon|(Multi)Polygon} from a {@link Point}.
  8. *
  9. * @name polygonTangents
  10. * @param {Coord} pt to calculate the tangent points from
  11. * @param {Feature<Polygon|MultiPolygon>} polygon to get tangents from
  12. * @returns {FeatureCollection<Point>} Feature Collection containing the two tangent points
  13. * @example
  14. * var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
  15. * var point = turf.point([61, 5]);
  16. *
  17. * var tangents = turf.polygonTangents(point, polygon)
  18. *
  19. * //addToMap
  20. * var addToMap = [tangents, point, polygon];
  21. */
  22. function polygonTangents(pt, polygon) {
  23. var pointCoords = getCoords(pt);
  24. var polyCoords = getCoords(polygon);
  25. var rtan;
  26. var ltan;
  27. var enext;
  28. var eprev;
  29. var bbox = calcBbox(polygon);
  30. var nearestPtIndex = 0;
  31. var nearest = null;
  32. // If the point lies inside the polygon bbox then we need to be a bit trickier
  33. // otherwise points lying inside reflex angles on concave polys can have issues
  34. if (
  35. pointCoords[0] > bbox[0] &&
  36. pointCoords[0] < bbox[2] &&
  37. pointCoords[1] > bbox[1] &&
  38. pointCoords[1] < bbox[3]
  39. ) {
  40. nearest = nearestPoint(pt, explode(polygon));
  41. nearestPtIndex = nearest.properties.featureIndex;
  42. }
  43. var type = getType(polygon);
  44. switch (type) {
  45. case "Polygon":
  46. rtan = polyCoords[0][nearestPtIndex];
  47. ltan = polyCoords[0][0];
  48. if (nearest !== null) {
  49. if (nearest.geometry.coordinates[1] < pointCoords[1])
  50. ltan = polyCoords[0][nearestPtIndex];
  51. }
  52. eprev = isLeft(
  53. polyCoords[0][0],
  54. polyCoords[0][polyCoords[0].length - 1],
  55. pointCoords
  56. );
  57. var out = processPolygon(
  58. polyCoords[0],
  59. pointCoords,
  60. eprev,
  61. enext,
  62. rtan,
  63. ltan);
  64. rtan = out[0];
  65. ltan = out[1];
  66. break;
  67. case "MultiPolygon":
  68. var closestFeature = 0;
  69. var closestVertex = 0;
  70. var verticesCounted = 0;
  71. for (var i = 0; i < polyCoords[0].length; i++) {
  72. closestFeature = i;
  73. var verticeFound = false;
  74. for (var i2 = 0; i2 < polyCoords[0][i].length; i2++) {
  75. closestVertex = i2;
  76. if (verticesCounted === nearestPtIndex) {
  77. verticeFound = true;
  78. break;
  79. }
  80. verticesCounted++;
  81. }
  82. if (verticeFound) break;
  83. }
  84. rtan = polyCoords[0][closestFeature][closestVertex];
  85. ltan = polyCoords[0][closestFeature][closestVertex];
  86. eprev = isLeft(
  87. polyCoords[0][0][0],
  88. polyCoords[0][0][polyCoords[0][0].length - 1],
  89. pointCoords
  90. );
  91. polyCoords.forEach(function (ring) {
  92. var out = processPolygon(
  93. ring[0],
  94. pointCoords,
  95. eprev,
  96. enext,
  97. rtan,
  98. ltan);
  99. rtan = out[0];
  100. ltan = out[1];
  101. });
  102. break;
  103. }
  104. return featureCollection([point(rtan), point(ltan)]);
  105. }
  106. function processPolygon(polygonCoords, ptCoords, eprev, enext, rtan, ltan) {
  107. for (var i = 0; i < polygonCoords.length; i++) {
  108. var currentCoords = polygonCoords[i];
  109. var nextCoordPair = polygonCoords[i + 1];
  110. if (i === polygonCoords.length - 1) {
  111. nextCoordPair = polygonCoords[0];
  112. }
  113. enext = isLeft(currentCoords, nextCoordPair, ptCoords);
  114. if (eprev <= 0 && enext > 0) {
  115. if (!isBelow(ptCoords, currentCoords, rtan)) {
  116. rtan = currentCoords;
  117. }
  118. } else if (eprev > 0 && enext <= 0) {
  119. if (!isAbove(ptCoords, currentCoords, ltan)) {
  120. ltan = currentCoords;
  121. }
  122. }
  123. eprev = enext;
  124. }
  125. return [rtan, ltan];
  126. }
  127. function isAbove(point1, point2, point3) {
  128. return isLeft(point1, point2, point3) > 0;
  129. }
  130. function isBelow(point1, point2, point3) {
  131. return isLeft(point1, point2, point3) < 0;
  132. }
  133. function isLeft(point1, point2, point3) {
  134. return (
  135. (point2[0] - point1[0]) * (point3[1] - point1[1]) -
  136. (point3[0] - point1[0]) * (point2[1] - point1[1])
  137. );
  138. }
  139. export default polygonTangents;