-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rect2.ts
387 lines (320 loc) · 9.66 KB
/
Rect2.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
import LineSegment2 from './LineSegment2';
import Mat33 from '../Mat33';
import { Point2, Vec2 } from '../Vec2';
import Abstract2DShape from './Abstract2DShape';
import Vec3 from '../Vec3';
/** An object that can be converted to a {@link Rect2}. */
export interface RectTemplate {
x: number;
y: number;
w?: number;
h?: number;
width?: number;
height?: number;
}
/**
* Represents a rectangle in 2D space, parallel to the XY axes.
*
* `invariant: w ≥ 0, h ≥ 0, immutable`
*/
export class Rect2 extends Abstract2DShape {
// Derived state:
// topLeft assumes up is -y
public readonly topLeft: Point2;
public readonly size: Vec2;
public readonly area: number;
public constructor(
public readonly x: number,
public readonly y: number,
public readonly w: number,
public readonly h: number
) {
super();
if (w < 0) {
this.x += w;
this.w = Math.abs(w);
}
if (h < 0) {
this.y += h;
this.h = Math.abs(h);
}
// Precompute/store vector forms.
this.topLeft = Vec2.of(this.x, this.y);
this.size = Vec2.of(this.w, this.h);
this.area = this.w * this.h;
}
public translatedBy(vec: Vec2): Rect2 {
return new Rect2(vec.x + this.x, vec.y + this.y, this.w, this.h);
}
// Returns a copy of this with the given size (but same top-left).
public resizedTo(size: Vec2): Rect2 {
return new Rect2(this.x, this.y, size.x, size.y);
}
public override containsPoint(other: Point2): boolean {
return this.x <= other.x && this.y <= other.y
&& this.x + this.w >= other.x && this.y + this.h >= other.y;
}
public containsRect(other: Rect2): boolean {
return this.x <= other.x && this.y <= other.y
&& this.x + this.w >= other.x + other.w
&& this.y + this.h >= other.y + other.h;
}
/**
* @returns true iff this and `other` overlap
*/
public intersects(other: Rect2): boolean {
// Project along x/y axes.
const thisMinX = this.x;
const thisMaxX = thisMinX + this.w;
const otherMinX = other.x;
const otherMaxX = other.x + other.w;
if (thisMaxX < otherMinX || thisMinX > otherMaxX) {
return false;
}
const thisMinY = this.y;
const thisMaxY = thisMinY + this.h;
const otherMinY = other.y;
const otherMaxY = other.y + other.h;
if (thisMaxY < otherMinY || thisMinY > otherMaxY) {
return false;
}
return true;
}
// Returns the overlap of this and [other], or null, if no such
// overlap exists
public intersection(other: Rect2): Rect2|null {
if (!this.intersects(other)) {
return null;
}
const topLeft = this.topLeft.zip(other.topLeft, Math.max);
const bottomRight = this.bottomRight.zip(other.bottomRight, Math.min);
return Rect2.fromCorners(topLeft, bottomRight);
}
// Returns a new rectangle containing both [this] and [other].
public union(other: Rect2): Rect2 {
return Rect2.union(this, other);
}
// Returns a the subdivision of this into [columns] columns
// and [rows] rows. For example,
// Rect2.unitSquare.divideIntoGrid(2, 2)
// -> [ Rect2(0, 0, 0.5, 0.5), Rect2(0.5, 0, 0.5, 0.5), Rect2(0, 0.5, 0.5, 0.5), Rect2(0.5, 0.5, 0.5, 0.5) ]
// The rectangles are ordered in row-major order.
public divideIntoGrid(columns: number, rows: number): Rect2[] {
const result: Rect2[] = [];
if (columns <= 0 || rows <= 0) {
return result;
}
const eachRectWidth = this.w / columns;
const eachRectHeight = this.h / rows;
if (eachRectWidth === 0) {
columns = 1;
}
if (eachRectHeight === 0) {
rows = 1;
}
for (let j = 0; j < rows; j++) {
for (let i = 0; i < columns; i++) {
const x = eachRectWidth * i + this.x;
const y = eachRectHeight * j + this.y;
result.push(new Rect2(x, y, eachRectWidth, eachRectHeight));
}
}
return result;
}
// Returns a rectangle containing this and [point].
// [margin] is the minimum distance between the new point and the edge
// of the resultant rectangle.
public grownToPoint(point: Point2, margin: number = 0): Rect2 {
const otherRect = new Rect2(
point.x - margin, point.y - margin,
margin * 2, margin * 2
);
return this.union(otherRect);
}
// Returns this grown by [margin] in both the x and y directions.
public grownBy(margin: number): Rect2 {
if (margin === 0) {
return this;
}
// Prevent width/height from being negative
if (margin < 0) {
const xMargin = -Math.min(-margin, this.w / 2);
const yMargin = -Math.min(-margin, this.h / 2);
return new Rect2(
this.x - xMargin, this.y - yMargin,
this.w + xMargin * 2, this.h + yMargin * 2,
);
}
return new Rect2(
this.x - margin, this.y - margin, this.w + margin * 2, this.h + margin * 2
);
}
public getClosestPointOnBoundaryTo(target: Point2) {
const closestEdgePoints = this.getEdges().map(edge => {
return edge.closestPointTo(target);
});
let closest: Point2|null = null;
let closestDist: number|null = null;
for (const point of closestEdgePoints) {
const dist = point.distanceTo(target);
if (closestDist === null || dist < closestDist) {
closest = point;
closestDist = dist;
}
}
return closest!;
}
/**
* Returns `true` iff all points in this rectangle are within `distance` from `point`:
*
* If $R$ is the set of points in this rectangle, returns `true` iff
* $$
* \forall {\bf a} \in R, \|\texttt{point} - {\bf a}\| < \texttt{radius}
* $$
*/
public isWithinRadiusOf(radius: number, point: Point2) {
if (this.maxDimension > radius) {
return false;
}
const squareRadius = radius * radius;
return this.corners.every(corner => corner.minus(point).magnitudeSquared() < squareRadius);
}
public get corners(): Point2[] {
return [
this.bottomRight,
this.topRight,
this.topLeft,
this.bottomLeft,
];
}
public get maxDimension() {
return Math.max(this.w, this.h);
}
public get minDimension() {
return Math.min(this.w, this.h);
}
public get bottomRight() {
return this.topLeft.plus(this.size);
}
public get topRight() {
return this.bottomRight.plus(Vec2.of(0, -this.h));
}
public get bottomLeft() {
return this.topLeft.plus(Vec2.of(0, this.h));
}
public get width() {
return this.w;
}
public get height() {
return this.h;
}
public get center() {
return Vec2.of(this.x + this.w / 2, this.y + this.h / 2);
}
// Returns edges in the order
// [ rightEdge, topEdge, leftEdge, bottomEdge ]
public getEdges(): LineSegment2[] {
const corners = this.corners;
return [
new LineSegment2(corners[0], corners[1]),
new LineSegment2(corners[1], corners[2]),
new LineSegment2(corners[2], corners[3]),
new LineSegment2(corners[3], corners[0]),
];
}
public override intersectsLineSegment(lineSegment: LineSegment2): Point2[] {
const result: Point2[] = [];
for (const edge of this.getEdges()) {
const intersection = edge.intersectsLineSegment(lineSegment);
intersection.forEach(point => result.push(point));
}
return result;
}
public override signedDistance(point: Vec3): number {
const closestBoundaryPoint = this.getClosestPointOnBoundaryTo(point);
const dist = point.minus(closestBoundaryPoint).magnitude();
if (this.containsPoint(point)) {
return -dist;
}
return dist;
}
public override getTightBoundingBox(): Rect2 {
return this;
}
// [affineTransform] is a transformation matrix that both scales and **translates**.
// the bounding box of this' four corners after transformed by the given affine transformation.
public transformedBoundingBox(affineTransform: Mat33): Rect2 {
return Rect2.bboxOf(this.corners.map(corner => affineTransform.transformVec2(corner)));
}
/** @return true iff this is equal to `other ± tolerance` */
public eq(other: Rect2, tolerance: number = 0): boolean {
return this.topLeft.eq(other.topLeft, tolerance) && this.size.eq(other.size, tolerance);
}
public override toString(): string {
return `Rect(point(${this.x}, ${this.y}), size(${this.w}, ${this.h}))`;
}
public static fromCorners(corner1: Point2, corner2: Point2) {
return new Rect2(
Math.min(corner1.x, corner2.x),
Math.min(corner1.y, corner2.y),
Math.abs(corner1.x - corner2.x),
Math.abs(corner1.y - corner2.y)
);
}
// Returns a box that contains all points in [points] with at least [margin]
// between each point and the edge of the box.
public static bboxOf(points: Point2[], margin: number = 0) {
let minX = 0;
let minY = 0;
let maxX = 0;
let maxY = 0;
let isFirst = true;
for (const point of points) {
if (isFirst) {
minX = point.x;
minY = point.y;
maxX = point.x;
maxY = point.y;
isFirst = false;
}
minX = Math.min(minX, point.x);
minY = Math.min(minY, point.y);
maxX = Math.max(maxX, point.x);
maxY = Math.max(maxY, point.y);
}
return Rect2.fromCorners(
Vec2.of(minX - margin, minY - margin),
Vec2.of(maxX + margin, maxY + margin)
);
}
// @returns a rectangle that contains all of the given rectangles, the bounding box
// of the given rectangles.
public static union(...rects: Rect2[]): Rect2 {
if (rects.length === 0) {
return Rect2.empty;
}
const firstRect = rects[0];
let minX: number = firstRect.x;
let minY: number = firstRect.y;
let maxX: number = firstRect.x + firstRect.w;
let maxY: number = firstRect.y + firstRect.h;
for (let i = 1; i < rects.length; i++) {
const rect = rects[i];
minX = Math.min(minX, rect.x);
minY = Math.min(minY, rect.y);
maxX = Math.max(maxX, rect.x + rect.w);
maxY = Math.max(maxY, rect.y + rect.h);
}
return new Rect2(
minX, minY, maxX - minX, maxY - minY,
);
}
public static of(template: RectTemplate) {
const width = template.width ?? template.w ?? 0;
const height = template.height ?? template.h ?? 0;
return new Rect2(template.x, template.y, width, height);
}
public static empty = new Rect2(0, 0, 0, 0);
public static unitSquare = new Rect2(0, 0, 1, 1);
}
export default Rect2;