-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frame.js
634 lines (587 loc) · 18.8 KB
/
Frame.js
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/**
* APSheet Frame Class
* ---------------------------
* A Frame is a simple representation of
* a rectangle based upon Points.
* A Frame holds two primary points:
* - origin: the topleft corner
* - corner: the bottomright corner
* These two points are enough to make
* calculations about other features of the Frame.
*/
import { Point, isCoordinate } from "./Point.js";
const validateGeometry = (origin, corner) => {
if (origin.x > corner.x || origin.y > corner.y) {
throw "Origin must be top-left and corner must be bottom-right";
}
};
class Frame {
constructor(origin, corner) {
this.origin = new Point(origin);
this.corner = new Point(corner);
validateGeometry(this.origin, this.corner);
this.isFrame = true;
this.isEmpty = false;
// Bind instance methods
this.contains = this.contains.bind(this);
this.equals = this.equals.bind(this);
this.translate = this.translate.bind(this);
this.intersection = this.intersection.bind(this);
this.union = this.union.bind(this);
this.copy = this.copy.bind(this);
this.forEachPoint = this.forEachPoint.bind(this);
this.forEachCoordinate = this.forEachCoordinate.bind(this);
this.mapEachPoint = this.mapEachPoint.bind(this);
this.mapEachPointRow = this.mapEachPointRow.bind(this);
this.mapEachCoordinateRow = this.mapEachCoordinateRow.bind(this);
this.mapEachCoordinate = this.mapEachCoordinate.bind(this);
this.forEachCoordinateRow = this.forEachCoordinateRow.bind(this);
this.forEachPointRow = this.forEachPointRow.bind(this);
}
/**
* Checks whether or not the current
* instance circumscribes or contains
* the provided argument. Argument can
* be a Point, Array, or another Frame.
* @param {Array|Point|Frame} aPointOrFrame - The
* object to check for inclusion in the Frame
* @returns {boolean}
*/
contains(aPointOrFrame) {
if (this.isEmpty) {
return false;
}
if (aPointOrFrame.isPoint) {
let validX =
aPointOrFrame.x >= this.origin.x &&
aPointOrFrame.x <= this.corner.x;
let validY =
aPointOrFrame.y >= this.origin.y &&
aPointOrFrame.y <= this.corner.y;
return validX && validY;
} else if (aPointOrFrame.isFrame) {
return (
this.contains(aPointOrFrame.origin) &&
this.contains(aPointOrFrame.corner)
);
} else if (isCoordinate(aPointOrFrame)) {
let validX =
aPointOrFrame[0] >= this.origin.x &&
aPointOrFrame[0] <= this.corner.x;
let validY =
aPointOrFrame[1] >= this.origin.y &&
aPointOrFrame[1] <= this.corner.y;
return validX && validY;
}
return false;
}
/**
* Returns true if the Frame given in
* the argument has the same origin and
* corner values as this instance.
* @param {Frame} otherFrame - The other Frame
* to compare this instance to
* @returns {boolean}
*/
equals(otherFrame) {
if (this.isEmpty && otherFrame.isEmpty) {
return true;
} else if (this.isEmpty || otherFrame.isEmpty) {
return false;
}
return (
this.origin.equals(otherFrame.origin) &&
this.corner.equals(otherFrame.corner)
);
}
/**
* Given a Point, this method translates
* the origin and corner of the current
* Frame instance. If the inPlace argument
* is false, we return a new translated
* Frame instance instead of modifying the
* current instance.
* @param {Point} translationPoint - A Point
* representing how each of the x,y dimensions
* will be translated
* @param {boolean} inPlace - Whether or not to
* modify the current instance or return a new
* Frame instance.
* @returns {Frame}
*/
translate(translationPoint, inPlace = true) {
if (this.isEmpty) {
if (inPlace) {
return this;
}
return this.constructor.newEmpty();
}
if (inPlace) {
let newFrame = new Frame(this.origin, this.corner);
newFrame.translate(translationPoint, false);
return newFrame;
}
this.corner.x += translationPoint.x;
this.corner.y += translationPoint.y;
this.origin.x += translationPoint.x;
this.origin.y += translationPoint.y;
return this;
}
/**
* Returns a new Frame that represents the
* intersection of the current instance and
* the Frame passed in as an argument.
* If there is no valid intersection, it
* returns a new empty Frame.
* @param {Frame} otherFrame - A Frame to
* compare for intersectionality
* @returns {Frame} - A new Frame instance
* that represents the intersection of myself
* and the passed-in Frame. Will be an empty
* Frame if there is no intersection
*/
intersection(otherFrame) {
if (this.isEmpty || otherFrame.isEmpty) {
return this.constructor.newEmpty();
}
if (this.contains(otherFrame)) {
/*
* TTTTTTTT
* TTOOOTTT
* TTOOOTTT
* TTOOOTTT
* TTTTTTTT
*/
return new Frame(otherFrame.origin, otherFrame.corner);
}
if (otherFrame.contains(this)) {
/*
* OOOOOOOO
* OOTTTOOO
* OOTTTOOO
* OOTTTOOO
* OOOOOOOO
*/
return new Frame(this.origin, this.corner);
}
if (
this.contains(otherFrame.origin) &&
otherFrame.contains(this.corner)
) {
/*
* TTTTTT
* TTTOOOOOO
* TTTOOOOOO
* TTTOOOOOO
* OOOOOO
*/
return new Frame(otherFrame.origin, this.corner);
}
if (
otherFrame.contains(this.origin) &&
this.contains(otherFrame.corner)
) {
/*
* OOOOOO
* OOOOOO
* OOOTTTTTT
* OOOTTTTTT
* TTTTTT
* TTTTTT
*/
return new Frame(this.origin, otherFrame.corner);
}
// If we get here, we possibly have a more complex
// intersection (at top right, bottom left, etc).
// We create a union frame and then get the min and
// max for all intersection points
let unionFrame = this.union(otherFrame);
let intersectPoints = unionFrame.points.filter((aPoint) => {
return this.contains(aPoint) && otherFrame.contains(aPoint);
});
if (intersectPoints.length > 0) {
let xVals = intersectPoints.map((aPoint) => {
return aPoint.x;
});
let yVals = intersectPoints.map((aPoint) => {
return aPoint.y;
});
let newOrigin = new Point([Math.min(...xVals), Math.min(...yVals)]);
let newCorner = new Point([Math.max(...xVals), Math.max(...yVals)]);
return new Frame(newOrigin, newCorner);
}
return this.constructor.newEmpty();
}
/**
* Returns a new Frame instance that
* encloses the total areas of both
* this frame and the incoming frame
* @param {Frame} otherFrame - A Frame
* instance that will be used to compute
* the union between it and myself
* @returns {Frame} - A new Frame instance
* representing the enclosure of myself and
* the passed in Frame.
*/
union(otherFrame) {
let cornerX;
let cornerY;
let originX;
let originY;
let newOrigin = new Point([
Math.min(this.origin.x, otherFrame.origin.x),
Math.min(this.origin.y, otherFrame.origin.y),
]);
let newCorner = new Point([
Math.max(this.corner.x, otherFrame.corner.x),
Math.max(this.corner.y, otherFrame.corner.y),
]);
return new Frame(newOrigin, newCorner);
}
/**
* Run the specified callback with each Point
* in the Frame's total collection of points.
* @param {function} callback - The function
* to call over each point.
*/
forEachPoint(callback) {
if (this.isEmpty) {
return;
}
for (let x = this.origin.x; x <= this.corner.x; x++) {
for (let y = this.origin.y; y <= this.corner.y; y++) {
callback(new Point([x, y]));
}
}
}
/**
* Accumulate the result of the specified callback
* in an array that we return for each Point
* in the Frame's total collection of points.
* @param {function} callback - The function to
* call whose return value will be accumulated
* in a result array.
* @returns {Array} An accumulated array of
* objects.
*/
mapEachPoint(callback) {
let result = [];
this.forEachPoint((point) => {
result.push(callback(point));
});
return result;
}
/**
* Call the incoming callback function
* for each Coordinate created from the
* Frame's total collection of Points.
* @param {function} callback - The function
* to call on each coordinate array
*/
forEachCoordinate(callback) {
if (this.isEmpty) {
return;
}
for (let x = this.origin.x; x <= this.corner.x; x++) {
for (let y = this.origin.y; y <= this.corner.y; y++) {
callback([x, y]);
}
}
}
/**
* Accumulate the result of the specified callback
* in an array as it is applied to each coordinate
* in the frame's total collection of points.
* @param {function} callback - The function to call
* whose return value will be accumulated in the
* result array.
* @returns {Array} The resulting array of mapped return
* valued from the callback
*/
mapEachCoordinate(callback) {
let result = [];
this.mapEachCoordinate((coordinate) => {
result.push(callback(coordinate));
});
return result;
}
/**
* I loop through each "row" of Coordinates inside
* myself and call the passed-in callback
* with the array of Coordinates and the row
* index as the two aarguments.
* @param {function} callback - A function whose
* arguments will be an array of Coordinates and
* the row index
*/
forEachCoordinateRow(callback) {
if (this.isEmpty) {
return;
}
for (let y = this.origin.y; y <= this.corner.y; y++) {
let row = [];
for (let x = this.origin.x; x <= this.corner.x; x++) {
row.push([x, y]);
}
callback(row, y);
}
}
/**
* I loop through each "row" of Coordinates inside
* myself and call the passed-in callback
* with the array of Coordinates and the row
* index as the two aarguments.
* I return a new array which is the result of calling
* the specified callback on each row.
* @param {function} callback - A function whose
* arguments will be an array of Coordinates and
* the row index
*/
mapEachCoordinateRow(callback) {
if (this.isEmpty) {
return;
}
let result = [];
for (let y = this.origin.y; y <= this.corner.y; y++) {
let row = [];
for (let x = this.origin.x; x <= this.corner.x; x++) {
row.push([x, y]);
}
result.push(callback(row, y));
}
return result;
}
/**
* I loop through each "row" of Points inside
* myself and call the passed-in callback
* with the array of Points and the row
* index as the two aarguments.
* @param {function} callback - A function whose
* arguments will be an array of Points and
* the row index
*/
forEachPointRow(callback) {
this.forEachCoordinateRow((row, rowIndex) => {
let pointRow = row.map((col) => {
return new Point(col);
});
callback(pointRow, rowIndex);
});
}
/**
* I loop through each "row" of Points inside
* myself and call the passed-in callback
* with the array of Points and the row
* index as the two aarguments.
* I respond with a new array containing the result
* of calling the callback on each row.
* @param {function} callback - A function whose
* arguments will be an array of Points and
* the row index
*/
mapEachPointRow(callback) {
return this.mapEachCoordinateRow((row, rowIndex) => {
let pointRow = row.map((col) => {
return new Point(col);
});
return callback(pointRow);
});
}
/**
* Returns an array of Points that are
* mapped to all Points contained within
* the Frame.
* The order is from the x dimension first.
* @returns {[Point]} - An array of Points
*/
get points() {
let points = [];
if (!this.isEmpty) {
for (let x = this.origin.x; x <= this.corner.x; x++) {
for (let y = this.origin.y; y <= this.corner.y; y++) {
points.push(new Point([x, y]));
}
}
}
return points;
}
/**
* Returns an array of Coordinate pairs
* that are mapped to all points contained
* within this Frame.
* The order is from the x dimension first.
* @returns {[Array]} - An array of coordinate
* pairs
*/
get coordinates() {
let coordinates = [];
if (!this.isEmpty) {
for (let x = this.origin.x; x <= this.corner.x; x++) {
for (let y = this.origin.y; y <= this.corner.y; y++) {
coordinates.push([x, y]);
}
}
}
return coordinates;
}
/**
* Returns a Point corresponding
* to the size in dimensions, x
* being width and y being height.
* Will return Point(0,0) for empty
* Frames.
* @returns {Point} - A Point representing
* my size
*/
get size() {
if (this.isEmpty) {
return new Point([0, 0]);
}
let x = this.corner.x - this.origin.x + 1;
let y = this.corner.y - this.origin.y + 1;
return new Point([x, y]);
}
/**
* Return the number of dimensions for
* the current Frame.
* Note that a Frame with identical
* origin and corner is not necessarily
* 'empty' (0-dimensional), as it can
* contain a single point (cell) which
* is actually 1 dimensional.
* @returns {number} - A number representing
* my total number of dimensions
*/
get dimensions() {
if (this.isEmpty) {
return 0;
}
if (this.origin.equals(this.corner)) {
return 1;
}
return 2;
}
/**
* Returns the total number of contained
* Points/coordinates given the current
* origin and corner values.
* @returns {number} - A number representing
* my total number of contained Points
*/
get area() {
if (this.isEmpty) {
return 0;
}
let maxX = this.corner.x - this.origin.x + 1;
let maxY = this.corner.y - this.origin.y + 1;
return maxX * maxY;
}
/* Convenience Getters */
get left() {
return this.origin.x;
}
get right() {
return this.corner.x;
}
get top() {
return this.origin.y;
}
get bottom() {
return this.corner.y;
}
get topLeft() {
return new Point(this.origin);
}
get topRight() {
return new Point([this.corner.x, this.origin.y]);
}
get bottomLeft() {
return new Point([this.origin.x, this.corner.y]);
}
get bottomRight() {
return new Point(this.corner);
}
/**
* Return a new instance with the
* same origin, corner, and empty
* values as myself.
* @returns {Frame} - A new Frame instance
* that has my same origin, corner, and
* isEmpty values
*/
copy() {
let copyFrame = new Frame(this.origin, this.corner);
copyFrame.isEmpty = this.isEmpty;
return copyFrame;
}
/**
* Provide some comprehensible string
* representation of the frame
* @returns {String} - The string
* representation of myself
*/
toString() {
if (this.isEmpty) {
return `Frame(empty)`;
}
return `Frame(${this.origin}, ${this.corner})`;
}
/**
* Class method for constructing a new
* empty Frame.
* @returns {Frame} - A new Frame instance
* that has a 0,0 origin and corner and
* that is set to empty.
*/
static newEmpty() {
let newFrame = new this([0, 0], [0, 0]);
newFrame.isEmpty = true;
return newFrame;
}
/**
* Creates a new Frame instance based on two
* Points. Unlike the regular constructor, these
* points to not have to be the origin and corner,
* but could be any of the four corners.
* @param {Point|Coordinate} firstPoint - a Point
* or coordinate representing one possible corner
* of the desired Frame
* @param {Point|Coordinate} secondPoint - a Point
* or coordinate representing another possible
* corner of the desired Frame
* @returns {Frame} - A new Frame instance that has
* the correctly computed origin and corner
*/
static fromPointToPoint(firstPoint, secondPoint) {
if (firstPoint.equals(secondPoint)) {
return new Frame(firstPoint, secondPoint);
}
let origin = new Point([
Math.min(firstPoint.x, secondPoint.x),
Math.min(firstPoint.y, secondPoint.y),
]);
let corner = new Point([
Math.max(firstPoint.x, secondPoint.x),
Math.max(firstPoint.y, secondPoint.y),
]);
return new this(origin, corner);
}
/**
* I create a naw Frame with 0,0 origin
* and the correct size according to the
* passed-in dimensions.
* @param {number} width - The desired width
* of the new Frame
* @param {number} height - The desired height
* of thw new Frame
* @returns {Frame} - A new Frame instance with
* the correct origin and corner
*/
static newOfSize(width, height) {
// We return -1 for each
// dimension because Frame
// is zero-indexed
return new this([0, 0], [width - 1, height - 1]);
}
}
export { Frame as default, Frame };