-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rectangle.ts
387 lines (348 loc) · 10.6 KB
/
Rectangle.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Utilities
*/
import { Point } from "./Point";
import type { PointProps } from "./PointProps";
import type { SizeProps } from "./Size";
import { Size } from "./Size";
import { UiGeometry } from "./UiGeometry";
/** Describes 2d bounds.
* @public
*/
export interface RectangleProps {
readonly left: number;
readonly top: number;
readonly right: number;
readonly bottom: number;
}
/** Available corners of [[Rectangle]].
* @internal
*/
export enum Corner {
TopLeft,
TopRight,
BottomRight,
BottomLeft,
}
/** Describes and provides methods to work with 2d bounds.
* @internal
*/
export class Rectangle implements RectangleProps {
/** Creates rectangle from [[RectangleProps]]. */
public static create(props: RectangleProps): Rectangle {
return new Rectangle(props.left, props.top, props.right, props.bottom);
}
/** Creates rectangle from [[SizeProps]]. */
public static createFromSize(size: SizeProps): Rectangle {
return new Rectangle(0, 0, size.width, size.height);
}
/** Create a rectangle with 2 pairs of xy candidates. Theses are compared and shuffled as needed for the rectangle. */
public static createXYXY(
xA: number,
yA: number,
xB: number,
yB: number
): Rectangle {
return new Rectangle(
Math.min(xA, xB),
Math.min(yA, yB),
Math.max(xA, xB),
Math.max(yA, yB)
);
}
/** Creates rectangle with specified bounds. */
public constructor(
public readonly left = 0,
public readonly top = 0,
public readonly right = 0,
public readonly bottom = 0
) {}
/** @returns Size of this rectangle. */
public getSize(): Size {
const width = this.getWidth();
const height = this.getHeight();
return new Size(width, height);
}
/** @returns Width of this rectangle. */
public getWidth(): number {
return this.right - this.left;
}
/** @returns Height of this rectangle. */
public getHeight(): number {
return this.bottom - this.top;
}
/** @returns Position of specified corner. */
public getCorner(corner: Corner): Point {
switch (corner) {
case Corner.TopLeft: {
return this.topLeft();
}
case Corner.TopRight: {
return new Point(this.right, this.top);
}
case Corner.BottomLeft: {
return new Point(this.left, this.bottom);
}
case Corner.BottomRight: {
return new Point(this.right, this.bottom);
}
}
}
/**
* Inset the bounds of this rectangle.
* @note Negative arguments will increase the size of rectangle.
* @returns New [[Rectangle]] with modified bounds.
*/
public inset(
left: number,
top: number,
right: number,
bottom: number
): Rectangle {
return new Rectangle(
this.left + left,
this.top + top,
this.right - right,
this.bottom - bottom
);
}
/**
* Offsets the rectangle along the X and Y axes.
* @returns New [[Rectangle]] with modified position.
*/
public offset(offset: PointProps): Rectangle {
return new Rectangle(
this.left + offset.x,
this.top + offset.y,
this.right + offset.x,
this.bottom + offset.y
);
}
/**
* Offsets the rectangle along the X axis.
* @returns New [[Rectangle]] with modified position along X axis.
*/
public offsetX(offset: number): Rectangle {
return new Rectangle(
this.left + offset,
this.top,
this.right + offset,
this.bottom
);
}
/**
* Offsets the rectangle along the Y axis.
* @returns New [[Rectangle]] with modified position along Y axis.
*/
public offsetY(offset: number): Rectangle {
return new Rectangle(
this.left,
this.top + offset,
this.right,
this.bottom + offset
);
}
/**
* Moves the top left corner of rectangle to specified point.
* @returns New [[Rectangle]] with modified position.
*/
public setPosition(position: PointProps): Rectangle {
return new Rectangle(
position.x,
position.y,
position.x + this.getWidth(),
position.y + this.getHeight()
);
}
/**
* Sets the height of the rectangle.
* @note Only [[Edge.Bottom]] is subject to change.
* @returns New [[Rectangle]] with modified height.
*/
public setHeight(height: number): Rectangle {
return new Rectangle(this.left, this.top, this.right, this.top + height);
}
/**
* Sets the width of the rectangle.
* @note Only [[Edge.Right]] is subject to change.
* @returns New [[Rectangle]] with modified width.
*/
public setWidth(width: number): Rectangle {
return new Rectangle(this.left, this.top, this.left + width, this.bottom);
}
/**
* Sets the height and width of the rectangle.
* @note Only [[Edge.Bottom]] and [[Edge.Right]] are subjects to change.
* @returns New [[Rectangle]] with modified height.
*/
public setSize(size: SizeProps): Rectangle {
return new Rectangle(
this.left,
this.top,
this.left + size.width,
this.top + size.height
);
}
/** Checks if bounds of two rectangles match. */
public equals(other: RectangleProps): boolean {
if (
this.left === other.left &&
this.top === other.top &&
this.right === other.right &&
this.bottom === other.bottom
)
return true;
return false;
}
/**
* Checks if point is within bounds of the rectangle.
* @note Inclusive.
*/
public containsPoint(point: PointProps): boolean {
return (
point.x >= this.left &&
point.x <= this.right &&
point.y >= this.top &&
point.y <= this.bottom
);
}
/**
* Checks if a point given as x,y is within the rectangle.
* @note Inclusive.
*/
public containsXY(x: number, y: number): boolean {
return this.containsPoint({ x, y });
}
/**
* @returns true if this rectangle contains other rectangle.
* @note Inclusive.
*/
public contains(other: RectangleProps): boolean {
return (
other.left >= this.left &&
other.right <= this.right &&
other.top >= this.top &&
other.bottom <= this.bottom
);
}
/** @returns New [[Rectangle]] which is contained in other rectangle. */
public containIn(other: RectangleProps): Rectangle {
let contained: Rectangle = this.containVerticallyIn(other);
contained = contained.containHorizontallyIn(other);
return contained;
}
/** @returns New [[Rectangle]] which is vertically contained in other rectangle. */
public containVerticallyIn(other: RectangleProps): Rectangle {
const contained = this.offsetY(Math.min(other.bottom - this.bottom, 0));
return contained.offsetY(Math.max(other.top - contained.top, 0));
}
/** @returns New [[Rectangle]] which is horizontally contained in other rectangle. */
public containHorizontallyIn(other: RectangleProps): Rectangle {
const contained = this.offsetX(Math.min(other.right - this.right, 0));
return contained.offsetX(Math.max(other.left - contained.left, 0));
}
/** @returns [[Corner.TopLeft]] position of this rectangle. */
public topLeft(): Point {
return new Point(this.left, this.top);
}
/** @returns Center point position of this rectangle. */
public center(): Point {
const x = this.left + (this.right - this.left) / 2;
const y = this.top + (this.bottom - this.top) / 2;
return new Point(x, y);
}
/** @returns true if this rectangle intersects other rectangle. */
public intersects(other: RectangleProps): boolean {
return (
this.left < other.right &&
this.right > other.left &&
this.top < other.bottom &&
this.bottom > other.top
);
}
/**
* Merges outer edges of this and other rectangles.
* @returns New [[Rectangle]] with merged bounds.
*/
public outerMergeWith(other: RectangleProps): Rectangle {
const left = Math.min(this.left, other.left);
const top = Math.min(this.top, other.top);
const right = Math.max(this.right, other.right);
const bottom = Math.max(this.bottom, other.bottom);
return new Rectangle(left, top, right, bottom);
}
/**
* Vertically divides this rectangle into specified number of equal height segments.
* @returns Vertical rectangle segment.
*/
public getVerticalSegmentBounds(
segmentId: number,
numberOfSegments: number
): Rectangle {
const segmentHeight = this.getHeight() / numberOfSegments;
const top = segmentId * segmentHeight;
return this.inset(0, top, 0, 0).setHeight(segmentHeight);
}
/**
* Horizontally divides this rectangle into specified number of equal width segments.
* @returns Horizontal rectangle segment.
*/
public getHorizontalSegmentBounds(
segmentId: number,
numberOfSegments: number
): Rectangle {
const segmentWidth = this.getWidth() / numberOfSegments;
const left = segmentId * segmentWidth;
return this.inset(left, 0, 0, 0).setWidth(segmentWidth);
}
/**
* Calculates the shortest distance between this rectangle and a given point.
* @returns The shortest distance to a point.
*/
public getShortestDistanceToPoint(point: PointProps): number {
let shortestDistance = 0;
if (point.x < this.left) {
if (point.y < this.top)
shortestDistance = UiGeometry.hypotenuseXY(
this.left - point.x,
this.top - point.y
);
else if (point.y <= this.bottom) shortestDistance = this.left - point.x;
else
shortestDistance = UiGeometry.hypotenuseXY(
this.left - point.x,
this.bottom - point.y
);
} else if (point.x <= this.right) {
if (point.y < this.top) shortestDistance = this.top - point.y;
else if (point.y <= this.bottom) shortestDistance = 0;
else shortestDistance = point.y - this.bottom;
} else {
if (point.y < this.top)
shortestDistance = UiGeometry.hypotenuseXY(
this.right - point.x,
this.top - point.y
);
else if (point.y <= this.bottom) shortestDistance = point.x - this.right;
else
shortestDistance = UiGeometry.hypotenuseXY(
this.right - point.x,
this.bottom - point.y
);
}
return shortestDistance;
}
/** @returns [[RectangleProps]] object for this rectangle. */
public toProps(): RectangleProps {
return {
bottom: this.bottom,
left: this.left,
right: this.right,
top: this.top,
};
}
}