summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/geom/Rectangle2D.java
blob: d33dd91d429d4361db4fcb2d4e2e1405570328ba (plain)
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Denis M. Kishenko
 * @version $Revision$
 */
package java.awt.geom;

import java.util.NoSuchElementException;

import org.apache.harmony.awt.internal.nls.Messages;
import org.apache.harmony.misc.HashCode;

/**
 * The Class Rectangle2D represents a rectangle whose coordinates are given 
 * with the correct precision to be used with the Graphics2D classes.
 */
public abstract class Rectangle2D extends RectangularShape {

    /** The Constant OUT_LEFT is a mask that is used to indicate that a 
     * given point is outside the rectangle and to its left. */
    public static final int OUT_LEFT   = 1;
    
    /** The Constant OUT_TOP is a mask that is used to indicate that a 
     * given point is outside the rectangle and above it. */
    public static final int OUT_TOP    = 2;
    
    /** The Constant OUT_RIGHT is a mask that is used to indicate that a 
     * given point is outside the rectangle and to its right. */
    public static final int OUT_RIGHT  = 4;
    
    /** The Constant OUT_BOTTOM is a mask that is used to indicate that a 
     * given point is outside the rectangle and above it. */
    public static final int OUT_BOTTOM = 8;

    /**
     * The Class Float is the subclass of Rectangle2D that represents a 
     * rectangle whose data values are given as floats (with float-level
     * precision).
     */
    public static class Float extends Rectangle2D {

        /** The x coordinate of the rectangle's upper left corner. */
        public float x;
        
        /** The y coordinate of the rectangle's upper left corner. */
        public float y;
        
        /** The width of the rectangle. */
        public float width;
        
        /** The height of the rectangle. */
        public float height;

        /**
         * Instantiates a new empty rectangle with float-precision data fields.
         */
        public Float() {
        }

        /**
         * Instantiates a new rectangle with the specified float-precision data.
         * 
         * @param x the x coordinate of the rectangle's upper left corner
         * @param y the y coordinate of the rectangle's upper left corner
         * @param width the width of the rectangle
         * @param height the height of the rectangle
         */
        public Float(float x, float y, float width, float height) {
            setRect(x, y, width, height);
        }

        @Override
        public double getX() {
            return x;
        }

        @Override
        public double getY() {
            return y;
        }

        @Override
        public double getWidth() {
            return width;
        }

        @Override
        public double getHeight() {
            return height;
        }

        @Override
        public boolean isEmpty() {
            return width <= 0.0f || height <= 0.0f;
        }

        /**
         * Sets the rectangle's data to the given values.
         * 
         * @param x the x coordinate of the rectangle's upper left corner
         * @param y the y coordinate of the rectangle's upper left corner
         * @param width the width of the rectangle
         * @param height the height of the rectangle
         */
        public void setRect(float x, float y, float width, float height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        @Override
        public void setRect(double x, double y, double width, double height) {
            this.x = (float)x;
            this.y = (float)y;
            this.width = (float)width;
            this.height = (float)height;
        }

        @Override
        public void setRect(Rectangle2D r) {
            this.x = (float)r.getX();
            this.y = (float)r.getY();
            this.width = (float)r.getWidth();
            this.height = (float)r.getHeight();
        }

        @Override
        public int outcode(double px, double py) {
            int code = 0;

            if (width <= 0.0f) {
                code |= OUT_LEFT | OUT_RIGHT;
            } else
                if (px < x) {
                    code |= OUT_LEFT;
                } else
                    if (px > x + width) {
                        code |= OUT_RIGHT;
                    }

            if (height <= 0.0f) {
                code |= OUT_TOP | OUT_BOTTOM;
            } else
                if (py < y) {
                    code |= OUT_TOP;
                } else
                    if (py > y + height) {
                        code |= OUT_BOTTOM;
                    }

            return code;
        }

        @Override
        public Rectangle2D getBounds2D() {
            return new Float(x, y, width, height);
        }

        @Override
        public Rectangle2D createIntersection(Rectangle2D r) {
            Rectangle2D dst;
            if (r instanceof Double) {
                dst = new Rectangle2D.Double();
            } else {
                dst = new Rectangle2D.Float();
            }
            Rectangle2D.intersect(this, r, dst);
            return dst;
        }

        @Override
        public Rectangle2D createUnion(Rectangle2D r) {
            Rectangle2D dst;
            if (r instanceof Double) {
                dst = new Rectangle2D.Double();
            } else {
                dst = new Rectangle2D.Float();
            }
            Rectangle2D.union(this, r, dst);
            return dst;
        }

        @Override
        public String toString() {
            // The output format based on 1.5 release behaviour. It could be obtained in the following way
            // System.out.println(new Rectangle2D.Float().toString())
            return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
        }
    }

    /**
     * The Class Double is the subclass of Rectangle2D that represents a 
     * rectangle whose data values are given as doubles (with double-precision-level
     * precision).
     */
    public static class Double extends Rectangle2D {

        /** The x coordinate of the rectangle's upper left corner. */
        public double x;
        
        /** The y coordinate of the rectangle's upper left corner. */
        public double y;
        
        /** The width of the rectangle. */
        public double width;
        
        /** The height of the rectangle. */
        public double height;

        /**
         * Instantiates a new empty rectangle with double-precision data fields.
         */
        public Double() {
        }

        /**
         * Instantiates a new rectangle with the given double values.
         * 
         * @param x the x coordinate of the rectangle's upper left corner
         * @param y the y coordinate of the rectangle's upper left corner
         * @param width the width of the rectangle
         * @param height the height of the rectangle
         */
        public Double(double x, double y, double width, double height) {
            setRect(x, y, width, height);
        }

        @Override
        public double getX() {
            return x;
        }

        @Override
        public double getY() {
            return y;
        }

        @Override
        public double getWidth() {
            return width;
        }

        @Override
        public double getHeight() {
            return height;
        }

        @Override
        public boolean isEmpty() {
            return width <= 0.0 || height <= 0.0;
        }

        @Override
        public void setRect(double x, double y, double width, double height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        @Override
        public void setRect(Rectangle2D r) {
            this.x = r.getX();
            this.y = r.getY();
            this.width = r.getWidth();
            this.height = r.getHeight();
        }

        @Override
        public int outcode(double px, double py) {
            int code = 0;

            if (width <= 0.0) {
                code |= OUT_LEFT | OUT_RIGHT;
            } else
                if (px < x) {
                    code |= OUT_LEFT;
                } else
                    if (px > x + width) {
                        code |= OUT_RIGHT;
                    }

            if (height <= 0.0) {
                code |= OUT_TOP | OUT_BOTTOM;
            } else
                if (py < y) {
                    code |= OUT_TOP;
                } else
                    if (py > y + height) {
                        code |= OUT_BOTTOM;
                    }

            return code;
        }

        @Override
        public Rectangle2D getBounds2D() {
            return new Double(x, y, width, height);
        }

        @Override
        public Rectangle2D createIntersection(Rectangle2D r) {
            Rectangle2D dst = new Rectangle2D.Double();
            Rectangle2D.intersect(this, r, dst);
            return dst;
        }

        @Override
        public Rectangle2D createUnion(Rectangle2D r) {
            Rectangle2D dest = new Rectangle2D.Double();
            Rectangle2D.union(this, r, dest);
            return dest;
        }

        @Override
        public String toString() {
            // The output format based on 1.5 release behaviour. It could be obtained in the following way
            // System.out.println(new Rectangle2D.Double().toString())
            return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
        }
    }

    /**
     * The Class Iterator provides 
     * access to the coordinates of the Rectangle2D's boundary modified 
     * by an AffineTransform. 
     */
    class Iterator implements PathIterator {

        /** The x coordinate of the rectangle's upper left corner. */
        double x;
        
        /** The y coordinate of the rectangle's upper left corner. */
        double y;
        
        
        /** The width of the rectangle. */
        double width;
        
        /** The height of the rectangle. */
        double height;
                
        /** The AffineTransform that is used to modify the coordinates 
         * that are returned by the path iterator. */
        AffineTransform t;
        
        /** The current segment index. */
        int index;
        
        /**
         * Constructs a new Rectangle2D.Iterator for given rectangle and transformation.
         * 
         * @param r - the source Rectangle2D object
         * @param at - the AffineTransform object to apply to the coordinates 
         * before returning them
         */
        Iterator(Rectangle2D r, AffineTransform at) {
            this.x = r.getX();
            this.y = r.getY();
            this.width = r.getWidth();
            this.height = r.getHeight();
            this.t = at;
            if (width < 0.0 || height < 0.0) {
                index = 6;
            }
        }

        public int getWindingRule() {
            return WIND_NON_ZERO;
        }

        public boolean isDone() {
            return index > 5;
        }

        public void next() {
            index++;
        }

        public int currentSegment(double[] coords) {
            if (isDone()) {
                throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$
            }
            if (index == 5) {
                return SEG_CLOSE;
            }
            int type;
            if (index == 0) {
                type = SEG_MOVETO;
                coords[0] = x;
                coords[1] = y;
            } else {
                type = SEG_LINETO;
                switch(index) {
                case 1:
                    coords[0] = x + width;
                    coords[1] = y;
                    break;
                case 2:
                    coords[0] = x + width;
                    coords[1] = y + height;
                    break;
                case 3:
                    coords[0] = x;
                    coords[1] = y + height;
                    break;
                case 4:
                    coords[0] = x;
                    coords[1] = y;
                    break;
                }
            }
            if (t != null) {
                t.transform(coords, 0, coords, 0, 1);
            }
            return type;
        }

        public int currentSegment(float[] coords) {
            if (isDone()) {
                throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$
            }
            if (index == 5) {
                return SEG_CLOSE;
            }
            int type;
            if (index == 0) {
                coords[0] = (float)x;
                coords[1] = (float)y;
                type = SEG_MOVETO;
            } else {
                type = SEG_LINETO;
                switch(index) {
                case 1:
                    coords[0] = (float)(x + width);
                    coords[1] = (float)y;
                    break;
                case 2:
                    coords[0] = (float)(x + width);
                    coords[1] = (float)(y + height);
                    break;
                case 3:
                    coords[0] = (float)x;
                    coords[1] = (float)(y + height);
                    break;
                case 4:
                    coords[0] = (float)x;
                    coords[1] = (float)y;
                    break;
                }
            }
            if (t != null) {
                t.transform(coords, 0, coords, 0, 1);
            }
            return type;
        }

    }

    /**
     * Instantiates a new rectangle2 d.
     */
    protected Rectangle2D() {
    }

    /**
     * Sets the rectangle's location and dimension.
     * 
     * @param x the x coordinate of the rectangle's upper left corner
     * @param y the y coordinate of the rectangle's upper left corner
     * @param width the width of the rectangle
     * @param height the height of the rectangle
     */
    public abstract void setRect(double x, double y, double width, double height);

    /**
     * Gets the location of the point with respect to the rectangle and 
     * packs the information into a single int using the bitmasks 
     * {@link Rectangle2D#OUT_LEFT}, {@link Rectangle2D#OUT_RIGHT}, 
     * {@link Rectangle2D#OUT_TOP}, and {@link Rectangle2D#OUT_BOTTOM}.
     * If the rectangle has zero or negative width, then every point
     * is regarded as being both to the left and to the right of the
     * rectangle. Similarly, if the height is zero or negative then 
     * all points are considered to be both both above and below it.
     * 
     * @param x the x coordinate of the point to check
     * @param y the y coordinate of the point to check
     * 
     * @return the point's location with respect to the rectangle.
     */
    public abstract int outcode(double x, double y);

    /**
     * Creates an new rectangle that is the intersection of this rectangle
     * with the given rectangle. The resulting rectangle may be empty. 
     * The data of this rectangle is left unchanged.
     * 
     * @param r the rectangle to intersect with this rectangle.
     * 
     * @return the new rectangle given by intersection.
     */
    public abstract Rectangle2D createIntersection(Rectangle2D r);

    /**
     * Creates an new rectangle that is the union of this rectangle
     * with the given rectangle. The new rectangle is the smallest 
     * rectangle which contains both this rectangle and the rectangle
     * specified as a parameter. The data of this rectangle is left unchanged.
     * 
     * @param r the rectangle to combine with this rectangle
     * 
     * @return the new rectangle given by union
     */
    public abstract Rectangle2D createUnion(Rectangle2D r);

    /**
     * Sets the data of this rectangle to match the data of the given 
     * rectangle.
     * 
     * @param r the rectangle whose data is to be copied into this rectangle's fields.
     */
    public void setRect(Rectangle2D r) {
        setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    }

    @Override
    public void setFrame(double x, double y, double width, double height) {
        setRect(x, y, width, height);
    }

    public Rectangle2D getBounds2D() {
        return (Rectangle2D)clone();
    }

    /**
     * Determines whether any part of the line segment  between (and including) 
     * the two given points touches any 
     * part of the rectangle, including its boundary.
     * 
     * @param x1 the x coordinate of one of the points that determines the 
     * line segment to test
     * @param y1 the y coordinate of one of the points that determines the 
     * line segment to test
     * @param x2 the x coordinate of one of the points that determines the 
     * line segment to test
     * @param y2 the y coordinate of one of the points that determines the 
     * line segment to test
     * 
     * @return true, if at least one point of the line segment between the 
     * two points matches any point of the interior of the rectangle or the
     * rectangle's boundary.
     */
    public boolean intersectsLine(double x1, double y1, double x2, double y2) {
        double rx1 = getX();
        double ry1 = getY();
        double rx2 = rx1 + getWidth();
        double ry2 = ry1 + getHeight();
        return
            (rx1 <= x1 && x1 <= rx2 && ry1 <= y1 && y1 <= ry2) ||
            (rx1 <= x2 && x2 <= rx2 && ry1 <= y2 && y2 <= ry2) ||
            Line2D.linesIntersect(rx1, ry1, rx2, ry2, x1, y1, x2, y2) ||
            Line2D.linesIntersect(rx2, ry1, rx1, ry2, x1, y1, x2, y2);
    }

    /**
     * Determines whether any part of the specified line segment touches any 
     * part of the rectangle, including its boundary.
     * 
     * @param l the line segment to test
     * 
     * @return true, if at least one point of the given line segment 
     * matches any point of the interior of the rectangle or the
     * rectangle's boundary.
     */
    public boolean intersectsLine(Line2D l) {
        return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
    }

    /**
     * Gets the location of the point with respect to the rectangle and 
     * packs the information into a single int using the bitmasks 
     * {@link Rectangle2D#OUT_LEFT}, {@link Rectangle2D#OUT_RIGHT}, 
     * {@link Rectangle2D#OUT_TOP}, and {@link Rectangle2D#OUT_BOTTOM}.
     * If the rectangle has zero or negative width, then every point
     * is regarded as being both to the left and to the right of the
     * rectangle. Similarly, if the height is zero or negative then 
     * all points are considered to be both both above and below it.
     * 
     * @param p the point to check
     * 
     * @return the point's location with respect to the rectangle.
     */
    public int outcode(Point2D p) {
        return outcode(p.getX(), p.getY());
    }

    public boolean contains(double x, double y) {
        if (isEmpty()) {
            return false;
        }

        double x1 = getX();
        double y1 = getY();
        double x2 = x1 + getWidth();
        double y2 = y1 + getHeight();

        return
            x1 <= x && x < x2 &&
            y1 <= y && y < y2;
    }

    public boolean intersects(double x, double y, double width, double height) {
        if (isEmpty() || width <= 0.0 || height <= 0.0) {
            return false;
        }

        double x1 = getX();
        double y1 = getY();
        double x2 = x1 + getWidth();
        double y2 = y1 + getHeight();

        return
            x + width > x1 && x < x2 &&
            y + height > y1 && y < y2;
    }

    public boolean contains(double x, double y, double width, double height) {
        if (isEmpty() || width <= 0.0 || height <= 0.0) {
            return false;
        }

        double x1 = getX();
        double y1 = getY();
        double x2 = x1 + getWidth();
        double y2 = y1 + getHeight();

        return
            x1 <= x && x + width <= x2 &&
            y1 <= y && y + height <= y2;
    }

    /**
     * Changes the data values of the destination rectangle to match 
     * the intersection of the two source rectangles, leaving the 
     * two source rectangles unchanged. The resulting rectangle may be empty. 
     * 
     * @param src1 one of the two source rectangles giving the data to intersect
     * @param src2 one of the two source rectangles giving the data to intersect
     * @param dst the destination object where the data of the intersection is written
     */
    public static void intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dst) {
        double x1 = Math.max(src1.getMinX(), src2.getMinX());
        double y1 = Math.max(src1.getMinY(), src2.getMinY());
        double x2 = Math.min(src1.getMaxX(), src2.getMaxX());
        double y2 = Math.min(src1.getMaxY(), src2.getMaxY());
        dst.setFrame(x1, y1, x2 - x1, y2 - y1);
    }

    /**
     * Changes the data values of the destination rectangle to match 
     * the union of the two source rectangles, leaving the 
     * two source rectangles unchanged. The union is the smallest rectangle
     * tha completely covers the two source rectangles. 
     * 
     * @param src1 one of the two source rectangles giving the data
     * @param src2 one of the two source rectangles giving the data
     * @param dst the destination object where the data of the union is written
     */
    public static void union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dst) {
        double x1 = Math.min(src1.getMinX(), src2.getMinX());
        double y1 = Math.min(src1.getMinY(), src2.getMinY());
        double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
        double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
        dst.setFrame(x1, y1, x2 - x1, y2 - y1);
    }

    /**
     * Enlarges the rectangle so that it includes the given point.
     * 
     * @param x the x coordinate of the new point to be covered by the rectangle
     * @param y the y coordinate of the new point to be covered by the rectangle
     */
    public void add(double x, double y) {
        double x1 = Math.min(getMinX(), x);
        double y1 = Math.min(getMinY(), y);
        double x2 = Math.max(getMaxX(), x);
        double y2 = Math.max(getMaxY(), y);
        setRect(x1, y1, x2 - x1, y2 - y1);
    }

    /**
     * Enlarges the rectangle so that it includes the given point.
     * 
     * @param p the new point to be covered by the rectangle
     */
    public void add(Point2D p) {
        add(p.getX(), p.getY());
    }

    /**
     * Enlarges the rectangle so that it covers the given rectangle.
     * 
     * @param r the new rectangle to be covered by this rectangle
     */
    public void add(Rectangle2D r) {
        union(this, r, this);
    }

    public PathIterator getPathIterator(AffineTransform t) {
        return new Iterator(this, t);
    }

    @Override
    public PathIterator getPathIterator(AffineTransform t, double flatness) {
        return new Iterator(this, t);
    }

    @Override
    public int hashCode() {
        HashCode hash = new HashCode();
        hash.append(getX());
        hash.append(getY());
        hash.append(getWidth());
        hash.append(getHeight());
        return hash.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof Rectangle2D) {
            Rectangle2D r = (Rectangle2D)obj;
            return
                getX() == r.getX() &&
                getY() == r.getY() &&
                getWidth() == r.getWidth() &&
                getHeight() == r.getHeight();
        }
        return false;
    }

}