is-polygons-intersect.js 3.1 KB

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