/
rectangle.ts
448 lines (411 loc) · 13.2 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import {
BoundingBox,
Geometry,
Interval,
IntervalSorted,
Line,
Plane,
Point,
PointContainment,
Polyline,
shapetypesSettings,
Transform
} from '../index';
/**
* A rectangular shape aligned to a plane.
*
* ### Example
* ```js
* import { Plane, Point, Rectangle, Vector } from 'shapetypes';
*
* // Create a new rectangle
* const rect = new Rectangle(Plane.worldXY(), 10, 20);
*
* // Get properties of the rectangle
* console.log(rect.area);
* // => 200
* console.log(rect.circumference);
* // => 60
* console.log(rect.center.toString());
* // => (5,10)
*
* // Create a new rectangle aligned to a plane
* const plane = new Plane(new Point(3,4), new Vector(1,1));
* const angledRect = new Rectangle(plane, 10, 20);
*
* // Get corners of angled rectangle
* console.log(angledRect.getCorners());
* // => [(3,4),(-11.14213562373095,8.14213562373095),(-4.071067811865474,25.213203435596427),(10.071067811865476,11.071067811865476)]
* ```
*/
export class Rectangle extends Geometry {
// -----------------------
// STATIC
// -----------------------
/**
* Creates a rectangle from two corner points.
* @category Create
* @param cornerA One corner of the rectangle.
* @param cornerB The opposite corner of the rectangle.
* @param plane The plane the rectangle is orientated to.
* If unset, the rectangle will be aligned to the global x- and y-axis.
*/
public static fromCorners(
cornerA: Point,
cornerB: Point,
plane: Plane = Plane.worldXY()
): Rectangle {
const a = plane.remapToPlaneSpace(cornerA);
const b = plane.remapToPlaneSpace(cornerB);
const x = new IntervalSorted(a.x, b.x);
const y = new IntervalSorted(a.y, b.y);
return new Rectangle(plane, x, y);
}
/**
* Creates a rectangle centered and aligned to a plane.
*
* @category Create
* @param x Width of the rectangle along the x-axis of the plane (rectangle will be centered on the plane's origin).
* @param y Width of the rectangle along the y-axis of the plane (rectangle will be centered on the plane's origin).
* @param center The plane the rectangle is aligned to. The plane's origin will be the center of the rectangle.
*/
public static fromCenter(center: Plane, x: number, y: number): Rectangle {
return new Rectangle(
center,
IntervalSorted.fromCenter(0, x),
IntervalSorted.fromCenter(0, y)
);
}
// -----------------------
// VARS
// -----------------------
private readonly _plane: Plane;
private readonly _x: IntervalSorted;
private readonly _y: IntervalSorted;
// -----------------------
// CONSTRUCTOR
// -----------------------
/***
* Creates a rectangle aligned to a plane. One corner of the rectangle will
* sit on the plane's origin, with the rest of the rectangle growing along the
* plane's axes from that point.
*
*
* @param plane The plane the rectangle is aligned to. The plane's origin will be one corner of the rectangle
* with the rest of the rectangle growing from that point.
* @param x The width of the rectangle along the plane's x-axis.
* @param y The width of the rectangle along the plane's y-axis.
*/
constructor(plane: Plane, x: number, y: number);
/**
* Creates a new rectangle on a plane.
* @param plane The position of the rectangle and its axes.
* @param x Position of the rectangle along the x-axis of the plane.
* @param y Position of the rectangle along the y-axis of the plane.
*/
constructor(
plane: Plane,
x: Interval | IntervalSorted,
y: Interval | IntervalSorted
);
constructor(
plane: Plane,
x: Interval | IntervalSorted | number,
y: Interval | IntervalSorted | number
) {
super();
this._plane = plane;
if (x instanceof IntervalSorted) {
this._x = x;
} else if (x instanceof Interval) {
this._x = x.asSorted();
} else {
this._x = new IntervalSorted(0, x);
}
if (y instanceof IntervalSorted) {
this._y = y;
} else if (y instanceof Interval) {
this._y = y.asSorted();
} else {
this._y = new IntervalSorted(0, y);
}
}
// -----------------------
// GET
// -----------------------
/**
* Gets the area of the rectangle.
*/
get area(): number {
return this._x.length * this._y.length;
}
/***
* Gets the smallest bounding box that contains the rectangle.
*/
get boundingBox(): BoundingBox {
return BoundingBox.fromPoints(this.getCorners());
}
/**
* Gets the center of the rectangle.
*/
get center(): Point {
return this._plane.pointAt(this._x.mid, this._y.mid);
}
/**
* Gets the total length of the rectangle's edges.
*/
get circumference(): number {
return this._x.length * 2 + this._y.length * 2;
}
/**
* Gets the plane the rectangle is aligned to.
*
* Depending on how the rectangle is positioned on this plane
* (using the intervals [[x]] and [[y]]), the plane's origin may or may not be
* a corner or center of the rectangle.
*/
get plane(): Plane {
return this._plane;
}
/**
* Gets the width of the rectangle along its x-axis.
*/
get widthX(): number {
return this._x.length;
}
/**
* Gets the width of the rectangle along its y-axis.
*/
get widthY(): number {
return this._y.length;
}
/**
* Gets the position of the rectangle relative to the [[plane]]'s x-axis.
*/
get x(): IntervalSorted {
return this._x;
}
/**
* Gets the position of the rectangle relative to the [[plane]]'s y-axis.
*/
get y(): IntervalSorted {
return this._y;
}
// -----------------------
// PUBLIC
// -----------------------
/***
* Finds the closest point on the rectangle and returns the point.
* @param testPoint The target to get closest to.
* @param includeInterior If false, the closest point must lie on the outer edge of the rectangle.
* If true, the closest point can also be a point on the interior of the rectangle.
*/
public closestPoint(
testPoint: Point,
includeInterior: boolean = false
): Point {
if (includeInterior) {
if (this.contains(testPoint)) {
return testPoint;
}
}
return this.toPolyline().closestPoint(testPoint);
}
/***
* Checks whether a point is inside, outside, or on the edge of a rectangle.
*
* @param testPoint The point to test for containment.
* @param tolerance The distance the point can be from the edge of the rectangle and still considered coincident.
*/
public contains(
testPoint: Point,
tolerance = shapetypesSettings.absoluteTolerance
): PointContainment {
const planePoint = this._plane.remapToPlaneSpace(testPoint);
const x = this._x.containsPoint(planePoint.x, tolerance);
const y = this._y.containsPoint(planePoint.y, tolerance);
if (x === PointContainment.coincident) {
if (y === PointContainment.inside || y === PointContainment.coincident) {
return PointContainment.coincident;
}
}
if (y === PointContainment.coincident) {
if (x === PointContainment.inside || x === PointContainment.coincident) {
return PointContainment.coincident;
}
}
if (x === PointContainment.inside && y === PointContainment.inside) {
return PointContainment.inside;
}
return PointContainment.outside;
}
/**
* Gets a corner of the rectangle.
*
* @param minX If true, point will be at the min x value relative to the rectangle's plane. If false, it will be at max.
* @param minY If true, point will be at the min y value. If false, it will be at max.
*/
public corner(minX: boolean, minY: boolean): Point {
if (minX) {
if (minY) {
return this._plane.pointAt(this._x.min, this._y.min);
} else {
return this._plane.pointAt(this._x.min, this._y.max);
}
} else {
if (minY) {
return this._plane.pointAt(this._x.max, this._y.min);
} else {
return this._plane.pointAt(this._x.max, this._y.max);
}
}
}
/***
* Checks whether another rectangle has the same dimensions and location. Returns true if it does.
* @param otherRectangle Rectangle to compare against.
* @param tolerance The amount the dimensions can differ and still be considered equal.
*/
public equals(
otherRectangle: Rectangle,
tolerance: number = shapetypesSettings.absoluteTolerance
): boolean {
if (this._plane.equals(otherRectangle.plane, tolerance)) {
if (this._x.equals(otherRectangle.x)) {
if (this._y.equals(otherRectangle.y)) {
return true;
}
}
}
return false;
}
/***
* Gets the four corners of the rectangle.
* The order is: [[minX, minY], [minX, maxY], [maxX, maxY], [maxX, minY]].
*/
public getCorners(): readonly Point[] {
return [
this._plane.pointAt(this._x.min, this._y.min),
this._plane.pointAt(this._x.min, this._y.max),
this._plane.pointAt(this._x.max, this._y.max),
this._plane.pointAt(this._x.max, this._y.min)
];
}
/**
* Gets the four edges of the rectangle. Follows the order in [[getCorners]].
*/
public getEdges(): readonly Line[] {
const corners = this.getCorners();
return [
new Line(corners[0], corners[1]),
new Line(corners[1], corners[2]),
new Line(corners[2], corners[3]),
new Line(corners[3], corners[0])
];
}
/***
* Remaps a point from the u-v space of the rectangle to the global coordinate system.
* @param u The normalized distance along the x-axis of the rectangle.
* @param v The normalized distance along the y-axis of the rectangle.
* @returns The uvPoint remapped to the global coordinate system.
*/
public pointAt(u: number, v: number): Point;
/**
* Remaps a point from the u-v space of the rectangle to the global coordinate system.
* @param uvPoint A point in the u-v coordinates of the rectangle.
* The point's x value is the normalized distance along the x-axis of the rectangle (u-direction).
* The point's y value is the normalized distance along the y-axis of the rectangle (v-direction).
* @returns The uvPoint remapped to the global coordinate system.
*/
public pointAt(uvPoint: Point): Point;
public pointAt(uvPointorU: Point | number, v?: number): Point {
if (uvPointorU instanceof Point) {
return this._plane.pointAt(
this._x.valueAt(uvPointorU.x),
this._y.valueAt(uvPointorU.y)
);
}
if (v === undefined) {
/* istanbul ignore next */
throw new Error("Shouldn't be possible");
}
return this._plane.pointAt(this._x.valueAt(uvPointorU), this._y.valueAt(v));
}
/***
* Gets the rectangle as a string in the format: `[plane,widthX,widthY]`.
*/
public toString(): string {
return (
'[' + this._plane.toString() + ',' + this.widthX + ',' + this.widthY + ']'
);
}
/***
* Gets the rectangle as a closed polyline.
*/
public toPolyline(): Polyline {
const poly = new Polyline(this.getCorners(), true);
return poly;
}
/**
* Creates a copy of the rectangle with a different [[plane]].
*
* @param newPlane The new plane for the rectangle. This will change the rectangle's location and orientation.
*/
public withPlane(newPlane: Plane): Rectangle {
return new Rectangle(newPlane, this._x, this._y);
}
/**
* Creates a copy of the rectangle with a different [[x]] interval.
* @param newX The new position of the rectangle along the x-axis of the plane.
*/
public withX(newX: IntervalSorted): Rectangle {
return new Rectangle(this._plane, newX, this._y);
}
/**
* Creates a copy of the rectangle with a different [[y]] interval.
* @param newY The new position of the rectangle along the y-axis of the plane.
*/
public withY(newY: IntervalSorted): Rectangle {
return new Rectangle(this._plane, this._x, newY);
}
// -----------------------
// TRANSFORMABLE
// -----------------------
/***
* Transforms the rectangle by a [[transform]] matrix and returns the result.
*
* ### Example
* ```js
* import { Plane, Rectangle, Transform } from 'shapetypes';
*
* // Create a new rectangle
* const rect = new Rectangle(Plane.worldXY(), 10, 20);
* console.log(rect.area);
* // => 200
*
* // Scale rectangle using a transform matrix
* const matrix = Transform.scale(2);
* const scaled = rect.transform(matrix);
* console.log(scaled.area);
* // => 800
*
* // Scale rectangle using the direct method
* const otherScaled = rect.scale(2);
* console.log(otherScaled.area);
* // => 800
* ```
*
* @note If you're applying the same transformation a lot of geometry,
* creating the [[Transform]] matrix once and calling this function is faster
* than using the direct methods.
*
* @category Transform
* @param change The [[transform]] matrix to apply to the rectangle.
*/
public transform(change: Transform): this {
const cornerA = change.transformPoint(this.corner(true, true));
const cornerB = change.transformPoint(this.corner(false, false));
const plane = this._plane.transform(change);
// @ts-ignore
return Rectangle.fromCorners(cornerA, cornerB, plane);
}
}