-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathRectanglei.java
665 lines (619 loc) · 19.3 KB
/
Rectanglei.java
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
* The MIT License
*
* Copyright (c) 2020 JOML
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.joml;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* Represents a 2D axis-aligned rectangle.
*
* @author Kai Burjack
*/
public class Rectanglei implements Externalizable {
/**
* The x coordinate of the minimum corner.
*/
public int minX;
/**
* The y coordinate of the minimum corner.
*/
public int minY;
/**
* The x coordinate of the maximum corner.
*/
public int maxX;
/**
* The y coordinate of the maximum corner.
*/
public int maxY;
/**
* Create a new {@link Rectanglei} with a minimum and maximum corner of <code>(0, 0)</code>.
*/
public Rectanglei() {
}
/**
* Create a new {@link Rectanglei} as a copy of the given <code>source</code>.
*
* @param source
* the {@link Rectanglei} to copy from
*/
public Rectanglei(Rectanglei source) {
this.minX = source.minX;
this.minY = source.minY;
this.maxX = source.maxX;
this.maxY = source.maxY;
}
/**
* Create a new {@link Rectanglei} with the given <code>min</code> and <code>max</code> corner coordinates.
*
* @param min
* the minimum coordinates
* @param max
* the maximum coordinates
*/
public Rectanglei(Vector2ic min, Vector2ic max) {
this.minX = min.x();
this.minY = min.y();
this.maxX = max.x();
this.maxY = max.y();
}
/**
* Create a new {@link Rectanglei} with the given minimum and maximum corner coordinates.
*
* @param minX
* the x coordinate of the minimum corner
* @param minY
* the y coordinate of the minimum corner
* @param maxX
* the x coordinate of the maximum corner
* @param maxY
* the y coordinate of the maximum corner
*/
public Rectanglei(int minX, int minY, int maxX, int maxY) {
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}
/**
* Return the length of the rectangle in the X dimension.
*
* @return length in the X dimension
*/
public int lengthX() {
return maxX - minX;
}
/**
* Return the length of the rectangle in the Y dimension.
*
* @return length in the Y dimension
*/
public int lengthY() {
return maxY - minY;
}
/**
* Return the area of the rectangle
*
* @return area
*/
public int area() {
return lengthX() * lengthY();
}
/**
* Check if this and the given rectangle intersect.
*
* @param other
* the other rectangle
* @return <code>true</code> iff both rectangles intersect; <code>false</code> otherwise
*/
public boolean intersects(Rectangled other) {
return minX < other.maxX && maxX >= other.minX &&
maxY >= other.minY && minY < other.maxY;
}
/**
* Check if this and the given rectangle intersect.
*
* @param other
* the other rectangle
* @return <code>true</code> iff both rectangles intersect; <code>false</code> otherwise
*/
public boolean intersects(Rectanglef other) {
return minX < other.maxX && maxX >= other.minX &&
maxY >= other.minY && minY < other.maxY;
}
/**
* Check if this and the given rectangle intersect.
*
* @param other
* the other rectangle
* @return <code>true</code> iff both rectangles intersect; <code>false</code> otherwise
*/
public boolean intersects(Rectanglei other) {
return minX < other.maxX && maxX >= other.minX &&
maxY >= other.minY && minY < other.maxY;
}
private Rectanglei validate() {
if (!isValid()) {
minX = Integer.MAX_VALUE;
minY = Integer.MAX_VALUE;
maxX = Integer.MIN_VALUE;
maxY = Integer.MIN_VALUE;
}
return this;
}
/**
* Check whether <code>this</code> rectangle represents a valid rectangle.
*
* @return <code>true</code> iff this rectangle is valid; <code>false</code> otherwise
*/
public boolean isValid() {
return minX < maxX && minY < maxY;
}
/**
* Compute the rectangle of intersection between <code>this</code> and the given rectangle.
* <p>
* If the two rectangles do not intersect, then the minimum coordinates of <code>this</code>
* will have a value of {@link Integer#MAX_VALUE} and the maximum coordinates will have a value of
* {@link Integer#MIN_VALUE}.
*
* @param other
* the other rectangle
* @return this
*/
public Rectanglei intersection(Rectanglei other) {
return intersection(other, this);
}
/**
* Compute the rectangle of intersection between <code>this</code> and the given rectangle and
* store the result in <code>dest</code>.
* <p>
* If the two rectangles do not intersect, then the minimum coordinates of <code>dest</code>
* will have a value of {@link Integer#MAX_VALUE} and the maximum coordinates will have a value of
* {@link Integer#MIN_VALUE}.
*
* @param other
* the other rectangle
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei intersection(Rectanglei other, Rectanglei dest) {
dest.minX = Math.max(minX, other.minX);
dest.minY = Math.max(minY, other.minY);
dest.maxX = Math.min(maxX, other.maxX);
dest.maxY = Math.min(maxY, other.maxY);
return dest.validate();
}
/**
* Return the length of this rectangle in the X and Y dimensions and store the result in <code>dest</code>.
*
* @param dest
* will hold the result
* @return dest
*/
public Vector2i lengths(Vector2i dest) {
return dest.set(lengthX(), lengthY());
}
/**
* Check if this rectangle contains the given <code>rectangle</code>.
*
* @param rectangle
* the rectangle to test
* @return <code>true</code> iff this rectangle contains the rectangle; <code>false</code> otherwise
*/
public boolean contains(Rectangled rectangle) {
return rectangle.minX >= minX && rectangle.maxX <= maxX &&
rectangle.minY >= minY && rectangle.maxY <= maxY;
}
/**
* Check if this rectangle contains the given <code>rectangle</code>.
*
* @param rectangle
* the rectangle to test
* @return <code>true</code> iff this rectangle contains the rectangle; <code>false</code> otherwise
*/
public boolean contains(Rectanglef rectangle) {
return rectangle.minX >= minX && rectangle.maxX <= maxX &&
rectangle.minY >= minY && rectangle.maxY <= maxY;
}
/**
* Check if this rectangle contains the given <code>rectangle</code>.
*
* @param rectangle
* the rectangle to test
* @return <code>true</code> iff this rectangle contains the rectangle; <code>false</code> otherwise
*/
public boolean contains(Rectanglei rectangle) {
return rectangle.minX >= minX && rectangle.maxX <= maxX &&
rectangle.minY >= minY && rectangle.maxY <= maxY;
}
/**
* Check if this rectangle contains the given <code>point</code>.
*
* @param point
* the point to test
* @return <code>true</code> iff this rectangle contains the point; <code>false</code> otherwise
*/
public boolean contains(Vector2ic point) {
return contains(point.x(), point.y());
}
/**
* Check if this rectangle contains the given point <code>(x, y)</code>.
*
* @param x
* the x coordinate of the point to check
* @param y
* the y coordinate of the point to check
* @return <code>true</code> iff this rectangle contains the point; <code>false</code> otherwise
*/
public boolean contains(int x, int y) {
return x >= minX && y >= minX && x < maxX && y < maxY;
}
/**
* Translate <code>this</code> by the given vector <code>xy</code>.
*
* @param xy
* the vector to translate by
* @return this
*/
public Rectanglei translate(Vector2ic xy) {
return translate(xy.x(), xy.y(), this);
}
/**
* Translate <code>this</code> by the given vector <code>xy</code> and store the result in <code>dest</code>.
*
* @param xy
* the vector to translate by
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei translate(Vector2ic xy, Rectanglei dest) {
return translate(xy.x(), xy.y(), dest);
}
/**
* Translate <code>this</code> by the vector <code>(x, y)</code>.
*
* @param x
* the x coordinate to translate by
* @param y
* the y coordinate to translate by
* @return this
*/
public Rectanglei translate(int x, int y) {
return translate(x, y, this);
}
/**
* Translate <code>this</code> by the vector <code>(x, y)</code> and store the result in <code>dest</code>.
*
* @param x
* the x coordinate to translate by
* @param y
* the y coordinate to translate by
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei translate(int x, int y, Rectanglei dest) {
dest.minX = minX + x;
dest.minY = minY + y;
dest.maxX = maxX + x;
dest.maxY = maxY + y;
return dest;
}
/**
* Scale <code>this</code> about the origin.
*
* @param sf
* the scaling factor in the x and y axis.
* @return this
*/
public Rectanglei scale(int sf) {
return scale(sf, sf);
}
/**
* Scale <code>this</code> about the origin and store the result in <code>dest</code>.
*
* @param sf
* the scaling factor in the x and y axis
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei scale(int sf, Rectanglei dest) {
return scale(sf, sf, dest);
}
/**
* Scale <code>this</code> about an anchor.
* <p>
* This is effectively equivalent to <br>
* <pre>
* translate(-ax, -ay);
* scale(sf);
* translate(ax, ay);
* </pre>
*
* @param sf
* the scaling factor in the x and y axis
* @param ax
* the x coordinate of the anchor
* @param ay
* the y coordinate of the anchor
* @return this
*/
public Rectanglei scale(int sf, int ax, int ay) {
return scale(sf, sf, ax, ay);
}
/**
* Scale <code>this</code> about an anchor and store the result in <code>dest</code>.
* <p>
* This is effectively equivalent to <br>
* <pre>
* translate(-ax, -ay);
* scale(sf);
* translate(ax, ay);
* </pre>
*
* @param sf
* the scaling factor in the x and y axis
* @param ax
* the x coordinate of the anchor
* @param ay
* the y coordinate of the anchor
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei scale(int sf, int ax, int ay, Rectanglei dest) {
return scale(sf, sf, ax, ay, dest);
}
/**
* Scale <code>this</code> about an anchor.
* <p>
* This is effectively equivalent to <br>
* <pre>
* translate(anchor.negate());
* scale(sf);
* translate(anchor.negate());
* </pre>
*
* @param sf
* the scaling factor in the x and y axis
* @param anchor
* the location of the anchor
* @return this
*/
public Rectanglei scale(int sf, Vector2ic anchor) {
return scale(sf, anchor.x(), anchor.y());
}
/**
* Scale <code>this</code> about an anchor and store the result in <code>dest</code>.
* <p>
* This is effectively equivalent to <br>
* <pre>
* translate(anchor.negate());
* scale(sf);
* translate(anchor.negate());
* </pre>
*
* @param sf
* the scaling factor in the x and y axis
* @param anchor
* the location of the anchor
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei scale(int sf, Vector2ic anchor, Rectanglei dest) {
return scale(sf, anchor.x(), anchor.y(), dest);
}
/**
* Scale <code>this</code> about the origin.
*
* @param sx
* the scaling factor on the x axis
* @param sy
* the scaling factor on the y axis
* @return this
*/
public Rectanglei scale(int sx, int sy) {
return scale(sx, sy, 0, 0);
}
/**
* Scale <code>this</code> about the origin and store the result in <code>dest</code>.
*
* @param sx
* the scaling factor on the x axis
* @param sy
* the scaling factor on the y axis
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei scale(int sx, int sy, Rectanglei dest) {
return scale(sx, sy, 0, 0, dest);
}
/**
* Scale <code>this</code> about an anchor.
* This is equivalent to <br>
* <pre>
* translate(-ax, -ay);
* scale(sx, sy);
* translate(ax, ay);
* </pre>
*
* @param sx
* the scaling factor on the x axis
* @param sy
* the scaling factor on the y axis
* @param ax
* the x coordinate of the anchor
* @param ay
* the y coordinate of the anchor
* @return this
*/
public Rectanglei scale(int sx, int sy, int ax, int ay) {
minX = (minX - ax) * sx + ax;
minY = (minY - ay) * sy + ay;
maxX = (maxX - ax) * sx + ax;
maxY = (maxY - ay) * sy + ay;
return this;
}
/**
* Scale <code>this</code> about an anchor.
* <p>
* This is equivalent to <br>
* <pre>
* translate(anchor.negate());
* scale(sx, sy);
* translate(anchor.negate());
* </pre>
*
* @param sx
* the scaling factor on the x axis
* @param sy
* the scaling factor on the y axis
* @param anchor
* the location of the anchor
* @return this
*/
public Rectanglei scale(int sx, int sy, Vector2ic anchor) {
return scale(sx, sy, anchor.x(), anchor.y());
}
/**
* Scale <code>this</code> about an anchor and store the result in <code>dest</code>.
* <p>
* This is equivalent to <br>
* <pre>
* translate(-ax, -ay);
* scale(sx, sy);
* translate(ax, ay);
* </pre>
*
* @param sx
* the scaling factor on the x axis
* @param sy
* the scaling factor on the y axis
* @param ax
* the x coordinate of the anchor
* @param ay
* the y coordinate of the anchor
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei scale(int sx, int sy, int ax, int ay, Rectanglei dest) {
dest.minX = (minX - ax) * sx + ax;
dest.minY = (minY - ay) * sy + ay;
dest.maxX = (maxX - ax) * sx + ax;
dest.maxY = (maxY - ay) * sy + ay;
return dest;
}
/**
* Scale <code>this</code> about an anchor and store the result in <code>dest</code>.
* <p>
* This is equivalent to <br>
* <pre>
* translate(anchor.negate());
* scale(sx, sy);
* translate(anchor.negate());
* </pre>
*
* @param sx
* the scaling factor on the x axis
* @param sy
* the scaling factor on the y axis
* @param anchor
* the location of the anchor
* @param dest
* will hold the result
* @return dest
*/
public Rectanglei scale(int sx, int sy, Vector2ic anchor, Rectanglei dest) {
return scale(sx, sy, anchor.x(), anchor.y(), dest);
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + maxX;
result = prime * result + maxY;
result = prime * result + minX;
result = prime * result + minY;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rectanglei other = (Rectanglei) obj;
if (maxX != other.maxX)
return false;
if (maxY != other.maxY)
return false;
if (minX != other.minX)
return false;
if (minY != other.minY)
return false;
return true;
}
/**
* Return a string representation of this rectangle.
* <p>
* This method creates a new {@link DecimalFormat} on every invocation with the format string "<code>0.000E0;-</code>".
*
* @return the string representation
*/
public String toString() {
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}
/**
* Return a string representation of this rectangle by formatting the vector components with the given {@link NumberFormat}.
*
* @param formatter
* the {@link NumberFormat} used to format the vector components with
* @return the string representation
*/
public String toString(NumberFormat formatter) {
return "(" + formatter.format(minX) + " " + formatter.format(minY) + ") < "
+ "(" + formatter.format(maxX) + " " + formatter.format(maxY) + ")";
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeFloat(minX);
out.writeFloat(minY);
out.writeFloat(maxX);
out.writeFloat(maxY);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
minX = in.readInt();
minY = in.readInt();
maxX = in.readInt();
maxY = in.readInt();
}
}