summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/geom/Arc2D.java
blob: 56f5cd392d2283917c2889c42eda50438bb68318 (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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
/*
 *  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;

/**
 * The Class Arc2D represents a segment of a curve inscribed in a rectangle. The
 * curve is defined by a start angle and an extent angle (the end angle minus
 * the start angle) as a pie wedge whose point is in the center of the
 * rectangle. The Arc2D as a shape may be either OPEN (including nothing but the
 * curved arc segment itself), CHORD (the curved arc segment closed by a
 * connecting segment from the end to the beginning of the arc, or PIE (the
 * segments from the end of the arc to the center of the rectangle and from the
 * center of the rectangle back to the arc's start point are included).
 * 
 * @since Android 1.0
 */
public abstract class Arc2D extends RectangularShape {

    /**
     * The arc type OPEN indicates that the shape includes only the curved arc
     * segment.
     */
    public final static int OPEN = 0;

    /**
     * The arc type CHORD indicates that as a shape the connecting segment from
     * the end point of the curved arc to the beginning point is included.
     */
    public final static int CHORD = 1;

    /**
     * The arc type PIE indicates that as a shape the two segments from the
     * arc's endpoint to the center of the rectangle and from the center of the
     * rectangle to the arc's endpoint are included.
     */
    public final static int PIE = 2;

    /**
     * The Class Float is a subclass of Arc2D in which all of the data values
     * are given as floats.
     * 
     * @see Arc2D.Double
     * @since Android 1.0
     */
    public static class Float extends Arc2D {

        /**
         * The x coordinate of the upper left corner of the rectangle that
         * contains the arc.
         */
        public float x;

        /**
         * The y coordinate of the upper left corner of the rectangle that
         * contains the arc.
         */
        public float y;

        /**
         * The width of the rectangle that contains the arc.
         */
        public float width;

        /**
         * The height of the rectangle that contains the arc.
         */
        public float height;

        /**
         * The start angle of the arc in degrees.
         */
        public float start;

        /**
         * The width angle of the arc in degrees.
         */
        public float extent;

        /**
         * Instantiates a new Arc2D of type OPEN with float values.
         */
        public Float() {
            super(OPEN);
        }

        /**
         * Instantiates a new Arc2D of the specified type with float values.
         * 
         * @param type
         *            the type of the new Arc2D, either {@link Arc2D#OPEN},
         *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
         */
        public Float(int type) {
            super(type);
        }

        /**
         * Instantiates a Arc2D with the specified float-valued data.
         * 
         * @param x
         *            the x coordinate of the upper left corner of the rectangle
         *            that contains the arc.
         * @param y
         *            the y coordinate of the upper left corner of the rectangle
         *            that contains the arc.
         * @param width
         *            the width of the rectangle that contains the arc.
         * @param height
         *            the height of the rectangle that contains the arc.
         * @param start
         *            the start angle of the arc in degrees.
         * @param extent
         *            the width angle of the arc in degrees.
         * @param type
         *            the type of the new Arc2D, either {@link Arc2D#OPEN},
         *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
         */
        public Float(float x, float y, float width, float height, float start, float extent,
                int type) {
            super(type);
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.start = start;
            this.extent = extent;
        }

        /**
         * Instantiates a new Angle2D with the specified float-valued data and
         * the bounding rectangle given by the parameter bounds.
         * 
         * @param bounds
         *            the bounding rectangle of the Angle2D.
         * @param start
         *            the start angle of the arc in degrees.
         * @param extent
         *            the width angle of the arc in degrees.
         * @param type
         *            the type of the new Arc2D, either {@link Arc2D#OPEN},
         *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
         */
        public Float(Rectangle2D bounds, float start, float extent, int type) {
            super(type);
            this.x = (float)bounds.getX();
            this.y = (float)bounds.getY();
            this.width = (float)bounds.getWidth();
            this.height = (float)bounds.getHeight();
            this.start = start;
            this.extent = extent;
        }

        @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 double getAngleStart() {
            return start;
        }

        @Override
        public double getAngleExtent() {
            return extent;
        }

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

        @Override
        public void setArc(double x, double y, double width, double height, double start,
                double extent, int type) {
            this.setArcType(type);
            this.x = (float)x;
            this.y = (float)y;
            this.width = (float)width;
            this.height = (float)height;
            this.start = (float)start;
            this.extent = (float)extent;
        }

        @Override
        public void setAngleStart(double start) {
            this.start = (float)start;
        }

        @Override
        public void setAngleExtent(double extent) {
            this.extent = (float)extent;
        }

        @Override
        protected Rectangle2D makeBounds(double x, double y, double width, double height) {
            return new Rectangle2D.Float((float)x, (float)y, (float)width, (float)height);
        }

    }

    /**
     * The Class Double is a subclass of Arc2D in which all of the data values
     * are given as doubles.
     * 
     * @see Arc2D.Float
     * @since Android 1.0
     */
    public static class Double extends Arc2D {

        /**
         * The x coordinate of the upper left corner of the rectangle that
         * contains the arc.
         */
        public double x;

        /**
         * The y coordinate of the upper left corner of the rectangle that
         * contains the arc.
         */
        public double y;

        /**
         * The width of the rectangle that contains the arc.
         */
        public double width;

        /**
         * The height of the rectangle that contains the arc.
         */
        public double height;

        /**
         * The start angle of the arc in degrees.
         */
        public double start;

        /**
         * The width angle of the arc in degrees.
         */
        public double extent;

        /**
         * Instantiates a new Arc2D of type OPEN with double values.
         */
        public Double() {
            super(OPEN);
        }

        /**
         * Instantiates a new Arc2D of the specified type with double values.
         * 
         * @param type
         *            the type of the new Arc2D, either {@link Arc2D#OPEN},
         *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
         */
        public Double(int type) {
            super(type);
        }

        /**
         * Instantiates a Arc2D with the specified double-valued data.
         * 
         * @param x
         *            the x coordinate of the upper left corner of the rectangle
         *            that contains the arc.
         * @param y
         *            the y coordinate of the upper left corner of the rectangle
         *            that contains the arc.
         * @param width
         *            the width of the rectangle that contains the arc.
         * @param height
         *            the height of the rectangle that contains the arc.
         * @param start
         *            the start angle of the arc in degrees.
         * @param extent
         *            the width angle of the arc in degrees.
         * @param type
         *            the type of the new Arc2D, either {@link Arc2D#OPEN},
         *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
         */
        public Double(double x, double y, double width, double height, double start, double extent,
                int type) {
            super(type);
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.start = start;
            this.extent = extent;
        }

        /**
         * Instantiates a new Angle2D with the specified float-valued data and
         * the bounding rectangle given by the parameter bounds.
         * 
         * @param bounds
         *            the bounding rectangle of the Angle2D.
         * @param start
         *            the start angle of the arc in degrees.
         * @param extent
         *            the width angle of the arc in degrees.
         * @param type
         *            the type of the new Arc2D, either {@link Arc2D#OPEN},
         *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
         */
        public Double(Rectangle2D bounds, double start, double extent, int type) {
            super(type);
            this.x = bounds.getX();
            this.y = bounds.getY();
            this.width = bounds.getWidth();
            this.height = bounds.getHeight();
            this.start = start;
            this.extent = extent;
        }

        @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 double getAngleStart() {
            return start;
        }

        @Override
        public double getAngleExtent() {
            return extent;
        }

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

        @Override
        public void setArc(double x, double y, double width, double height, double start,
                double extent, int type) {
            this.setArcType(type);
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.start = start;
            this.extent = extent;
        }

        @Override
        public void setAngleStart(double start) {
            this.start = start;
        }

        @Override
        public void setAngleExtent(double extent) {
            this.extent = extent;
        }

        @Override
        protected Rectangle2D makeBounds(double x, double y, double width, double height) {
            return new Rectangle2D.Double(x, y, width, height);
        }

    }

    /**
     * The Class Iterator is the subclass of PathIterator that is used to
     * traverse the boundary of a shape of type Arc2D.
     */
    class Iterator implements PathIterator {

        /**
         * The x coordinate of the center of the arc's bounding rectangle.
         */
        double x;

        /**
         * The y coordinate of the center of the arc's bounding rectangle.
         */
        double y;

        /**
         * Half of the width of the arc's bounding rectangle (the radius in the
         * case of a circular arc).
         */
        double width;

        /**
         * Half of the height of the arc's bounding rectangle (the radius in the
         * case of a circular arc).
         */
        double height;

        /**
         * The start angle of the arc in degrees.
         */
        double angle;

        /**
         * The angle extent in degrees.
         */
        double extent;

        /**
         * The closure type of the arc.
         */
        int type;

        /**
         * The path iterator transformation.
         */
        AffineTransform t;

        /**
         * The current segment index.
         */
        int index;

        /**
         * The number of arc segments the source arc subdivided to be
         * approximated by Bezier curves. Depends on extent value.
         */
        int arcCount;

        /**
         * The number of line segments. Depends on closure type.
         */
        int lineCount;

        /**
         * The step to calculate next arc subdivision point.
         */
        double step;

        /**
         * The temporary value of cosinus of the current angle.
         */
        double cos;

        /**
         * The temporary value of sinus of the current angle.
         */
        double sin;

        /** The coefficient to calculate control points of Bezier curves. */
        double k;

        /**
         * The temporary value of x coordinate of the Bezier curve control
         * vector.
         */
        double kx;

        /**
         * The temporary value of y coordinate of the Bezier curve control
         * vector.
         */
        double ky;

        /**
         * The x coordinate of the first path point (MOVE_TO).
         */
        double mx;

        /**
         * The y coordinate of the first path point (MOVE_TO).
         */
        double my;

        /**
         * Constructs a new Arc2D.Iterator for given line and transformation
         * 
         * @param a
         *            the source Arc2D object.
         * @param t
         *            the AffineTransformation.
         */
        Iterator(Arc2D a, AffineTransform t) {
            if (width < 0 || height < 0) {
                arcCount = 0;
                lineCount = 0;
                index = 1;
                return;
            }

            this.width = a.getWidth() / 2.0;
            this.height = a.getHeight() / 2.0;
            this.x = a.getX() + width;
            this.y = a.getY() + height;
            this.angle = -Math.toRadians(a.getAngleStart());
            this.extent = -a.getAngleExtent();
            this.type = a.getArcType();
            this.t = t;

            if (Math.abs(extent) >= 360.0) {
                arcCount = 4;
                k = 4.0 / 3.0 * (Math.sqrt(2.0) - 1.0);
                step = Math.PI / 2.0;
                if (extent < 0.0) {
                    step = -step;
                    k = -k;
                }
            } else {
                arcCount = (int)Math.rint(Math.abs(extent) / 90.0);
                step = Math.toRadians(extent / arcCount);
                k = 4.0 / 3.0 * (1.0 - Math.cos(step / 2.0)) / Math.sin(step / 2.0);
            }

            lineCount = 0;
            if (type == Arc2D.CHORD) {
                lineCount++;
            } else if (type == Arc2D.PIE) {
                lineCount += 2;
            }
        }

        public int getWindingRule() {
            return WIND_NON_ZERO;
        }

        public boolean isDone() {
            return index > arcCount + lineCount;
        }

        public void next() {
            index++;
        }

        public int currentSegment(double[] coords) {
            if (isDone()) {
                // awt.4B=Iterator out of bounds
                throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$
            }
            int type;
            int count;
            if (index == 0) {
                type = SEG_MOVETO;
                count = 1;
                cos = Math.cos(angle);
                sin = Math.sin(angle);
                kx = k * width * sin;
                ky = k * height * cos;
                coords[0] = mx = x + cos * width;
                coords[1] = my = y + sin * height;
            } else if (index <= arcCount) {
                type = SEG_CUBICTO;
                count = 3;
                coords[0] = mx - kx;
                coords[1] = my + ky;
                angle += step;
                cos = Math.cos(angle);
                sin = Math.sin(angle);
                kx = k * width * sin;
                ky = k * height * cos;
                coords[4] = mx = x + cos * width;
                coords[5] = my = y + sin * height;
                coords[2] = mx + kx;
                coords[3] = my - ky;
            } else if (index == arcCount + lineCount) {
                type = SEG_CLOSE;
                count = 0;
            } else {
                type = SEG_LINETO;
                count = 1;
                coords[0] = x;
                coords[1] = y;
            }
            if (t != null) {
                t.transform(coords, 0, coords, 0, count);
            }
            return type;
        }

        public int currentSegment(float[] coords) {
            if (isDone()) {
                // awt.4B=Iterator out of bounds
                throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$
            }
            int type;
            int count;
            if (index == 0) {
                type = SEG_MOVETO;
                count = 1;
                cos = Math.cos(angle);
                sin = Math.sin(angle);
                kx = k * width * sin;
                ky = k * height * cos;
                coords[0] = (float)(mx = x + cos * width);
                coords[1] = (float)(my = y + sin * height);
            } else if (index <= arcCount) {
                type = SEG_CUBICTO;
                count = 3;
                coords[0] = (float)(mx - kx);
                coords[1] = (float)(my + ky);
                angle += step;
                cos = Math.cos(angle);
                sin = Math.sin(angle);
                kx = k * width * sin;
                ky = k * height * cos;
                coords[4] = (float)(mx = x + cos * width);
                coords[5] = (float)(my = y + sin * height);
                coords[2] = (float)(mx + kx);
                coords[3] = (float)(my - ky);
            } else if (index == arcCount + lineCount) {
                type = SEG_CLOSE;
                count = 0;
            } else {
                type = SEG_LINETO;
                count = 1;
                coords[0] = (float)x;
                coords[1] = (float)y;
            }
            if (t != null) {
                t.transform(coords, 0, coords, 0, count);
            }
            return type;
        }

    }

    /**
     * The closure type of the arc.
     */
    private int type;

    /**
     * Instantiates a new arc2D.
     * 
     * @param type
     *            the closure type.
     */
    protected Arc2D(int type) {
        setArcType(type);
    }

    /**
     * Takes the double-valued data and creates the corresponding Rectangle2D
     * object with values either of type float or of type double depending on
     * whether this Arc2D instance is of type Float or Double.
     * 
     * @param x
     *            the x coordinate of the upper left corner of the bounding
     *            rectangle.
     * @param y
     *            the y coordinate of the upper left corner of the bounding
     *            rectangle.
     * @param width
     *            the width of the bounding rectangle.
     * @param height
     *            the height of the bounding rectangle.
     * @return the corresponding Rectangle2D object.
     */
    protected abstract Rectangle2D makeBounds(double x, double y, double width, double height);

    /**
     * Gets the start angle.
     * 
     * @return the start angle.
     */
    public abstract double getAngleStart();

    /**
     * Gets the width angle.
     * 
     * @return the width angle.
     */
    public abstract double getAngleExtent();

    /**
     * Sets the start angle.
     * 
     * @param start
     *            the new start angle.
     */
    public abstract void setAngleStart(double start);

    /**
     * Sets the width angle.
     * 
     * @param extent
     *            the new width angle.
     */
    public abstract void setAngleExtent(double extent);

    /**
     * Sets the data values that define the arc.
     * 
     * @param x
     *            the x coordinate of the upper left corner of the rectangle
     *            that contains the arc.
     * @param y
     *            the y coordinate of the upper left corner of the rectangle
     *            that contains the arc.
     * @param width
     *            the width of the rectangle that contains the arc.
     * @param height
     *            the height of the rectangle that contains the arc.
     * @param start
     *            the start angle of the arc in degrees.
     * @param extent
     *            the width angle of the arc in degrees.
     * @param type
     *            the type of the new Arc2D, either {@link Arc2D#OPEN},
     *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
     */
    public abstract void setArc(double x, double y, double width, double height, double start,
            double extent, int type);

    /**
     * Gets the arc type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or
     * {@link Arc2D#PIE}.
     * 
     * @return the arc type.
     */
    public int getArcType() {
        return type;
    }

    /**
     * Sets the arc type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or
     * {@link Arc2D#PIE}.
     * 
     * @param type
     *            the new arc type.
     */
    public void setArcType(int type) {
        if (type != OPEN && type != CHORD && type != PIE) {
            // awt.205=Invalid type of Arc: {0}
            throw new IllegalArgumentException(Messages.getString("awt.205", type)); //$NON-NLS-1$
        }
        this.type = type;
    }

    /**
     * Gets the start point of the arc as a Point2D.
     * 
     * @return the start point of the curved arc segment.
     */
    public Point2D getStartPoint() {
        double a = Math.toRadians(getAngleStart());
        return new Point2D.Double(getX() + (1.0 + Math.cos(a)) * getWidth() / 2.0, getY()
                + (1.0 - Math.sin(a)) * getHeight() / 2.0);
    }

    /**
     * Gets the end point of the arc as a Point2D.
     * 
     * @return the end point of the curved arc segment.
     */
    public Point2D getEndPoint() {
        double a = Math.toRadians(getAngleStart() + getAngleExtent());
        return new Point2D.Double(getX() + (1.0 + Math.cos(a)) * getWidth() / 2.0, getY()
                + (1.0 - Math.sin(a)) * getHeight() / 2.0);
    }

    public Rectangle2D getBounds2D() {
        if (isEmpty()) {
            return makeBounds(getX(), getY(), getWidth(), getHeight());
        }
        double rx1 = getX();
        double ry1 = getY();
        double rx2 = rx1 + getWidth();
        double ry2 = ry1 + getHeight();

        Point2D p1 = getStartPoint();
        Point2D p2 = getEndPoint();

        double bx1 = containsAngle(180.0) ? rx1 : Math.min(p1.getX(), p2.getX());
        double by1 = containsAngle(90.0) ? ry1 : Math.min(p1.getY(), p2.getY());
        double bx2 = containsAngle(0.0) ? rx2 : Math.max(p1.getX(), p2.getX());
        double by2 = containsAngle(270.0) ? ry2 : Math.max(p1.getY(), p2.getY());

        if (type == PIE) {
            double cx = getCenterX();
            double cy = getCenterY();
            bx1 = Math.min(bx1, cx);
            by1 = Math.min(by1, cy);
            bx2 = Math.max(bx2, cx);
            by2 = Math.max(by2, cy);
        }
        return makeBounds(bx1, by1, bx2 - bx1, by2 - by1);
    }

    @Override
    public void setFrame(double x, double y, double width, double height) {
        setArc(x, y, width, height, getAngleStart(), getAngleExtent(), type);
    }

    /**
     * Sets the data that defines the arc.
     * 
     * @param point
     *            the upper left corner of the bounding rectangle.
     * @param size
     *            the size of the bounding rectangle.
     * @param start
     *            the start angle of the arc in degrees.
     * @param extent
     *            the angle width of the arc in degrees.
     * @param type
     *            the closure type, either {@link Arc2D#OPEN},
     *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
     */
    public void setArc(Point2D point, Dimension2D size, double start, double extent, int type) {
        setArc(point.getX(), point.getY(), size.getWidth(), size.getHeight(), start, extent, type);
    }

    /**
     * Sets the data that defines the arc.
     * 
     * @param rect
     *            the arc's bounding rectangle.
     * @param start
     *            the start angle of the arc in degrees.
     * @param extent
     *            the angle width of the arc in degrees.
     * @param type
     *            the closure type, either {@link Arc2D#OPEN},
     *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
     */
    public void setArc(Rectangle2D rect, double start, double extent, int type) {
        setArc(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), start, extent, type);
    }

    /**
     * Sets the data that defines the arc by copying it from another Arc2D.
     * 
     * @param arc
     *            the arc whose data is copied into this arc.
     */
    public void setArc(Arc2D arc) {
        setArc(arc.getX(), arc.getY(), arc.getWidth(), arc.getHeight(), arc.getAngleStart(), arc
                .getAngleExtent(), arc.getArcType());
    }

    /**
     * Sets the data for a circular arc by giving its center and radius.
     * 
     * @param x
     *            the x coordinate of the center of the circle.
     * @param y
     *            the y coordinate of the center of the circle.
     * @param radius
     *            the radius of the circle.
     * @param start
     *            the start angle of the arc in degrees.
     * @param extent
     *            the angle width of the arc in degrees.
     * @param type
     *            the closure type, either {@link Arc2D#OPEN},
     *            {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.
     */
    public void setArcByCenter(double x, double y, double radius, double start, double extent,
            int type) {
        setArc(x - radius, y - radius, radius * 2.0, radius * 2.0, start, extent, type);
    }

    /**
     * Sets the arc data for a circular arc based on two tangent lines and the
     * radius. The two tangent lines are the lines from p1 to p2 and from p2 to
     * p3, which determine a unique circle with the given radius. The start and
     * end points of the arc are the points where the circle touches the two
     * lines, and the arc itself is the shorter of the two circle segments
     * determined by the two points (in other words, it is the piece of the
     * circle that is closer to the lines' intersection point p2 and forms a
     * concave shape with the segments from p1 to p2 and from p2 to p3).
     * 
     * @param p1
     *            a point which determines one of the two tangent lines (with
     *            p2).
     * @param p2
     *            the point of intersection of the two tangent lines.
     * @param p3
     *            a point which determines one of the two tangent lines (with
     *            p2).
     * @param radius
     *            the radius of the circular arc.
     */
    public void setArcByTangent(Point2D p1, Point2D p2, Point2D p3, double radius) {
        // Used simple geometric calculations of arc center, radius and angles
        // by tangents
        double a1 = -Math.atan2(p1.getY() - p2.getY(), p1.getX() - p2.getX());
        double a2 = -Math.atan2(p3.getY() - p2.getY(), p3.getX() - p2.getX());
        double am = (a1 + a2) / 2.0;
        double ah = a1 - am;
        double d = radius / Math.abs(Math.sin(ah));
        double x = p2.getX() + d * Math.cos(am);
        double y = p2.getY() - d * Math.sin(am);
        ah = ah >= 0.0 ? Math.PI * 1.5 - ah : Math.PI * 0.5 - ah;
        a1 = getNormAngle(Math.toDegrees(am - ah));
        a2 = getNormAngle(Math.toDegrees(am + ah));
        double delta = a2 - a1;
        if (delta <= 0.0) {
            delta += 360.0;
        }
        setArcByCenter(x, y, radius, a1, delta, type);
    }

    /**
     * Sets a new start angle to be the angle given by the the vector from the
     * current center point to the specified point.
     * 
     * @param point
     *            the point that determines the new start angle.
     */
    public void setAngleStart(Point2D point) {
        double angle = Math.atan2(point.getY() - getCenterY(), point.getX() - getCenterX());
        setAngleStart(getNormAngle(-Math.toDegrees(angle)));
    }

    /**
     * Sets the angles in terms of vectors from the current arc center to the
     * points (x1, y1) and (x2, y2). The start angle is given by the vector from
     * the current center to the point (x1, y1) and the end angle is given by
     * the vector from the center to the point (x2, y2).
     * 
     * @param x1
     *            the x coordinate of the point whose vector from the center
     *            point determines the new start angle of the arc.
     * @param y1
     *            the y coordinate of the point whose vector from the center
     *            point determines the new start angle of the arc.
     * @param x2
     *            the x coordinate of the point whose vector from the center
     *            point determines the new end angle of the arc.
     * @param y2
     *            the y coordinate of the point whose vector from the center
     *            point determines the new end angle of the arc.
     */
    public void setAngles(double x1, double y1, double x2, double y2) {
        double cx = getCenterX();
        double cy = getCenterY();
        double a1 = getNormAngle(-Math.toDegrees(Math.atan2(y1 - cy, x1 - cx)));
        double a2 = getNormAngle(-Math.toDegrees(Math.atan2(y2 - cy, x2 - cx)));
        a2 -= a1;
        if (a2 <= 0.0) {
            a2 += 360.0;
        }
        setAngleStart(a1);
        setAngleExtent(a2);
    }

    /**
     * Sets the angles in terms of vectors from the current arc center to the
     * points p1 and p2. The start angle is given by the vector from the current
     * center to the point p1 and the end angle is given by the vector from the
     * center to the point p2.
     * 
     * @param p1
     *            the point whose vector from the center point determines the
     *            new start angle of the arc.
     * @param p2
     *            the point whose vector from the center point determines the
     *            new end angle of the arc.
     */
    public void setAngles(Point2D p1, Point2D p2) {
        setAngles(p1.getX(), p1.getY(), p2.getX(), p2.getY());
    }

    /**
     * Normalizes the angle by removing extra winding (past 360 degrees) and
     * placing it in the positive degree range.
     * 
     * @param angle
     *            the source angle in degrees.
     * @return an angle between 0 and 360 degrees which corresponds to the same
     *         direction vector as the source angle.
     */
    double getNormAngle(double angle) {
        double n = Math.floor(angle / 360.0);
        return angle - n * 360.0;
    }

    /**
     * Determines whether the given angle is contained in the span of the arc.
     * 
     * @param angle
     *            the angle to test in degrees.
     * @return true, if the given angle is between the start angle and the end
     *         angle of the arc.
     */
    public boolean containsAngle(double angle) {
        double extent = getAngleExtent();
        if (extent >= 360.0) {
            return true;
        }
        angle = getNormAngle(angle);
        double a1 = getNormAngle(getAngleStart());
        double a2 = a1 + extent;
        if (a2 > 360.0) {
            return angle >= a1 || angle <= a2 - 360.0;
        }
        if (a2 < 0.0) {
            return angle >= a2 + 360.0 || angle <= a1;
        }
        return extent > 0.0 ? a1 <= angle && angle <= a2 : a2 <= angle && angle <= a1;
    }

    public boolean contains(double px, double py) {
        // Normalize point
        double nx = (px - getX()) / getWidth() - 0.5;
        double ny = (py - getY()) / getHeight() - 0.5;

        if ((nx * nx + ny * ny) > 0.25) {
            return false;
        }

        double extent = getAngleExtent();
        double absExtent = Math.abs(extent);
        if (absExtent >= 360.0) {
            return true;
        }

        boolean containsAngle = containsAngle(Math.toDegrees(-Math.atan2(ny, nx)));
        if (type == PIE) {
            return containsAngle;
        }
        if (absExtent <= 180.0 && !containsAngle) {
            return false;
        }

        Line2D l = new Line2D.Double(getStartPoint(), getEndPoint());
        int ccw1 = l.relativeCCW(px, py);
        int ccw2 = l.relativeCCW(getCenterX(), getCenterY());
        return ccw1 == 0 || ccw2 == 0 || ((ccw1 + ccw2) == 0 ^ absExtent > 180.0);
    }

    public boolean contains(double rx, double ry, double rw, double rh) {

        if (!(contains(rx, ry) && contains(rx + rw, ry) && contains(rx + rw, ry + rh) && contains(
                rx, ry + rh))) {
            return false;
        }

        double absExtent = Math.abs(getAngleExtent());
        if (type != PIE || absExtent <= 180.0 || absExtent >= 360.0) {
            return true;
        }

        Rectangle2D r = new Rectangle2D.Double(rx, ry, rw, rh);

        double cx = getCenterX();
        double cy = getCenterY();
        if (r.contains(cx, cy)) {
            return false;
        }

        Point2D p1 = getStartPoint();
        Point2D p2 = getEndPoint();

        return !r.intersectsLine(cx, cy, p1.getX(), p1.getY())
                && !r.intersectsLine(cx, cy, p2.getX(), p2.getY());
    }

    @Override
    public boolean contains(Rectangle2D rect) {
        return contains(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
    }

    public boolean intersects(double rx, double ry, double rw, double rh) {

        if (isEmpty() || rw <= 0.0 || rh <= 0.0) {
            return false;
        }

        // Check: Does arc contain rectangle's points
        if (contains(rx, ry) || contains(rx + rw, ry) || contains(rx, ry + rh)
                || contains(rx + rw, ry + rh)) {
            return true;
        }

        double cx = getCenterX();
        double cy = getCenterY();
        Point2D p1 = getStartPoint();
        Point2D p2 = getEndPoint();
        Rectangle2D r = new Rectangle2D.Double(rx, ry, rw, rh);

        // Check: Does rectangle contain arc's points
        if (r.contains(p1) || r.contains(p2) || (type == PIE && r.contains(cx, cy))) {
            return true;
        }

        if (type == PIE) {
            if (r.intersectsLine(p1.getX(), p1.getY(), cx, cy)
                    || r.intersectsLine(p2.getX(), p2.getY(), cx, cy)) {
                return true;
            }
        } else {
            if (r.intersectsLine(p1.getX(), p1.getY(), p2.getX(), p2.getY())) {
                return true;
            }
        }

        // Nearest rectangle point
        double nx = cx < rx ? rx : (cx > rx + rw ? rx + rw : cx);
        double ny = cy < ry ? ry : (cy > ry + rh ? ry + rh : cy);
        return contains(nx, ny);
    }

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

}