@next2d/geom 1.14.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,513 @@
1
+ import { Point } from "./Point";
2
+ import { $Math, $clamp, $SHORT_INT_MIN, $SHORT_INT_MAX } from "@next2d/share";
3
+ /**
4
+ * Rectangle オブジェクトは、その位置(左上隅のポイント (x, y) で示される)、および幅と高さで定義される領域です。
5
+ * Rectangle クラスの x、y、width、および height の各プロパティは、互いに独立しているため、
6
+ * あるプロパティの値を変更しても、他のプロパティに影響はありません。
7
+ * ただし、right プロパティと bottom プロパティはこれら 4 つのプロパティと不可分に関連しています。
8
+ * 例えば、right プロパティの値を変更すると width プロパティの値も変更されます。
9
+ * bottom プロパティの値を変更すると、height プロパティの値も変更されます。
10
+ *
11
+ * A Rectangle object is an area defined by its position,
12
+ * as indicated by its top-left corner point (x, y) and by its width and its height.
13
+ * The x, y, width, and height properties of the Rectangle class are independent of each other;
14
+ * changing the value of one property has no effect on the others. However,
15
+ * the right and bottom properties are integrally related to those four properties.
16
+ * For example, if you change the value of the right property, the value of the width property changes;
17
+ * if you change the bottom property, the value of the height property changes.
18
+ *
19
+ * @example <caption>Example usage of Rectangle.</caption>
20
+ * // new Rectangle
21
+ * const {Rectangle} = next2d.geom;
22
+ * const rectangle = new Rectangle(0, 0, 100, 100);
23
+ *
24
+ * @class
25
+ * @memberOf next2d.geom
26
+ */
27
+ export class Rectangle {
28
+ /**
29
+ * @param {number} [x=0]
30
+ * @param {number} [y=0]
31
+ * @param {number} [width=0]
32
+ * @param {number} [height=0]
33
+ *
34
+ * @constructor
35
+ * @public
36
+ */
37
+ constructor(x = 0, y = 0, width = 0, height = 0) {
38
+ /**
39
+ * @type {number}
40
+ * @default 0
41
+ * @private
42
+ */
43
+ this._$x = 0;
44
+ /**
45
+ * @type {number}
46
+ * @default 0
47
+ * @private
48
+ */
49
+ this._$y = 0;
50
+ /**
51
+ * @type {number}
52
+ * @default 0
53
+ * @private
54
+ */
55
+ this._$width = 0;
56
+ /**
57
+ * @type {number}
58
+ * @default 0
59
+ * @private
60
+ */
61
+ this._$height = 0;
62
+ // init
63
+ this.setTo(x, y, width, height);
64
+ }
65
+ /**
66
+ * 指定されたクラスのストリングを返します。
67
+ * Returns the string representation of the specified class.
68
+ *
69
+ * @return {string}
70
+ * @default [class Rectangle]
71
+ * @method
72
+ * @static
73
+ */
74
+ static toString() {
75
+ return "[class Rectangle]";
76
+ }
77
+ /**
78
+ * @description 指定されたクラスの空間名を返します。
79
+ * Returns the space name of the specified class.
80
+ *
81
+ * @member {string}
82
+ * @default next2d.geom.Rectangle
83
+ * @const
84
+ * @static
85
+ */
86
+ static get namespace() {
87
+ return "next2d.geom.Rectangle";
88
+ }
89
+ /**
90
+ * @description 指定されたオブジェクトのストリングを返します。
91
+ * Returns the string representation of the specified object.
92
+ *
93
+ * @return {string}
94
+ * @method
95
+ * @public
96
+ */
97
+ toString() {
98
+ return `(x=${this.x}, y=${this.y}, w=${this.width}, h=${this.height})`;
99
+ }
100
+ /**
101
+ * @description 指定されたオブジェクトの空間名を返します。
102
+ * Returns the space name of the specified object.
103
+ *
104
+ * @member {string}
105
+ * @default next2d.geom.Rectangle
106
+ * @const
107
+ * @public
108
+ */
109
+ get namespace() {
110
+ return "next2d.geom.Rectangle";
111
+ }
112
+ /**
113
+ * @description y プロパティと height プロパティの合計です。
114
+ * The sum of the y and height properties.
115
+ *
116
+ * @member {number}
117
+ * @public
118
+ */
119
+ get bottom() {
120
+ return this.y + this.height;
121
+ }
122
+ set bottom(bottom) {
123
+ this.height = +bottom - this.y;
124
+ }
125
+ /**
126
+ * @description Rectangle オブジェクトの右下隅の位置で、
127
+ * right プロパティと bottom プロパティの値で決まります。
128
+ * The location of the Rectangle object's bottom-right corner,
129
+ * determined by the values of the right and bottom properties.
130
+ *
131
+ * @member {Point}
132
+ * @public
133
+ */
134
+ get bottomRight() {
135
+ return new Point(this.right, this.bottom);
136
+ }
137
+ set bottomRight(point) {
138
+ this.right = point.x;
139
+ this.bottom = point.y;
140
+ }
141
+ /**
142
+ * @description 矩形の高さ(ピクセル単位)です。
143
+ * The height of the rectangle, in pixels.
144
+ *
145
+ * @member {number}
146
+ * @public
147
+ */
148
+ get height() {
149
+ return this._$height;
150
+ }
151
+ set height(height) {
152
+ this._$height = $clamp(+height, $SHORT_INT_MIN, $SHORT_INT_MAX, 0);
153
+ }
154
+ /**
155
+ * @description 矩形の左上隅の x 座標です。
156
+ * The x coordinate of the top-left corner of the rectangle.
157
+ *
158
+ * @member {number}
159
+ * @public
160
+ */
161
+ get left() {
162
+ return this.x;
163
+ }
164
+ set left(left) {
165
+ this.width = this.right - +left;
166
+ this.x = left;
167
+ }
168
+ /**
169
+ * @description x プロパティと width プロパティの合計です。
170
+ * The sum of the x and width properties.
171
+ *
172
+ * @member {number}
173
+ * @public
174
+ */
175
+ get right() {
176
+ return this.x + this.width;
177
+ }
178
+ set right(right) {
179
+ this.width = +right - this.x;
180
+ }
181
+ /**
182
+ * @description Rectangle オブジェクトのサイズで、
183
+ * width プロパティと height プロパティの値を持つ Point オブジェクトとして表現されます。
184
+ * The size of the Rectangle object,
185
+ * expressed as a Point object with the values of the width and height properties.
186
+ *
187
+ * @member {Point}
188
+ * @public
189
+ */
190
+ get size() {
191
+ return new Point(this.width, this.height);
192
+ }
193
+ set size(point) {
194
+ this.width = point.x;
195
+ this.height = point.y;
196
+ }
197
+ /**
198
+ * @description 矩形の左上隅の y 座標です。
199
+ * The y coordinate of the top-left corner of the rectangle.
200
+ *
201
+ * @member {number}
202
+ * @public
203
+ */
204
+ get top() {
205
+ return this.y;
206
+ }
207
+ set top(top) {
208
+ this.height = +(this.bottom - +top);
209
+ this.y = top;
210
+ }
211
+ /**
212
+ * @description Rectangle オブジェクトの左上隅の位置で、
213
+ * そのポイントの x 座標と y 座標で決まります。
214
+ * The location of the Rectangle object's top-left corner,
215
+ * determined by the x and y coordinates of the point.
216
+ *
217
+ * @member {Point}
218
+ * @public
219
+ */
220
+ get topLeft() {
221
+ return new Point(this.x, this.y);
222
+ }
223
+ set topLeft(point) {
224
+ this.left = point.x;
225
+ this.top = point.y;
226
+ }
227
+ /**
228
+ * @description 矩形の幅(ピクセル単位)です。
229
+ * The width of the rectangle, in pixels.
230
+ *
231
+ * @member {number}
232
+ * @public
233
+ */
234
+ get width() {
235
+ return this._$width;
236
+ }
237
+ set width(width) {
238
+ this._$width = $clamp(+width, $SHORT_INT_MIN, $SHORT_INT_MAX, 0);
239
+ }
240
+ /**
241
+ * @description 矩形の左上隅の x 座標です。
242
+ * The x coordinate of the top-left corner of the rectangle.
243
+ *
244
+ * @member {number}
245
+ * @public
246
+ */
247
+ get x() {
248
+ return this._$x;
249
+ }
250
+ set x(x) {
251
+ this._$x = $clamp(+x, $SHORT_INT_MIN, $SHORT_INT_MAX, 0);
252
+ }
253
+ /**
254
+ * @description 矩形の左上隅の y 座標です。
255
+ * The y coordinate of the top-left corner of the rectangle.
256
+ *
257
+ * @member {number}
258
+ * @public
259
+ */
260
+ get y() {
261
+ return this._$y;
262
+ }
263
+ set y(y) {
264
+ this._$y = $clamp(+y, $SHORT_INT_MIN, $SHORT_INT_MAX, 0);
265
+ }
266
+ /**
267
+ * @description 元の Rectangle オブジェクトと x、y、width、および height の各プロパティの値が同じである、
268
+ * 新しい Rectangle オブジェクトを返します。
269
+ * Returns a new Rectangle object with the same values for the x, y, width,
270
+ * and height properties as the original Rectangle object.
271
+ *
272
+ * @return {Rectangle}
273
+ * @method
274
+ * @public
275
+ */
276
+ clone() {
277
+ return new Rectangle(this.x, this.y, this.width, this.height);
278
+ }
279
+ /**
280
+ * @description 指定されたポイントがこの Rectangle オブジェクトで定義される矩形領域内にあるかどうかを判別します。
281
+ * Determines whether the specified point is contained within
282
+ * the rectangular region defined by this Rectangle object.
283
+ *
284
+ * @param {number} x
285
+ * @param {number} y
286
+ * @return {boolean}
287
+ * @method
288
+ * @public
289
+ */
290
+ contains(x, y) {
291
+ return this.x <= x && this.y <= y && this.right > x && this.bottom > y;
292
+ }
293
+ /**
294
+ * @description 指定されたポイントがこの Rectangle オブジェクトで定義される矩形領域内にあるかどうかを判別します。
295
+ * Determines whether the specified point is contained within
296
+ * the rectangular region defined by this Rectangle object.
297
+ *
298
+ * @param {Point} point
299
+ * @return {boolean}
300
+ * @method
301
+ * @public
302
+ */
303
+ containsPoint(point) {
304
+ return this.x <= point.x && this.y <= point.y &&
305
+ this.right > point.x && this.bottom > point.y;
306
+ }
307
+ /**
308
+ * @description rect パラメーターで指定された Rectangle オブジェクトがこの Rectangle オブジェクト内にあるかどうかを判別します。
309
+ * Determines whether the Rectangle object specified by
310
+ * the rect parameter is contained within this Rectangle object.
311
+ *
312
+ * @param {Rectangle} rect
313
+ * @return {boolean}
314
+ * @method
315
+ * @public
316
+ */
317
+ containsRect(rect) {
318
+ return this.x <= rect.x && this.y <= rect.y &&
319
+ this.right >= rect.right && this.bottom >= rect.bottom;
320
+ }
321
+ /**
322
+ * @description すべての矩形データを、ソース Rectangle オブジェクトから、
323
+ * 呼び出し元の Rectangle オブジェクトにコピーします。
324
+ * Copies all of rectangle data from
325
+ * the source Rectangle object into the calling Rectangle object.
326
+ *
327
+ * @param {Rectangle} source_rect
328
+ * @return {void}
329
+ * @method
330
+ * @public
331
+ */
332
+ copyFrom(source_rect) {
333
+ this.x = source_rect.x;
334
+ this.y = source_rect.y;
335
+ this.width = source_rect.width;
336
+ this.height = source_rect.height;
337
+ }
338
+ /**
339
+ * @description toCompare パラメーターで指定されたオブジェクトが
340
+ * この Rectangle オブジェクトと等しいかどうかを判別します。
341
+ * Determines whether the object specified
342
+ * in the toCompare parameter is equal to this Rectangle object.
343
+ *
344
+ * @param {Rectangle} to_compare
345
+ * @return {boolean}
346
+ * @method
347
+ * @public
348
+ */
349
+ equals(to_compare) {
350
+ return this.x === to_compare.x && this.y === to_compare.y &&
351
+ this.width === to_compare.width && this.height === to_compare.height;
352
+ }
353
+ /**
354
+ * @description Rectangle オブジェクトのサイズを、指定された量(ピクセル単位)だけ大きくします。
355
+ * Increases the size of the Rectangle object by the specified amounts, in pixels.
356
+ *
357
+ * @param {number} dx
358
+ * @param {number} dy
359
+ * @return void
360
+ * @method
361
+ * @public
362
+ */
363
+ inflate(dx, dy) {
364
+ this.x = this.x - +dx;
365
+ this.width = this.width + 2 * +dx;
366
+ this.y = this.y - +dy;
367
+ this.height = this.height + 2 * +dy;
368
+ }
369
+ /**
370
+ * @description Rectangle オブジェクトのサイズを大きくします。
371
+ * Increases the size of the Rectangle object.
372
+ *
373
+ * @param {Point} point
374
+ * @return {void}
375
+ * @method
376
+ * @public
377
+ */
378
+ inflatePoint(point) {
379
+ this.x = this.x - point.x;
380
+ this.width = this.width + 2 * point.x;
381
+ this.y = this.y - point.y;
382
+ this.height = this.height + 2 * point.y;
383
+ }
384
+ /**
385
+ * @description toIntersect パラメーターで指定された Rectangle オブジェクトが
386
+ * この Rectangle オブジェクトと交差する場合に、交差領域を Rectangle オブジェクトとして返します。
387
+ * If the Rectangle object specified in the toIntersect parameter intersects
388
+ * with this Rectangle object, returns the area of intersection as a Rectangle object.
389
+ *
390
+ * @param {Rectangle} to_intersect
391
+ * @return {Rectangle}
392
+ * @method
393
+ * @public
394
+ */
395
+ intersection(to_intersect) {
396
+ const sx = $Math.max(this.x, to_intersect.x);
397
+ const sy = $Math.max(this.y, to_intersect.y);
398
+ const ex = $Math.min(this.right, to_intersect.right);
399
+ const ey = $Math.min(this.bottom, to_intersect.bottom);
400
+ const w = ex - sx;
401
+ const h = ey - sy;
402
+ return w > 0 && h > 0 ? new Rectangle(sx, sy, w, h) : new Rectangle(0, 0, 0, 0);
403
+ }
404
+ /**
405
+ * @description toIntersect パラメーターで指定されたオブジェクトが
406
+ * この Rectangle オブジェクトと交差するかどうかを判別します。
407
+ * Determines whether the object specified
408
+ * in the toIntersect parameter intersects with this Rectangle object.
409
+ *
410
+ * @param {Rectangle} to_intersect
411
+ * @return {boolean}
412
+ * @method
413
+ * @public
414
+ */
415
+ intersects(to_intersect) {
416
+ const sx = $Math.max(this.x, to_intersect.x);
417
+ const sy = $Math.max(this.y, to_intersect.y);
418
+ const ex = $Math.min(this.right, to_intersect.right);
419
+ const ey = $Math.min(this.bottom, to_intersect.bottom);
420
+ return ex - sx > 0 && ey - sy > 0;
421
+ }
422
+ /**
423
+ * @description この Rectangle オブジェクトが空かどうかを判別します。
424
+ * Determine if this Rectangle object is empty.
425
+ *
426
+ * @return {boolean}
427
+ * @method
428
+ * @public
429
+ */
430
+ isEmpty() {
431
+ return this.width <= 0 || this.height <= 0;
432
+ }
433
+ /**
434
+ * @description Rectangle オブジェクトの位置(左上隅で決定される)を、指定された量だけ調整します。
435
+ * Adjusts the location of the Rectangle object,
436
+ * as determined by its top-left corner, by the specified amounts.
437
+ *
438
+ * @param {number} dx
439
+ * @param {number} dy
440
+ * @return {void}
441
+ * @method
442
+ * @public
443
+ */
444
+ offset(dx, dy) {
445
+ this.x += dx;
446
+ this.y += dy;
447
+ }
448
+ /**
449
+ * @description Point オブジェクトをパラメーターとして使用して、Rectangle オブジェクトの位置を調整します。
450
+ * Adjusts the location of the Rectangle object using a Point object as a parameter.
451
+ *
452
+ * @param {Point} point
453
+ * @return {void}
454
+ * @method
455
+ * @public
456
+ */
457
+ offsetPoint(point) {
458
+ this.x += point.x;
459
+ this.y += point.y;
460
+ }
461
+ /**
462
+ * @description Rectangle オブジェクトのすべてのプロパティを 0 に設定します。
463
+ * Sets all properties of the Rectangle object to 0.
464
+ *
465
+ * @return {void}
466
+ * @method
467
+ * @public
468
+ */
469
+ setEmpty() {
470
+ this._$x = 0;
471
+ this._$y = 0;
472
+ this._$width = 0;
473
+ this._$height = 0;
474
+ }
475
+ /**
476
+ * @description Rectangle のメンバーを指定の値に設定します。
477
+ * Sets the members of Rectangle to the specified values
478
+ *
479
+ * @param {number} x
480
+ * @param {number} y
481
+ * @param {number} width
482
+ * @param {number} height
483
+ * @return {void}
484
+ * @method
485
+ * @public
486
+ */
487
+ setTo(x, y, width, height) {
488
+ this.x = x;
489
+ this.y = y;
490
+ this.width = width;
491
+ this.height = height;
492
+ }
493
+ /**
494
+ * @description 2 つの矩形間の水平と垂直の空間を塗りつぶすことにより、
495
+ * 2 つの矩形を加算して新しい Rectangle オブジェクトを作成します。
496
+ * Adds two rectangles together to create a new Rectangle object,
497
+ * by filling in the horizontal and vertical space between the two rectangles.
498
+ *
499
+ * @param {Rectangle} to_union
500
+ * @return {Rectangle}
501
+ * @method
502
+ * @public
503
+ */
504
+ union(to_union) {
505
+ if (this.isEmpty()) {
506
+ return to_union.clone();
507
+ }
508
+ if (to_union.isEmpty()) {
509
+ return this.clone();
510
+ }
511
+ return new Rectangle($Math.min(this.x, to_union.x), $Math.min(this.y, to_union.y), $Math.max(this.right - to_union.left, to_union.right - this.left), $Math.max(this.bottom - to_union.top, to_union.bottom - this.top));
512
+ }
513
+ }