-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
rect.ts
443 lines (386 loc) · 13.4 KB
/
rect.ts
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://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.
*/
/**
* @category core/math
*/
import { CCClass } from '../data/class';
import { ValueType } from '../value-types/value-type';
import { Mat4 } from './mat4';
import { Size } from './size';
import { IRectLike, IVec2Like } from './type-define';
import { Vec2 } from './vec2';
import { legacyCC } from '../global-exports';
/**
* 轴对齐矩形。
* 矩形内的所有点都大于等于矩形的最小点 (xMin, yMin) 并且小于等于矩形的最大点 (xMax, yMax)。
* 矩形的宽度定义为 xMax - xMin;高度定义为 yMax - yMin。
*/
export class Rect extends ValueType {
/**
* 由任意两个点创建一个矩形,目标矩形即是这两个点各向 x、y 轴作线所得到的矩形。
* @param v1 指定的点。
* @param v2 指定的点。
* @returns 目标矩形。
*/
public static fromMinMax <Out extends IRectLike, VecLike extends IVec2Like> (out: Out, v1: VecLike, v2: VecLike) {
const minX = Math.min(v1.x, v2.x);
const minY = Math.min(v1.y, v2.y);
const maxX = Math.max(v1.x, v2.x);
const maxY = Math.max(v1.y, v2.y);
out.x = minX;
out.y = minY;
out.width = maxX - minX;
out.height = maxY - minY;
return out;
}
/**
* 根据指定的插值比率,从当前矩形到目标矩形之间做插值。
* @param out 本方法将插值结果赋值给此参数
* @param from 起始矩形。
* @param to 目标矩形。
* @param ratio 插值比率,范围为 [0,1]。
*/
public static lerp <Out extends IRectLike> (out: Out, from: Out, to: Out, ratio: number) {
const x = from.x;
const y = from.y;
const w = from.width;
const h = from.height;
out.x = x + (to.x - x) * ratio;
out.y = y + (to.y - y) * ratio;
out.width = w + (to.width - w) * ratio;
out.height = h + (to.height - h) * ratio;
return out;
}
/**
* 计算当前矩形与指定矩形重叠部分的矩形,将其赋值给出口矩形。
* @param out 出口矩形。
* @param one 指定的一个矩形。
* @param other 指定的另一个矩形。
*/
public static intersection <Out extends IRectLike> (out: Out, one: Out, other: Out) {
const axMin = one.x;
const ayMin = one.y;
const axMax = one.x + one.width;
const ayMax = one.y + one.height;
const bxMin = other.x;
const byMin = other.y;
const bxMax = other.x + other.width;
const byMax = other.y + other.height;
out.x = Math.max(axMin, bxMin);
out.y = Math.max(ayMin, byMin);
out.width = Math.min(axMax, bxMax) - out.x;
out.height = Math.min(ayMax, byMax) - out.y;
return out;
}
/**
* 创建同时包含当前矩形和指定矩形的最小矩形,将其赋值给出口矩形。
* @param out 出口矩形。
* @param one 指定的一个矩形。
* @param other 指定的另一个矩形。
*/
public static union <Out extends IRectLike> (out: Out, one: Out, other: Out) {
const x = one.x;
const y = one.y;
const w = one.width;
const h = one.height;
const bx = other.x;
const by = other.y;
const bw = other.width;
const bh = other.height;
out.x = Math.min(x, bx);
out.y = Math.min(y, by);
out.width = Math.max(x + w, bx + bw) - out.x;
out.height = Math.max(y + h, by + bh) - out.y;
return out;
}
/**
* 获取或设置矩形在 x 轴上的最小值。
*/
get xMin () {
return this.x;
}
set xMin (value) {
this.width += this.x - value;
this.x = value;
}
/**
* 获取或设置矩形在 y 轴上的最小值。
*/
get yMin () {
return this.y;
}
set yMin (value) {
this.height += this.y - value;
this.y = value;
}
/**
* 获取或设置矩形在 x 轴上的最大值。
*/
get xMax () {
return this.x + this.width;
}
set xMax (value) {
this.width = value - this.x;
}
/**
* 获取或设置矩形在 y 轴上的最大值。
*/
get yMax () {
return this.y + this.height;
}
set yMax (value) {
this.height = value - this.y;
}
/**
* 获取或设置矩形中心点的坐标。
*/
get center () {
return new Vec2(this.x + this.width * 0.5,
this.y + this.height * 0.5);
}
set center (value) {
this.x = value.x - this.width * 0.5;
this.y = value.y - this.height * 0.5;
}
/**
* 获取或设置矩形最小点的坐标。
*/
get origin () {
return new legacyCC.Vec2(this.x, this.y);
}
set origin (value) {
this.x = value.x;
this.y = value.y;
}
/**
* 获取或设置矩形的尺寸。
*/
get size () {
return new Size(this.width, this.height);
}
set size (value) {
this.width = value.width;
this.height = value.height;
}
// compatibility with vector interfaces
set z (val) { this.width = val; }
get z () { return this.width; }
set w (val) { this.height = val; }
get w () { return this.height; }
/**
* 获取或设置矩形最小点的 x 坐标。
*/
public declare x: number;
/**
* 获取或设置矩形最小点的 y 坐标。
*/
public declare y: number;
/**
* 获取或设置矩形的宽度。
*/
public declare width: number;
/**
* 获取或设置矩形的高度。
*/
public declare height: number;
/**
* 构造与指定矩形相等的矩形。
* @param other 相比较的矩形。
*/
constructor (other: Rect);
/**
* 构造具有指定的最小值和尺寸的矩形。
* @param x 矩形在 x 轴上的最小值。
* @param y 矩形在 y 轴上的最小值。
* @param width 矩形的宽度。
* @param height 矩形的高度。
*/
constructor (x?: number, y?: number, width?: number, height?: number);
constructor (x?: Rect | number, y?: number, width?: number, height?: number) {
super();
if (x && typeof x === 'object') {
this.y = x.y;
this.width = x.width;
this.height = x.height;
this.x = x.x;
} else {
this.x = x || 0;
this.y = y || 0;
this.width = width || 0;
this.height = height || 0;
}
}
/**
* 克隆当前矩形。
*/
public clone () {
return new Rect(this.x, this.y, this.width, this.height);
}
/**
* 设置当前矩形使其与指定矩形相等。
* @param other 相比较的矩形。
* @returns `this`
*/
public set (other: Rect);
/**
* 设置当前矩形使其与指定参数的矩形相等。
* @param x 指定矩形的 x 参数
* @param y 指定矩形的 y 参数
* @param width 指定矩形的 width 参数
* @param height 指定矩形的 height 参数
* @returns `this`
*/
public set (x?: number, y?: number, width?: number, height?: number);
public set (x?: Rect | number, y?: number, width?: number, height?: number) {
if (x && typeof x === 'object') {
this.y = x.y;
this.width = x.width;
this.height = x.height;
this.x = x.x;
} else {
this.x = x || 0;
this.y = y || 0;
this.width = width || 0;
this.height = height || 0;
}
return this;
}
/**
* 判断当前矩形是否与指定矩形相等。
* @param other 相比较的矩形。
* @returns 两矩阵的最小值和最大值都分别相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Rect) {
return this.x === other.x &&
this.y === other.y &&
this.width === other.width &&
this.height === other.height;
}
/**
* 根据指定的插值比率,从当前矩形到目标矩形之间做插值。
* @param to 目标矩形。
* @param ratio 插值比率,范围为 [0,1]。
*/
public lerp (to: Rect, ratio: number) {
const x = this.x;
const y = this.y;
const w = this.width;
const h = this.height;
this.x = x + (to.x - x) * ratio;
this.y = y + (to.y - y) * ratio;
this.width = w + (to.width - w) * ratio;
this.height = h + (to.height - h) * ratio;
return this;
}
/**
* 返回当前矩形的字符串表示。
* @returns 当前矩形的字符串表示。
*/
public toString () {
return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)}, ${this.width.toFixed(2)}, ${this.height.toFixed(2)})`;
}
/**
* 判断当前矩形是否与指定矩形相交。
* @param other 相比较的矩形。
* @returns 相交则返回 `true`,否则返回 `false`。
*/
public intersects (other: Rect) {
const maxax = this.x + this.width;
const maxay = this.y + this.height;
const maxbx = other.x + other.width;
const maxby = other.y + other.height;
return !(maxax < other.x || maxbx < this.x || maxay < other.y || maxby < this.y);
}
/**
* 判断当前矩形是否包含指定的点。
* @param point 指定的点。
* @returns 指定的点包含在矩形内则返回 `true`,否则返回 `false`。
*/
public contains (point: Vec2) {
return (this.x <= point.x &&
this.x + this.width >= point.x &&
this.y <= point.y &&
this.y + this.height >= point.y);
}
/**
* 判断当前矩形是否包含指定矩形。
* @param other 指定的矩形。
* @returns 指定矩形所有的点都包含在当前矩形内则返回 `true`,否则返回 `false`。
*/
public containsRect (other: Rect) {
return (this.x <= other.x &&
this.x + this.width >= other.x + other.width &&
this.y <= other.y &&
this.y + this.height >= other.y + other.height);
}
/**
* 应用矩阵变换到当前矩形:
* 应用矩阵变换到当前矩形的最小点得到新的最小点,
* 将当前矩形的尺寸视为二维向量应用矩阵变换得到新的尺寸;
* 并将如此构成的新矩形。
* @param matrix 变换矩阵。
*/
public transformMat4 (mat: Mat4) {
const ol = this.x;
const ob = this.y;
const or = ol + this.width;
const ot = ob + this.height;
const lbx = mat.m00 * ol + mat.m04 * ob + mat.m12;
const lby = mat.m01 * ol + mat.m05 * ob + mat.m13;
const rbx = mat.m00 * or + mat.m04 * ob + mat.m12;
const rby = mat.m01 * or + mat.m05 * ob + mat.m13;
const ltx = mat.m00 * ol + mat.m04 * ot + mat.m12;
const lty = mat.m01 * ol + mat.m05 * ot + mat.m13;
const rtx = mat.m00 * or + mat.m04 * ot + mat.m12;
const rty = mat.m01 * or + mat.m05 * ot + mat.m13;
const minX = Math.min(lbx, rbx, ltx, rtx);
const maxX = Math.max(lbx, rbx, ltx, rtx);
const minY = Math.min(lby, rby, lty, rty);
const maxY = Math.max(lby, rby, lty, rty);
this.x = minX;
this.y = minY;
this.width = maxX - minX;
this.height = maxY - minY;
return this;
}
}
CCClass.fastDefine('cc.Rect', Rect, { x: 0, y: 0, width: 0, height: 0 });
legacyCC.Rect = Rect;
/**
* 构造与指定矩形相等的矩形。等价于 `new Rect(rect)`。
* @param rect 相比较的矩形。
* @returns `new Rect(rect)`
*/
export function rect (rect: Rect): Rect;
/**
* 构造具有指定的最小值和尺寸的矩形,等价于`new Rect(x, y, width, height)`。
* @param x 矩形在 x 轴上的最小值。
* @param y 矩形在 y 轴上的最小值。
* @param width 矩形的宽度。
* @param height 矩形的高度。
* @returns `new Rect(x, y, width, height)`
*/
export function rect (x?: number, y?: number, width?: number, height?: number): Rect;
export function rect (x: Rect | number = 0, y: number = 0, width: number = 0, height: number = 0): Rect {
return new Rect(x as any, y, width, height);
}
legacyCC.rect = rect;