KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ArcIterator.java 1.16 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 arc
14  * through the PathIterator interface.
15  *
16  * @version 10 Feb 1997
17  * @author Jim Graham
18  */

19 class ArcIterator implements PathIterator JavaDoc {
20     double x, y, w, h, angStRad, increment, cv;
21     AffineTransform JavaDoc affine;
22     int index;
23     int arcSegs;
24     int lineSegs;
25
26     ArcIterator(Arc2D JavaDoc a, AffineTransform JavaDoc at) {
27     this.w = a.getWidth() / 2;
28     this.h = a.getHeight() / 2;
29     this.x = a.getX() + w;
30     this.y = a.getY() + h;
31     this.angStRad = -Math.toRadians(a.getAngleStart());
32     this.affine = at;
33     double ext = -a.getAngleExtent();
34     if (ext >= 360.0 || ext <= -360) {
35         arcSegs = 4;
36         this.increment = Math.PI / 2;
37         // btan(Math.PI / 2);
38
this.cv = 0.5522847498307933;
39         if (ext < 0) {
40         increment = -increment;
41         cv = -cv;
42         }
43     } else {
44         arcSegs = (int) Math.ceil(Math.abs(ext) / 90.0);
45         this.increment = Math.toRadians(ext / arcSegs);
46         this.cv = btan(increment);
47         if (cv == 0) {
48         arcSegs = 0;
49         }
50     }
51     switch (a.getArcType()) {
52     case Arc2D.OPEN:
53         lineSegs = 0;
54         break;
55     case Arc2D.CHORD:
56         lineSegs = 1;
57         break;
58     case Arc2D.PIE:
59         lineSegs = 2;
60         break;
61     }
62     if (w < 0 || h < 0) {
63         arcSegs = lineSegs = -1;
64     }
65     }
66
67     /**
68      * Return the winding rule for determining the insideness of the
69      * path.
70      * @see #WIND_EVEN_ODD
71      * @see #WIND_NON_ZERO
72      */

73     public int getWindingRule() {
74     return WIND_NON_ZERO;
75     }
76
77     /**
78      * Tests if there are more points to read.
79      * @return true if there are more points to read
80      */

81     public boolean isDone() {
82     return index > arcSegs + lineSegs;
83     }
84
85     /**
86      * Moves the iterator to the next segment of the path forwards
87      * along the primary direction of traversal as long as there are
88      * more points in that direction.
89      */

90     public void next() {
91     index++;
92     }
93
94     /*
95      * btan computes the length (k) of the control segments at
96      * the beginning and end of a cubic bezier that approximates
97      * a segment of an arc with extent less than or equal to
98      * 90 degrees. This length (k) will be used to generate the
99      * 2 bezier control points for such a segment.
100      *
101      * Assumptions:
102      * a) arc is centered on 0,0 with radius of 1.0
103      * b) arc extent is less than 90 degrees
104      * c) control points should preserve tangent
105      * d) control segments should have equal length
106      *
107      * Initial data:
108      * start angle: ang1
109      * end angle: ang2 = ang1 + extent
110      * start point: P1 = (x1, y1) = (cos(ang1), sin(ang1))
111      * end point: P4 = (x4, y4) = (cos(ang2), sin(ang2))
112      *
113      * Control points:
114      * P2 = (x2, y2)
115      * | x2 = x1 - k * sin(ang1) = cos(ang1) - k * sin(ang1)
116      * | y2 = y1 + k * cos(ang1) = sin(ang1) + k * cos(ang1)
117      *
118      * P3 = (x3, y3)
119      * | x3 = x4 + k * sin(ang2) = cos(ang2) + k * sin(ang2)
120      * | y3 = y4 - k * cos(ang2) = sin(ang2) - k * cos(ang2)
121      *
122      * The formula for this length (k) can be found using the
123      * following derivations:
124      *
125      * Midpoints:
126      * a) bezier (t = 1/2)
127      * bPm = P1 * (1-t)^3 +
128      * 3 * P2 * t * (1-t)^2 +
129      * 3 * P3 * t^2 * (1-t) +
130      * P4 * t^3 =
131      * = (P1 + 3P2 + 3P3 + P4)/8
132      *
133      * b) arc
134      * aPm = (cos((ang1 + ang2)/2), sin((ang1 + ang2)/2))
135      *
136      * Let angb = (ang2 - ang1)/2; angb is half of the angle
137      * between ang1 and ang2.
138      *
139      * Solve the equation bPm == aPm
140      *
141      * a) For xm coord:
142      * x1 + 3*x2 + 3*x3 + x4 = 8*cos((ang1 + ang2)/2)
143      *
144      * cos(ang1) + 3*cos(ang1) - 3*k*sin(ang1) +
145      * 3*cos(ang2) + 3*k*sin(ang2) + cos(ang2) =
146      * = 8*cos((ang1 + ang2)/2)
147      *
148      * 4*cos(ang1) + 4*cos(ang2) + 3*k*(sin(ang2) - sin(ang1)) =
149      * = 8*cos((ang1 + ang2)/2)
150      *
151      * 8*cos((ang1 + ang2)/2)*cos((ang2 - ang1)/2) +
152      * 6*k*sin((ang2 - ang1)/2)*cos((ang1 + ang2)/2) =
153      * = 8*cos((ang1 + ang2)/2)
154      *
155      * 4*cos(angb) + 3*k*sin(angb) = 4
156      *
157      * k = 4 / 3 * (1 - cos(angb)) / sin(angb)
158      *
159      * b) For ym coord we derive the same formula.
160      *
161      * Since this formula can generate "NaN" values for small
162      * angles, we will derive a safer form that does not involve
163      * dividing by very small values:
164      * (1 - cos(angb)) / sin(angb) =
165      * = (1 - cos(angb))*(1 + cos(angb)) / sin(angb)*(1 + cos(angb)) =
166      * = (1 - cos(angb)^2) / sin(angb)*(1 + cos(angb)) =
167      * = sin(angb)^2 / sin(angb)*(1 + cos(angb)) =
168      * = sin(angb) / (1 + cos(angb))
169      *
170      */

171     private static double btan(double increment) {
172     increment /= 2.0;
173     return 4.0 / 3.0 * Math.sin(increment) / (1.0 + Math.cos(increment));
174     }
175
176     /**
177      * Returns the coordinates and type of the current path segment in
178      * the iteration.
179      * The return value is the path segment type:
180      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
181      * A float array of length 6 must be passed in and may be used to
182      * store the coordinates of the point(s).
183      * Each point is stored as a pair of float x,y coordinates.
184      * SEG_MOVETO and SEG_LINETO types will return one point,
185      * SEG_QUADTO will return two points,
186      * SEG_CUBICTO will return 3 points
187      * and SEG_CLOSE will not return any points.
188      * @see #SEG_MOVETO
189      * @see #SEG_LINETO
190      * @see #SEG_QUADTO
191      * @see #SEG_CUBICTO
192      * @see #SEG_CLOSE
193      */

194     public int currentSegment(float[] coords) {
195     if (isDone()) {
196         throw new NoSuchElementException("arc iterator out of bounds");
197     }
198     double angle = angStRad;
199     if (index == 0) {
200         coords[0] = (float) (x + Math.cos(angle) * w);
201         coords[1] = (float) (y + Math.sin(angle) * h);
202         if (affine != null) {
203         affine.transform(coords, 0, coords, 0, 1);
204         }
205         return SEG_MOVETO;
206     }
207     if (index > arcSegs) {
208         if (index == arcSegs + lineSegs) {
209         return SEG_CLOSE;
210         }
211         coords[0] = (float) x;
212         coords[1] = (float) y;
213         if (affine != null) {
214         affine.transform(coords, 0, coords, 0, 1);
215         }
216         return SEG_LINETO;
217     }
218     angle += increment * (index - 1);
219     double relx = Math.cos(angle);
220     double rely = Math.sin(angle);
221     coords[0] = (float) (x + (relx - cv * rely) * w);
222     coords[1] = (float) (y + (rely + cv * relx) * h);
223     angle += increment;
224     relx = Math.cos(angle);
225     rely = Math.sin(angle);
226     coords[2] = (float) (x + (relx + cv * rely) * w);
227     coords[3] = (float) (y + (rely - cv * relx) * h);
228     coords[4] = (float) (x + relx * w);
229     coords[5] = (float) (y + rely * h);
230     if (affine != null) {
231         affine.transform(coords, 0, coords, 0, 3);
232     }
233     return SEG_CUBICTO;
234     }
235
236     /**
237      * Returns the coordinates and type of the current path segment in
238      * the iteration.
239      * The return value is the path segment type:
240      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
241      * A double array of length 6 must be passed in and may be used to
242      * store the coordinates of the point(s).
243      * Each point is stored as a pair of double x,y coordinates.
244      * SEG_MOVETO and SEG_LINETO types will return one point,
245      * SEG_QUADTO will return two points,
246      * SEG_CUBICTO will return 3 points
247      * and SEG_CLOSE will not return any points.
248      * @see #SEG_MOVETO
249      * @see #SEG_LINETO
250      * @see #SEG_QUADTO
251      * @see #SEG_CUBICTO
252      * @see #SEG_CLOSE
253      */

254     public int currentSegment(double[] coords) {
255     if (isDone()) {
256         throw new NoSuchElementException("arc iterator out of bounds");
257     }
258     double angle = angStRad;
259     if (index == 0) {
260         coords[0] = x + Math.cos(angle) * w;
261         coords[1] = y + Math.sin(angle) * h;
262         if (affine != null) {
263         affine.transform(coords, 0, coords, 0, 1);
264         }
265         return SEG_MOVETO;
266     }
267     if (index > arcSegs) {
268         if (index == arcSegs + lineSegs) {
269         return SEG_CLOSE;
270         }
271         coords[0] = x;
272         coords[1] = y;
273         if (affine != null) {
274         affine.transform(coords, 0, coords, 0, 1);
275         }
276         return SEG_LINETO;
277     }
278     angle += increment * (index - 1);
279     double relx = Math.cos(angle);
280     double rely = Math.sin(angle);
281     coords[0] = x + (relx - cv * rely) * w;
282     coords[1] = y + (rely + cv * relx) * h;
283     angle += increment;
284     relx = Math.cos(angle);
285     rely = Math.sin(angle);
286     coords[2] = x + (relx + cv * rely) * w;
287     coords[3] = y + (rely - cv * relx) * h;
288     coords[4] = x + relx * w;
289     coords[5] = y + rely * h;
290     if (affine != null) {
291         affine.transform(coords, 0, coords, 0, 3);
292     }
293     return SEG_CUBICTO;
294     }
295 }
296
Popular Tags