is-polygons-intersect.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import isPointInPolygon from './point-in-polygon';
  2. import getLineIntersect from './get-line-intersect';
  3. import { each } from 'lodash-es';
  4. function parseToLines(points) {
  5. var lines = [];
  6. var count = points.length;
  7. for (var i = 0; i < count - 1; i++) {
  8. var point = points[i];
  9. var next = points[i + 1];
  10. lines.push({
  11. from: {
  12. x: point[0],
  13. y: point[1],
  14. },
  15. to: {
  16. x: next[0],
  17. y: next[1],
  18. },
  19. });
  20. }
  21. if (lines.length > 1) {
  22. var first = points[0];
  23. var last = points[count - 1];
  24. lines.push({
  25. from: {
  26. x: last[0],
  27. y: last[1],
  28. },
  29. to: {
  30. x: first[0],
  31. y: first[1],
  32. },
  33. });
  34. }
  35. return lines;
  36. }
  37. function lineIntersectPolygon(lines, line) {
  38. var isIntersect = false;
  39. each(lines, function (l) {
  40. if (getLineIntersect(l.from, l.to, line.from, line.to)) {
  41. isIntersect = true;
  42. return false;
  43. }
  44. });
  45. return isIntersect;
  46. }
  47. function getBBox(points) {
  48. var xArr = points.map(function (p) { return p[0]; });
  49. var yArr = points.map(function (p) { return p[1]; });
  50. return {
  51. minX: Math.min.apply(null, xArr),
  52. maxX: Math.max.apply(null, xArr),
  53. minY: Math.min.apply(null, yArr),
  54. maxY: Math.max.apply(null, yArr),
  55. };
  56. }
  57. function intersectBBox(box1, box2) {
  58. return !(box2.minX > box1.maxX || box2.maxX < box1.minX || box2.minY > box1.maxY || box2.maxY < box1.minY);
  59. }
  60. export default function isPolygonsIntersect(points1, points2) {
  61. // 空数组,或者一个点返回 false
  62. if (points1.length < 2 || points2.length < 2) {
  63. return false;
  64. }
  65. var bbox1 = getBBox(points1);
  66. var bbox2 = getBBox(points2);
  67. // 判定包围盒是否相交,比判定点是否在多边形内要快的多,可以筛选掉大多数情况
  68. if (!intersectBBox(bbox1, bbox2)) {
  69. return false;
  70. }
  71. var isIn = false;
  72. // 判定点是否在多边形内部,一旦有一个点在另一个多边形内,则返回
  73. each(points2, function (point) {
  74. if (isPointInPolygon(points1, point[0], point[1])) {
  75. isIn = true;
  76. return false;
  77. }
  78. });
  79. if (isIn) {
  80. return true;
  81. }
  82. // 两个多边形都需要判定
  83. each(points1, function (point) {
  84. if (isPointInPolygon(points2, point[0], point[1])) {
  85. isIn = true;
  86. return false;
  87. }
  88. });
  89. if (isIn) {
  90. return true;
  91. }
  92. var lines1 = parseToLines(points1);
  93. var lines2 = parseToLines(points2);
  94. var isIntersect = false;
  95. each(lines2, function (line) {
  96. if (lineIntersectPolygon(lines1, line)) {
  97. isIntersect = true;
  98. return false;
  99. }
  100. });
  101. return isIntersect;
  102. }
  103. //# sourceMappingURL=is-polygons-intersect.js.map