1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.isIntersect = exports.isIntersectRect = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var util_1 = require("@antv/util");
|
6 | var matrix_util_1 = require("@antv/matrix-util");
|
7 | var dot = matrix_util_1.vec2.dot;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function getAxes(points /** 多边形的关键点 */) {
|
13 |
|
14 | if (points.length > 4) {
|
15 | return [];
|
16 | }
|
17 |
|
18 | var vector = function (start, end) {
|
19 | return [end.x - start.x, end.y - start.y];
|
20 | };
|
21 |
|
22 | var AB = vector(points[0], points[1]);
|
23 | var BC = vector(points[1], points[2]);
|
24 | return [AB, BC];
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function rotateAtPoint(point, deg, origin) {
|
32 | if (deg === void 0) { deg = 0; }
|
33 | if (origin === void 0) { origin = { x: 0, y: 0 }; }
|
34 | var x = point.x, y = point.y;
|
35 | return {
|
36 | x: (x - origin.x) * Math.cos(-deg) + (y - origin.y) * Math.sin(-deg) + origin.x,
|
37 | y: (origin.x - x) * Math.sin(-deg) + (y - origin.y) * Math.cos(-deg) + origin.y,
|
38 | };
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function getRectPoints(box) {
|
47 | var points = [
|
48 | { x: box.x, y: box.y },
|
49 | { x: box.x + box.width, y: box.y },
|
50 | { x: box.x + box.width, y: box.y + box.height },
|
51 | { x: box.x, y: box.y + box.height },
|
52 | ];
|
53 | var rotation = box.rotation;
|
54 | if (rotation) {
|
55 | return [
|
56 | rotateAtPoint(points[0], rotation, points[0]),
|
57 | rotateAtPoint(points[1], rotation, points[0]),
|
58 | rotateAtPoint(points[2], rotation, points[0]),
|
59 | rotateAtPoint(points[3], rotation, points[0]),
|
60 | ];
|
61 | }
|
62 | return points;
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function getProjection(points /** 多边形的关键点 */, axis) {
|
72 |
|
73 | if (points.length > 4) {
|
74 | return { min: 0, max: 0 };
|
75 | }
|
76 | var scalars = [];
|
77 | points.forEach(function (point) {
|
78 | scalars.push(dot([point.x, point.y], axis));
|
79 | });
|
80 | return { min: Math.min.apply(Math, scalars), max: Math.max.apply(Math, scalars) };
|
81 | }
|
82 | function isProjectionOverlap(projection1, projection2) {
|
83 | return projection1.max > projection2.min && projection1.min < projection2.max;
|
84 | }
|
85 | function isValidNumber(d) {
|
86 | return (0, util_1.isNumber)(d) && !Number.isNaN(d) && d !== Infinity && d !== -Infinity;
|
87 | }
|
88 | function isValidBox(box) {
|
89 | return Object.values(box).every(isValidNumber);
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 | function isIntersectRect(box1, box2, margin) {
|
95 | if (margin === void 0) { margin = 0; }
|
96 | return !(box2.x > box1.x + box1.width + margin ||
|
97 | box2.x + box2.width < box1.x - margin ||
|
98 | box2.y > box1.y + box1.height + margin ||
|
99 | box2.y + box2.height < box1.y - margin);
|
100 | }
|
101 | exports.isIntersectRect = isIntersectRect;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function isIntersect(box1, box2) {
|
109 |
|
110 | if (!isValidBox(box1) || !isValidBox(box2))
|
111 | return false;
|
112 |
|
113 | if (!box1.rotation && !box2.rotation) {
|
114 | return isIntersectRect(box1, box2);
|
115 | }
|
116 |
|
117 | var rect1Points = getRectPoints(box1);
|
118 | var rect2Points = getRectPoints(box2);
|
119 |
|
120 | var axes = (0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([], getAxes(rect1Points), true), getAxes(rect2Points), true);
|
121 | for (var i = 0; i < axes.length; i++) {
|
122 | var axis = axes[i];
|
123 | var projection1 = getProjection(rect1Points, axis);
|
124 | var projection2 = getProjection(rect2Points, axis);
|
125 |
|
126 | if (!isProjectionOverlap(projection1, projection2))
|
127 | return false;
|
128 | }
|
129 | return true;
|
130 | }
|
131 | exports.isIntersect = isIntersect;
|
132 |
|
\ | No newline at end of file |