Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/math/rect.js
4509 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A utility class for representing rectangles. Some of these
9
* functions should be migrated over to non-nullable params.
10
*/
11
12
goog.provide('goog.math.Rect');
13
14
goog.require('goog.asserts');
15
goog.require('goog.math.Box');
16
goog.require('goog.math.Coordinate');
17
goog.require('goog.math.IRect');
18
goog.require('goog.math.Size');
19
20
21
22
/**
23
* Class for representing rectangular regions.
24
* @param {number} x Left.
25
* @param {number} y Top.
26
* @param {number} w Width.
27
* @param {number} h Height.
28
* @struct
29
* @constructor
30
* @implements {goog.math.IRect}
31
*/
32
goog.math.Rect = function(x, y, w, h) {
33
'use strict';
34
/** @type {number} */
35
this.left = x;
36
37
/** @type {number} */
38
this.top = y;
39
40
/** @type {number} */
41
this.width = w;
42
43
/** @type {number} */
44
this.height = h;
45
};
46
47
48
/**
49
* @return {!goog.math.Rect} A new copy of this Rectangle.
50
*/
51
goog.math.Rect.prototype.clone = function() {
52
'use strict';
53
return new goog.math.Rect(this.left, this.top, this.width, this.height);
54
};
55
56
57
/**
58
* Returns a new Box object with the same position and dimensions as this
59
* rectangle.
60
* @return {!goog.math.Box} A new Box representation of this Rectangle.
61
*/
62
goog.math.Rect.prototype.toBox = function() {
63
'use strict';
64
var right = this.left + this.width;
65
var bottom = this.top + this.height;
66
return new goog.math.Box(this.top, right, bottom, this.left);
67
};
68
69
70
/**
71
* Creates a new Rect object with the position and size given.
72
* @param {!goog.math.Coordinate} position The top-left coordinate of the Rect
73
* @param {!goog.math.Size} size The size of the Rect
74
* @return {!goog.math.Rect} A new Rect initialized with the given position and
75
* size.
76
*/
77
goog.math.Rect.createFromPositionAndSize = function(position, size) {
78
'use strict';
79
return new goog.math.Rect(position.x, position.y, size.width, size.height);
80
};
81
82
83
/**
84
* Creates a new Rect object with the same position and dimensions as a given
85
* Box. Note that this is only the inverse of toBox if left/top are defined.
86
* @param {goog.math.Box} box A box.
87
* @return {!goog.math.Rect} A new Rect initialized with the box's position
88
* and size.
89
*/
90
goog.math.Rect.createFromBox = function(box) {
91
'use strict';
92
return new goog.math.Rect(
93
box.left, box.top, box.right - box.left, box.bottom - box.top);
94
};
95
96
97
if (goog.DEBUG) {
98
/**
99
* Returns a nice string representing size and dimensions of rectangle.
100
* @return {string} In the form (50, 73 - 75w x 25h).
101
* @override
102
*/
103
goog.math.Rect.prototype.toString = function() {
104
'use strict';
105
return '(' + this.left + ', ' + this.top + ' - ' + this.width + 'w x ' +
106
this.height + 'h)';
107
};
108
}
109
110
111
/**
112
* Compares rectangles for equality.
113
* @param {goog.math.IRect} a A Rectangle.
114
* @param {goog.math.IRect} b A Rectangle.
115
* @return {boolean} True iff the rectangles have the same left, top, width,
116
* and height, or if both are null.
117
*/
118
goog.math.Rect.equals = function(a, b) {
119
'use strict';
120
if (a == b) {
121
return true;
122
}
123
if (!a || !b) {
124
return false;
125
}
126
return a.left == b.left && a.width == b.width && a.top == b.top &&
127
a.height == b.height;
128
};
129
130
131
/**
132
* Computes the intersection of this rectangle and the rectangle parameter. If
133
* there is no intersection, returns false and leaves this rectangle as is.
134
* @param {goog.math.IRect} rect A Rectangle.
135
* @return {boolean} True iff this rectangle intersects with the parameter.
136
*/
137
goog.math.Rect.prototype.intersection = function(rect) {
138
'use strict';
139
var x0 = Math.max(this.left, rect.left);
140
var x1 = Math.min(this.left + this.width, rect.left + rect.width);
141
142
if (x0 <= x1) {
143
var y0 = Math.max(this.top, rect.top);
144
var y1 = Math.min(this.top + this.height, rect.top + rect.height);
145
146
if (y0 <= y1) {
147
this.left = x0;
148
this.top = y0;
149
this.width = x1 - x0;
150
this.height = y1 - y0;
151
152
return true;
153
}
154
}
155
return false;
156
};
157
158
159
/**
160
* Returns the intersection of two rectangles. Two rectangles intersect if they
161
* touch at all, for example, two zero width and height rectangles would
162
* intersect if they had the same top and left.
163
* @param {goog.math.IRect} a A Rectangle.
164
* @param {goog.math.IRect} b A Rectangle.
165
* @return {goog.math.Rect} A new intersection rect (even if width and height
166
* are 0), or null if there is no intersection.
167
*/
168
goog.math.Rect.intersection = function(a, b) {
169
'use strict';
170
// There is no nice way to do intersection via a clone, because any such
171
// clone might be unnecessary if this function returns null. So, we duplicate
172
// code from above.
173
174
var x0 = Math.max(a.left, b.left);
175
var x1 = Math.min(a.left + a.width, b.left + b.width);
176
177
if (x0 <= x1) {
178
var y0 = Math.max(a.top, b.top);
179
var y1 = Math.min(a.top + a.height, b.top + b.height);
180
181
if (y0 <= y1) {
182
return new goog.math.Rect(x0, y0, x1 - x0, y1 - y0);
183
}
184
}
185
return null;
186
};
187
188
189
/**
190
* Returns whether two rectangles intersect. Two rectangles intersect if they
191
* touch at all, for example, two zero width and height rectangles would
192
* intersect if they had the same top and left.
193
* @param {goog.math.IRect} a A Rectangle.
194
* @param {goog.math.IRect} b A Rectangle.
195
* @return {boolean} Whether a and b intersect.
196
*/
197
goog.math.Rect.intersects = function(a, b) {
198
'use strict';
199
return (
200
a.left <= b.left + b.width && b.left <= a.left + a.width &&
201
a.top <= b.top + b.height && b.top <= a.top + a.height);
202
};
203
204
205
/**
206
* Returns whether a rectangle intersects this rectangle.
207
* @param {goog.math.IRect} rect A rectangle.
208
* @return {boolean} Whether rect intersects this rectangle.
209
*/
210
goog.math.Rect.prototype.intersects = function(rect) {
211
'use strict';
212
return goog.math.Rect.intersects(this, rect);
213
};
214
215
216
/**
217
* Computes the difference regions between two rectangles. The return value is
218
* an array of 0 to 4 rectangles defining the remaining regions of the first
219
* rectangle after the second has been subtracted.
220
* @param {goog.math.Rect} a A Rectangle.
221
* @param {goog.math.IRect} b A Rectangle.
222
* @return {!Array<!goog.math.Rect>} An array with 0 to 4 rectangles which
223
* together define the difference area of rectangle a minus rectangle b.
224
*/
225
goog.math.Rect.difference = function(a, b) {
226
'use strict';
227
var intersection = goog.math.Rect.intersection(a, b);
228
if (!intersection || !intersection.height || !intersection.width) {
229
return [a.clone()];
230
}
231
232
var result = [];
233
234
var top = a.top;
235
var height = a.height;
236
237
var ar = a.left + a.width;
238
var ab = a.top + a.height;
239
240
var br = b.left + b.width;
241
var bb = b.top + b.height;
242
243
// Subtract off any area on top where A extends past B
244
if (b.top > a.top) {
245
result.push(new goog.math.Rect(a.left, a.top, a.width, b.top - a.top));
246
top = b.top;
247
// If we're moving the top down, we also need to subtract the height diff.
248
height -= b.top - a.top;
249
}
250
// Subtract off any area on bottom where A extends past B
251
if (bb < ab) {
252
result.push(new goog.math.Rect(a.left, bb, a.width, ab - bb));
253
height = bb - top;
254
}
255
// Subtract any area on left where A extends past B
256
if (b.left > a.left) {
257
result.push(new goog.math.Rect(a.left, top, b.left - a.left, height));
258
}
259
// Subtract any area on right where A extends past B
260
if (br < ar) {
261
result.push(new goog.math.Rect(br, top, ar - br, height));
262
}
263
264
return result;
265
};
266
267
268
/**
269
* Computes the difference regions between this rectangle and `rect`. The
270
* return value is an array of 0 to 4 rectangles defining the remaining regions
271
* of this rectangle after the other has been subtracted.
272
* @param {goog.math.IRect} rect A Rectangle.
273
* @return {!Array<!goog.math.Rect>} An array with 0 to 4 rectangles which
274
* together define the difference area of rectangle a minus rectangle b.
275
*/
276
goog.math.Rect.prototype.difference = function(rect) {
277
'use strict';
278
return goog.math.Rect.difference(this, rect);
279
};
280
281
282
/**
283
* Expand this rectangle to also include the area of the given rectangle.
284
* @param {goog.math.IRect} rect The other rectangle.
285
*/
286
goog.math.Rect.prototype.boundingRect = function(rect) {
287
'use strict';
288
// We compute right and bottom before we change left and top below.
289
var right = Math.max(this.left + this.width, rect.left + rect.width);
290
var bottom = Math.max(this.top + this.height, rect.top + rect.height);
291
292
this.left = Math.min(this.left, rect.left);
293
this.top = Math.min(this.top, rect.top);
294
295
this.width = right - this.left;
296
this.height = bottom - this.top;
297
};
298
299
300
/**
301
* Returns a new rectangle which completely contains both input rectangles.
302
* @param {goog.math.IRect} a A rectangle.
303
* @param {goog.math.IRect} b A rectangle.
304
* @return {goog.math.Rect} A new bounding rect, or null if either rect is
305
* null.
306
*/
307
goog.math.Rect.boundingRect = function(a, b) {
308
'use strict';
309
if (!a || !b) {
310
return null;
311
}
312
313
var newRect = new goog.math.Rect(a.left, a.top, a.width, a.height);
314
newRect.boundingRect(b);
315
316
return newRect;
317
};
318
319
320
/**
321
* Tests whether this rectangle entirely contains another rectangle or
322
* coordinate.
323
*
324
* @param {goog.math.IRect|goog.math.Coordinate} another The rectangle or
325
* coordinate to test for containment.
326
* @return {boolean} Whether this rectangle contains given rectangle or
327
* coordinate.
328
*/
329
goog.math.Rect.prototype.contains = function(another) {
330
'use strict';
331
if (another instanceof goog.math.Coordinate) {
332
return another.x >= this.left && another.x <= this.left + this.width &&
333
another.y >= this.top && another.y <= this.top + this.height;
334
} else { // (another instanceof goog.math.IRect)
335
return this.left <= another.left &&
336
this.left + this.width >= another.left + another.width &&
337
this.top <= another.top &&
338
this.top + this.height >= another.top + another.height;
339
}
340
};
341
342
343
/**
344
* @param {!goog.math.Coordinate} point A coordinate.
345
* @return {number} The squared distance between the point and the closest
346
* point inside the rectangle. Returns 0 if the point is inside the
347
* rectangle.
348
*/
349
goog.math.Rect.prototype.squaredDistance = function(point) {
350
'use strict';
351
var dx = point.x < this.left ?
352
this.left - point.x :
353
Math.max(point.x - (this.left + this.width), 0);
354
var dy = point.y < this.top ? this.top - point.y :
355
Math.max(point.y - (this.top + this.height), 0);
356
return dx * dx + dy * dy;
357
};
358
359
360
/**
361
* @param {!goog.math.Coordinate} point A coordinate.
362
* @return {number} The distance between the point and the closest point
363
* inside the rectangle. Returns 0 if the point is inside the rectangle.
364
*/
365
goog.math.Rect.prototype.distance = function(point) {
366
'use strict';
367
return Math.sqrt(this.squaredDistance(point));
368
};
369
370
371
/**
372
* @return {!goog.math.Size} The size of this rectangle.
373
*/
374
goog.math.Rect.prototype.getSize = function() {
375
'use strict';
376
return new goog.math.Size(this.width, this.height);
377
};
378
379
380
/**
381
* @return {!goog.math.Coordinate} A new coordinate for the top-left corner of
382
* the rectangle.
383
*/
384
goog.math.Rect.prototype.getTopLeft = function() {
385
'use strict';
386
return new goog.math.Coordinate(this.left, this.top);
387
};
388
389
390
/**
391
* @return {!goog.math.Coordinate} A new coordinate for the center of the
392
* rectangle.
393
*/
394
goog.math.Rect.prototype.getCenter = function() {
395
'use strict';
396
return new goog.math.Coordinate(
397
this.left + this.width / 2, this.top + this.height / 2);
398
};
399
400
401
/**
402
* @return {!goog.math.Coordinate} A new coordinate for the bottom-right corner
403
* of the rectangle.
404
*/
405
goog.math.Rect.prototype.getBottomRight = function() {
406
'use strict';
407
return new goog.math.Coordinate(
408
this.left + this.width, this.top + this.height);
409
};
410
411
412
/**
413
* Rounds the fields to the next larger integer values.
414
* @return {!goog.math.Rect} This rectangle with ceil'd fields.
415
*/
416
goog.math.Rect.prototype.ceil = function() {
417
'use strict';
418
this.left = Math.ceil(this.left);
419
this.top = Math.ceil(this.top);
420
this.width = Math.ceil(this.width);
421
this.height = Math.ceil(this.height);
422
return this;
423
};
424
425
426
/**
427
* Rounds the fields to the next smaller integer values.
428
* @return {!goog.math.Rect} This rectangle with floored fields.
429
*/
430
goog.math.Rect.prototype.floor = function() {
431
'use strict';
432
this.left = Math.floor(this.left);
433
this.top = Math.floor(this.top);
434
this.width = Math.floor(this.width);
435
this.height = Math.floor(this.height);
436
return this;
437
};
438
439
440
/**
441
* Rounds the fields to nearest integer values.
442
* @return {!goog.math.Rect} This rectangle with rounded fields.
443
*/
444
goog.math.Rect.prototype.round = function() {
445
'use strict';
446
this.left = Math.round(this.left);
447
this.top = Math.round(this.top);
448
this.width = Math.round(this.width);
449
this.height = Math.round(this.height);
450
return this;
451
};
452
453
454
/**
455
* Translates this rectangle by the given offsets. If a
456
* `goog.math.Coordinate` is given, then the left and top values are
457
* translated by the coordinate's x and y values. Otherwise, left and top are
458
* translated by `tx` and `opt_ty` respectively.
459
* @param {number|goog.math.Coordinate} tx The value to translate left by or the
460
* the coordinate to translate this rect by.
461
* @param {number=} opt_ty The value to translate top by.
462
* @return {!goog.math.Rect} This rectangle after translating.
463
*/
464
goog.math.Rect.prototype.translate = function(tx, opt_ty) {
465
'use strict';
466
if (tx instanceof goog.math.Coordinate) {
467
this.left += tx.x;
468
this.top += tx.y;
469
} else {
470
this.left += goog.asserts.assertNumber(tx);
471
if (typeof opt_ty === 'number') {
472
this.top += opt_ty;
473
}
474
}
475
return this;
476
};
477
478
479
/**
480
* Scales this rectangle by the given scale factors. The left and width values
481
* are scaled by `sx` and the top and height values are scaled by
482
* `opt_sy`. If `opt_sy` is not given, then all fields are scaled
483
* by `sx`.
484
* @param {number} sx The scale factor to use for the x dimension.
485
* @param {number=} opt_sy The scale factor to use for the y dimension.
486
* @return {!goog.math.Rect} This rectangle after scaling.
487
*/
488
goog.math.Rect.prototype.scale = function(sx, opt_sy) {
489
'use strict';
490
var sy = (typeof opt_sy === 'number') ? opt_sy : sx;
491
this.left *= sx;
492
this.width *= sx;
493
this.top *= sy;
494
this.height *= sy;
495
return this;
496
};
497
498