KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > geom > EllipseIterator


1 /*
2  * @(#)EllipseIterator.java 1.12 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.geom;
9
10 import java.util.*;
11
12 /**
13  * A utility class to iterate over the path segments of an ellipse
14  * through the PathIterator interface.
15  *
16  * @version 10 Feb 1997
17  * @author Jim Graham
18  */

19 class EllipseIterator implements PathIterator JavaDoc {
20     double x, y, w, h;
21     AffineTransform JavaDoc affine;
22     int index;
23
24     EllipseIterator(Ellipse2D JavaDoc e, AffineTransform JavaDoc at) {
25     this.x = e.getX();
26     this.y = e.getY();
27     this.w = e.getWidth();
28     this.h = e.getHeight();
29     this.affine = at;
30     if (w < 0 || h < 0) {
31         index = 6;
32     }
33     }
34
35     /**
36      * Return the winding rule for determining the insideness of the
37      * path.
38      * @see #WIND_EVEN_ODD
39      * @see #WIND_NON_ZERO
40      */

41     public int getWindingRule() {
42     return WIND_NON_ZERO;
43     }
44
45     /**
46      * Tests if there are more points to read.
47      * @return true if there are more points to read
48      */

49     public boolean isDone() {
50     return index > 5;
51     }
52
53     /**
54      * Moves the iterator to the next segment of the path forwards
55      * along the primary direction of traversal as long as there are
56      * more points in that direction.
57      */

58     public void next() {
59     index++;
60     }
61
62     // ArcIterator.btan(Math.PI/2)
63
public static final double CtrlVal = 0.5522847498307933;
64
65     /*
66      * ctrlpts contains the control points for a set of 4 cubic
67      * bezier curves that approximate a circle of radius 0.5
68      * centered at 0.5, 0.5
69      */

70     private static final double pcv = 0.5 + CtrlVal * 0.5;
71     private static final double ncv = 0.5 - CtrlVal * 0.5;
72     private static double ctrlpts[][] = {
73     { 1.0, pcv, pcv, 1.0, 0.5, 1.0 },
74     { ncv, 1.0, 0.0, pcv, 0.0, 0.5 },
75     { 0.0, ncv, ncv, 0.0, 0.5, 0.0 },
76     { pcv, 0.0, 1.0, ncv, 1.0, 0.5 }
77     };
78
79     /**
80      * Returns the coordinates and type of the current path segment in
81      * the iteration.
82      * The return value is the path segment type:
83      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
84      * A float array of length 6 must be passed in and may be used to
85      * store the coordinates of the point(s).
86      * Each point is stored as a pair of float x,y coordinates.
87      * SEG_MOVETO and SEG_LINETO types will return one point,
88      * SEG_QUADTO will return two points,
89      * SEG_CUBICTO will return 3 points
90      * and SEG_CLOSE will not return any points.
91      * @see #SEG_MOVETO
92      * @see #SEG_LINETO
93      * @see #SEG_QUADTO
94      * @see #SEG_CUBICTO
95      * @see #SEG_CLOSE
96      */

97     public int currentSegment(float[] coords) {
98     if (isDone()) {
99         throw new NoSuchElementException("ellipse iterator out of bounds");
100     }
101     if (index == 5) {
102         return SEG_CLOSE;
103     }
104     if (index == 0) {
105         double ctrls[] = ctrlpts[3];
106         coords[0] = (float) (x + ctrls[4] * w);
107         coords[1] = (float) (y + ctrls[5] * h);
108         if (affine != null) {
109         affine.transform(coords, 0, coords, 0, 1);
110         }
111         return SEG_MOVETO;
112     }
113     double ctrls[] = ctrlpts[index - 1];
114     coords[0] = (float) (x + ctrls[0] * w);
115     coords[1] = (float) (y + ctrls[1] * h);
116     coords[2] = (float) (x + ctrls[2] * w);
117     coords[3] = (float) (y + ctrls[3] * h);
118     coords[4] = (float) (x + ctrls[4] * w);
119     coords[5] = (float) (y + ctrls[5] * h);
120     if (affine != null) {
121         affine.transform(coords, 0, coords, 0, 3);
122     }
123     return SEG_CUBICTO;
124     }
125
126     /**
127      * Returns the coordinates and type of the current path segment in
128      * the iteration.
129      * The return value is the path segment type:
130      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
131      * A double array of length 6 must be passed in and may be used to
132      * store the coordinates of the point(s).
133      * Each point is stored as a pair of double x,y coordinates.
134      * SEG_MOVETO and SEG_LINETO types will return one point,
135      * SEG_QUADTO will return two points,
136      * SEG_CUBICTO will return 3 points
137      * and SEG_CLOSE will not return any points.
138      * @see #SEG_MOVETO
139      * @see #SEG_LINETO
140      * @see #SEG_QUADTO
141      * @see #SEG_CUBICTO
142      * @see #SEG_CLOSE
143      */

144     public int currentSegment(double[] coords) {
145     if (isDone()) {
146         throw new NoSuchElementException("ellipse iterator out of bounds");
147     }
148     if (index == 5) {
149         return SEG_CLOSE;
150     }
151     if (index == 0) {
152         double ctrls[] = ctrlpts[3];
153         coords[0] = x + ctrls[4] * w;
154         coords[1] = y + ctrls[5] * h;
155         if (affine != null) {
156         affine.transform(coords, 0, coords, 0, 1);
157         }
158         return SEG_MOVETO;
159     }
160     double ctrls[] = ctrlpts[index - 1];
161     coords[0] = x + ctrls[0] * w;
162     coords[1] = y + ctrls[1] * h;
163     coords[2] = x + ctrls[2] * w;
164     coords[3] = y + ctrls[3] * h;
165     coords[4] = x + ctrls[4] * w;
166     coords[5] = y + ctrls[5] * h;
167     if (affine != null) {
168         affine.transform(coords, 0, coords, 0, 3);
169     }
170     return SEG_CUBICTO;
171     }
172 }
173
Popular Tags