- 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
import pool from "./../system/pooling.js";
import Polygon from "./poly.js";
/**
* @import Vector2d from "./../math/vector2.js";
**/
/**
* @classdesc
* a rectangle Object
* @augments Polygon
*/
export default class Rect extends Polygon {
/**
* @param {number} x - position of the Rectangle
* @param {number} y - position of the Rectangle
* @param {number} w - width of the rectangle
* @param {number} h - height of the rectangle
*/
constructor(x, y, w, h) {
// parent constructor
super(x, y, [
pool.pull("Vector2d", 0, 0), // 0, 0
pool.pull("Vector2d", w, 0), // 1, 0
pool.pull("Vector2d", w, h), // 1, 1
pool.pull("Vector2d", 0, h) // 0, 1
]);
/**
* the shape type (used internally)
* @type {string}
* @default "Rectangle"
*/
this.type = "Rectangle";
}
/** @ignore */
onResetEvent(x, y, w, h) {
this.setShape(x, y, w, h);
}
/**
* set new value to the rectangle shape
* @param {number} x - position of the Rectangle
* @param {number} y - position of the Rectangle
* @param {number|Vector2d[]} w - width of the rectangle, or an array of vector defining the rectangle
* @param {number} [h] - height of the rectangle, if a numeral width parameter is specified
* @returns {Rect} this rectangle
*/
setShape(x, y, w, h) {
let points = w; // assume w is an array by default
this.pos.set(x, y);
if (arguments.length === 4) {
points = this.points;
points[0].set(0, 0); // 0, 0
points[1].set(w, 0); // 1, 0
points[2].set(w, h); // 1, 1
points[3].set(0, h); // 0, 1
}
this.setVertices(points);
return this;
}
/**
* left coordinate of the Rectangle
* @type {number}
*/
get left() {
return this.pos.x;
}
/**
* right coordinate of the Rectangle
* @type {number}
*/
get right() {
let w = this.width;
return (this.left + w) || w;
}
/**
* top coordinate of the Rectangle
* @type {number}
*/
get top() {
return this.pos.y;
}
/**
* bottom coordinate of the Rectangle
* @type {number}
*/
get bottom() {
let h = this.height;
return (this.top + h) || h;
}
/**
* width of the Rectangle
* @type {number}
*/
get width() {
return this.points[2].x;
}
set width(value) {
this.points[1].x = this.points[2].x = value;
this.recalc();
this.updateBounds();
}
/**
* height of the Rectangle
* @type {number}
*/
get height() {
return this.points[2].y;
}
set height(value) {
this.points[2].y = this.points[3].y = value;
this.recalc();
this.updateBounds();
}
/**
* absolute center of this rectangle on the horizontal axis
* @type {number}
*/
get centerX() {
if (isFinite(this.width)) {
return this.left + (this.width / 2);
} else {
return this.width;
}
}
set centerX (value) {
this.pos.x = value - (this.width / 2);
this.recalc();
this.updateBounds();
}
/**
* absolute center of this rectangle on the vertical axis
* @type {number}
*/
get centerY() {
if (isFinite(this.height)) {
return this.top + (this.height / 2);
} else {
return this.height;
}
}
set centerY(value) {
this.pos.y = value - (this.height / 2);
this.recalc();
this.updateBounds();
}
/**
* center the rectangle position around the given coordinates
* @param {number} x - the x coordinate around which to center this rectangle
* @param {number} y - the y coordinate around which to center this rectangle
* @returns {Rect} this rectangle
*/
centerOn(x, y) {
this.centerX = x;
this.centerY = y;
return this;
}
/**
* resize the rectangle
* @param {number} w - new width of the rectangle
* @param {number} h - new height of the rectangle
* @returns {Rect} this rectangle
*/
resize(w, h) {
this.width = w;
this.height = h;
return this;
}
/**
* scale the rectangle
* @param {number} x - a number representing the abscissa of the scaling vector.
* @param {number} [y=x] - a number representing the ordinate of the scaling vector.
* @returns {Rect} this rectangle
*/
scale(x, y = x) {
this.width *= x;
this.height *= y;
return this;
}
/**
* clone this rectangle
* @returns {Rect} new rectangle
*/
clone() {
return new Rect(this.left, this.top, this.width, this.height);
}
/**
* copy the position and size of the given rectangle into this one
* @param {Rect} rect - Source rectangle
* @returns {Rect} new rectangle
*/
copy(rect) {
return this.setShape(rect.left, rect.top, rect.width, rect.height);
}
/**
* merge this rectangle with another one
* @param {Rect} rect - other rectangle to union with
* @returns {Rect} the union(ed) rectangle
*/
union(rect) {
let x1 = Math.min(this.left, rect.left);
let y1 = Math.min(this.top, rect.top);
this.resize(
Math.max(this.right, rect.right) - x1,
Math.max(this.bottom, rect.bottom) - y1
);
this.pos.set(x1, y1);
return this;
}
/**
* check if this rectangle is intersecting with the specified one
* @param {Rect} rect
* @returns {boolean} true if overlaps
*/
overlaps(rect) {
return (
this.left < rect.right &&
rect.left < this.right &&
this.top < rect.bottom &&
rect.top < this.bottom
);
}
/**
* Returns true if the rectangle contains the given point or rectangle
* @param {number|Vector2d|Rect} x - x coordinate or a vector point, or a rectangle to test
* @param {number} [y] - y coordinate
* @returns {boolean} True if the rectangle contain the given point or rectangle, otherwise false
* @example
* if (rect.contains(10, 10)) {
* // do something
* }
* // or
* if (rect.contains(myVector2d)) {
* // do something
* }
* if (rect.contains(myRect)) {
* // do something
* }
*/
contains() {
let arg0 = arguments[0];
let _x1, _x2, _y1, _y2;
if (arguments.length === 2) {
// x, y
_x1 = _x2 = arg0;
_y1 = _y2 = arguments[1];
} else {
// only Rect based objects define a top and bottom properties
if (typeof arg0.bottom === "number") {
// me.Rect
_x1 = arg0.left;
_x2 = arg0.right;
_y1 = arg0.top;
_y2 = arg0.bottom;
} else {
// vector
_x1 = _x2 = arg0.x;
_y1 = _y2 = arg0.y;
}
}
return (
_x1 >= this.left &&
_x2 <= this.right &&
_y1 >= this.top &&
_y2 <= this.bottom
);
}
/**
* check if this rectangle is identical to the specified one
* @param {Rect} rect
* @returns {boolean} true if equals
*/
equals(rect) {
return (
rect.left === this.left &&
rect.right === this.right &&
rect.top === this.top &&
rect.bottom === this.bottom
);
}
/**
* determines whether all coordinates of this rectangle are finite numbers.
* @returns {boolean} false if all coordinates are positive or negative Infinity or NaN; otherwise, true.
*/
isFinite() {
return (isFinite(this.left) && isFinite(this.top) && isFinite(this.width) && isFinite(this.height));
}
/**
* Returns a polygon whose edges are the same as this box.
* @returns {Polygon} a new Polygon that represents this rectangle.
*/
toPolygon() {
return pool.pull("Polygon",
this.left, this.top, this.points
);
}
}