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