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