/
CCIntersection.js
358 lines (289 loc) · 9.6 KB
/
CCIntersection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* !#en Intersection helper class
* !#zh 辅助类,用于测试形状与形状是否相交
* @class Intersection
* @static
*/
var Intersection = {};
/**
* !#en Test line and line
* !#zh 测试线段与线段是否相交
* @method lineLine
* @param {Vec2} a1 - The start point of the first line
* @param {Vec2} a2 - The end point of the first line
* @param {Vec2} b1 - The start point of the second line
* @param {Vec2} b2 - The end point of the second line
* @return {boolean}
*/
function lineLine ( a1, a2, b1, b2 ) {
// jshint camelcase:false
var ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x);
var ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x);
var u_b = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
if ( u_b !== 0 ) {
var ua = ua_t / u_b;
var ub = ub_t / u_b;
if ( 0 <= ua && ua <= 1 && 0 <= ub && ub <= 1 ) {
return true;
}
}
return false;
}
Intersection.lineLine = lineLine;
/**
* !#en Test line and rect
* !#zh 测试线段与矩形是否相交
* @method lineRect
* @param {Vec2} a1 - The start point of the line
* @param {Vec2} a2 - The end point of the line
* @param {Rect} b - The rect
* @return {boolean}
*/
function lineRect ( a1, a2, b ) {
var r0 = new cc.Vec2( b.x, b.y );
var r1 = new cc.Vec2( b.x, b.yMax );
var r2 = new cc.Vec2( b.xMax, b.yMax );
var r3 = new cc.Vec2( b.xMax, b.y );
if ( lineLine( a1, a2, r0, r1 ) )
return true;
if ( lineLine( a1, a2, r1, r2 ) )
return true;
if ( lineLine( a1, a2, r2, r3 ) )
return true;
if ( lineLine( a1, a2, r3, r0 ) )
return true;
return false;
}
Intersection.lineRect = lineRect;
/**
* !#en Test line and polygon
* !#zh 测试线段与多边形是否相交
* @method linePolygon
* @param {Vec2} a1 - The start point of the line
* @param {Vec2} a2 - The end point of the line
* @param {Vec2[]} b - The polygon, a set of points
* @return {boolean}
*/
function linePolygon ( a1, a2, b ) {
var length = b.length;
for ( var i = 0; i < length; ++i ) {
var b1 = b[i];
var b2 = b[(i+1)%length];
if ( lineLine( a1, a2, b1, b2 ) )
return true;
}
return false;
}
Intersection.linePolygon = linePolygon;
/**
* !#en Test rect and rect
* !#zh 测试矩形与矩形是否相交
* @method rectRect
* @param {Rect} a - The first rect
* @param {Rect} b - The second rect
* @return {boolean}
*/
function rectRect ( a, b ) {
// jshint camelcase:false
var a_min_x = a.x;
var a_min_y = a.y;
var a_max_x = a.x + a.width;
var a_max_y = a.y + a.height;
var b_min_x = b.x;
var b_min_y = b.y;
var b_max_x = b.x + b.width;
var b_max_y = b.y + b.height;
return a_min_x <= b_max_x &&
a_max_x >= b_min_x &&
a_min_y <= b_max_y &&
a_max_y >= b_min_y
;
}
Intersection.rectRect = rectRect;
/**
* !#en Test rect and polygon
* !#zh 测试矩形与多边形是否相交
* @method rectPolygon
* @param {Rect} a - The rect
* @param {Vec2[]} b - The polygon, a set of points
* @return {boolean}
*/
function rectPolygon ( a, b ) {
var i, l;
var r0 = new cc.Vec2( a.x, a.y );
var r1 = new cc.Vec2( a.x, a.yMax );
var r2 = new cc.Vec2( a.xMax, a.yMax );
var r3 = new cc.Vec2( a.xMax, a.y );
// intersection check
if ( linePolygon( r0, r1, b ) )
return true;
if ( linePolygon( r1, r2, b ) )
return true;
if ( linePolygon( r2, r3, b ) )
return true;
if ( linePolygon( r3, r0, b ) )
return true;
// check if a contains b
for ( i = 0, l = b.length; i < l; ++i ) {
if ( pointInPolygon(b[i], a) )
return true;
}
// check if b contains a
if ( pointInPolygon(r0, b) )
return true;
if ( pointInPolygon(r1, b) )
return true;
if ( pointInPolygon(r2, b) )
return true;
if ( pointInPolygon(r3, b) )
return true;
return false;
}
Intersection.rectPolygon = rectPolygon;
/**
* !#en Test polygon and polygon
* !#zh 测试多边形与多边形是否相交
* @method polygonPolygon
* @param {Vec2[]} a - The first polygon, a set of points
* @param {Vec2[]} b - The second polygon, a set of points
* @return {boolean}
*/
function polygonPolygon ( a, b ) {
var i, l;
// check if a intersects b
for ( i = 0, l = a.length; i < l; ++i ) {
var a1 = a[i];
var a2 = a[(i+1)%l];
if ( linePolygon( a1, a2, b ) )
return true;
}
// check if a contains b
for ( i = 0, l = b.length; i < l; ++i ) {
if ( pointInPolygon(b[i], a) )
return true;
}
// check if b contains a
for ( i = 0, l = a.length; i < l; ++i ) {
if ( pointInPolygon( a[i], b ) )
return true;
}
return false;
}
Intersection.polygonPolygon = polygonPolygon;
/**
* !#en Test circle and circle
* !#zh 测试圆形与圆形是否相交
* @method circleCircle
* @param {Object} a - Object contains position and radius
* @param {Object} b - Object contains position and radius
* @return {boolean}
* @typescript circleCircle(a: {position: Vec2, radius: number}, b: {position: Vec2, radius: number}): boolean
*/
function circleCircle (a, b) {
var distance = a.position.sub(b.position).mag();
return distance < (a.radius + b.radius);
}
Intersection.circleCircle = circleCircle;
/**
* !#en Test polygon and circle
* !#zh 测试矩形与圆形是否相交
* @method polygonCircle
* @param {Vec2[]} polygon - The Polygon, a set of points
* @param {Object} circle - Object contains position and radius
* @return {boolean}
* @typescript polygonCircle(polygon: Vec2[], circle: {position: Vec2, radius: number}): boolean
*/
function polygonCircle (polygon, circle) {
var position = circle.position;
if (pointInPolygon(position, polygon)) {
return true;
}
for (var i = 0, l = polygon.length; i < l; i++) {
var start = i === 0 ? polygon[polygon.length - 1] : polygon[i- 1];
var end = polygon[i];
if (pointLineDistance(position, start, end, true) < circle.radius) {
return true;
}
}
return false;
}
Intersection.polygonCircle = polygonCircle;
/**
* !#en Test whether the point is in the polygon
* !#zh 测试一个点是否在一个多边形中
* @method pointInPolygon
* @param {Vec2} point - The point
* @param {Vec2[]} polygon - The polygon, a set of points
* @return {boolean}
*/
function pointInPolygon (point, polygon) {
var inside = false;
var x = point.x;
var y = point.y;
// use some raycasting to test hits
// https://github.com/substack/point-in-polygon/blob/master/index.js
var length = polygon.length;
for ( var i = 0, j = length-1; i < length; j = i++ ) {
var xi = polygon[i].x, yi = polygon[i].y,
xj = polygon[j].x, yj = polygon[j].y,
intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if ( intersect ) inside = !inside;
}
return inside;
}
Intersection.pointInPolygon = pointInPolygon;
/**
* !#en Calculate the distance of point to line.
* !#zh 计算点到直线的距离。如果这是一条线段并且垂足不在线段内,则会计算点到线段端点的距离。
* @method pointLineDistance
* @param {Vec2} point - The point
* @param {Vec2} start - The start point of line
* @param {Vec2} end - The end point of line
* @param {boolean} isSegment - whether this line is a segment
* @return {number}
*/
function pointLineDistance(point, start, end, isSegment) {
var dx = end.x - start.x;
var dy = end.y - start.y;
var d = dx*dx + dy*dy;
var t = ((point.x - start.x) * dx + (point.y - start.y) * dy) / d;
var p;
if (!isSegment) {
p = cc.v2(start.x + t * dx, start.y + t * dy);
}
else {
if (d) {
if (t < 0) p = start;
else if (t > 1) p = end;
else p = cc.v2(start.x + t * dx, start.y + t * dy);
}
else {
p = start;
}
}
dx = point.x - p.x;
dy = point.y - p.y;
return Math.sqrt(dx*dx + dy*dy);
}
Intersection.pointLineDistance = pointLineDistance;
cc.Intersection = module.exports = Intersection;