1/* 2 * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package java.awt.geom; 27 28import java.util.*; 29 30/** 31 * A utility class to iterate over the path segments of an arc 32 * through the PathIterator interface. 33 * 34 * @author Jim Graham 35 */ 36class ArcIterator implements PathIterator { 37 double x, y, w, h, angStRad, increment, cv; 38 AffineTransform affine; 39 int index; 40 int arcSegs; 41 int lineSegs; 42 43 ArcIterator(Arc2D a, AffineTransform at) { 44 this.w = a.getWidth() / 2; 45 this.h = a.getHeight() / 2; 46 this.x = a.getX() + w; 47 this.y = a.getY() + h; 48 this.angStRad = -Math.toRadians(a.getAngleStart()); 49 this.affine = at; 50 double ext = -a.getAngleExtent(); 51 if (ext >= 360.0 || ext <= -360) { 52 arcSegs = 4; 53 this.increment = Math.PI / 2; 54 // btan(Math.PI / 2); 55 this.cv = 0.5522847498307933; 56 if (ext < 0) { 57 increment = -increment; 58 cv = -cv; 59 } 60 } else { 61 arcSegs = (int) Math.ceil(Math.abs(ext) / 90.0); 62 this.increment = Math.toRadians(ext / arcSegs); 63 this.cv = btan(increment); 64 if (cv == 0) { 65 arcSegs = 0; 66 } 67 } 68 switch (a.getArcType()) { 69 case Arc2D.OPEN: 70 lineSegs = 0; 71 break; 72 case Arc2D.CHORD: 73 lineSegs = 1; 74 break; 75 case Arc2D.PIE: 76 lineSegs = 2; 77 break; 78 } 79 if (w < 0 || h < 0) { 80 arcSegs = lineSegs = -1; 81 } 82 } 83 84 /** 85 * Return the winding rule for determining the insideness of the 86 * path. 87 * @see #WIND_EVEN_ODD 88 * @see #WIND_NON_ZERO 89 */ 90 public int getWindingRule() { 91 return WIND_NON_ZERO; 92 } 93 94 /** 95 * Tests if there are more points to read. 96 * @return true if there are more points to read 97 */ 98 public boolean isDone() { 99 return index > arcSegs + lineSegs; 100 } 101 102 /** 103 * Moves the iterator to the next segment of the path forwards 104 * along the primary direction of traversal as long as there are 105 * more points in that direction. 106 */ 107 public void next() { 108 index++; 109 } 110 111 /* 112 * btan computes the length (k) of the control segments at 113 * the beginning and end of a cubic bezier that approximates 114 * a segment of an arc with extent less than or equal to 115 * 90 degrees. This length (k) will be used to generate the 116 * 2 bezier control points for such a segment. 117 * 118 * Assumptions: 119 * a) arc is centered on 0,0 with radius of 1.0 120 * b) arc extent is less than 90 degrees 121 * c) control points should preserve tangent 122 * d) control segments should have equal length 123 * 124 * Initial data: 125 * start angle: ang1 126 * end angle: ang2 = ang1 + extent 127 * start point: P1 = (x1, y1) = (cos(ang1), sin(ang1)) 128 * end point: P4 = (x4, y4) = (cos(ang2), sin(ang2)) 129 * 130 * Control points: 131 * P2 = (x2, y2) 132 * | x2 = x1 - k * sin(ang1) = cos(ang1) - k * sin(ang1) 133 * | y2 = y1 + k * cos(ang1) = sin(ang1) + k * cos(ang1) 134 * 135 * P3 = (x3, y3) 136 * | x3 = x4 + k * sin(ang2) = cos(ang2) + k * sin(ang2) 137 * | y3 = y4 - k * cos(ang2) = sin(ang2) - k * cos(ang2) 138 * 139 * The formula for this length (k) can be found using the 140 * following derivations: 141 * 142 * Midpoints: 143 * a) bezier (t = 1/2) 144 * bPm = P1 * (1-t)^3 + 145 * 3 * P2 * t * (1-t)^2 + 146 * 3 * P3 * t^2 * (1-t) + 147 * P4 * t^3 = 148 * = (P1 + 3P2 + 3P3 + P4)/8 149 * 150 * b) arc 151 * aPm = (cos((ang1 + ang2)/2), sin((ang1 + ang2)/2)) 152 * 153 * Let angb = (ang2 - ang1)/2; angb is half of the angle 154 * between ang1 and ang2. 155 * 156 * Solve the equation bPm == aPm 157 * 158 * a) For xm coord: 159 * x1 + 3*x2 + 3*x3 + x4 = 8*cos((ang1 + ang2)/2) 160 * 161 * cos(ang1) + 3*cos(ang1) - 3*k*sin(ang1) + 162 * 3*cos(ang2) + 3*k*sin(ang2) + cos(ang2) = 163 * = 8*cos((ang1 + ang2)/2) 164 * 165 * 4*cos(ang1) + 4*cos(ang2) + 3*k*(sin(ang2) - sin(ang1)) = 166 * = 8*cos((ang1 + ang2)/2) 167 * 168 * 8*cos((ang1 + ang2)/2)*cos((ang2 - ang1)/2) + 169 * 6*k*sin((ang2 - ang1)/2)*cos((ang1 + ang2)/2) = 170 * = 8*cos((ang1 + ang2)/2) 171 * 172 * 4*cos(angb) + 3*k*sin(angb) = 4 173 * 174 * k = 4 / 3 * (1 - cos(angb)) / sin(angb) 175 * 176 * b) For ym coord we derive the same formula. 177 * 178 * Since this formula can generate "NaN" values for small 179 * angles, we will derive a safer form that does not involve 180 * dividing by very small values: 181 * (1 - cos(angb)) / sin(angb) = 182 * = (1 - cos(angb))*(1 + cos(angb)) / sin(angb)*(1 + cos(angb)) = 183 * = (1 - cos(angb)^2) / sin(angb)*(1 + cos(angb)) = 184 * = sin(angb)^2 / sin(angb)*(1 + cos(angb)) = 185 * = sin(angb) / (1 + cos(angb)) 186 * 187 */ 188 private static double btan(double increment) { 189 increment /= 2.0; 190 return 4.0 / 3.0 * Math.sin(increment) / (1.0 + Math.cos(increment)); 191 } 192 193 /** 194 * Returns the coordinates and type of the current path segment in 195 * the iteration. 196 * The return value is the path segment type: 197 * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE. 198 * A float array of length 6 must be passed in and may be used to 199 * store the coordinates of the point(s). 200 * Each point is stored as a pair of float x,y coordinates. 201 * SEG_MOVETO and SEG_LINETO types will return one point, 202 * SEG_QUADTO will return two points, 203 * SEG_CUBICTO will return 3 points 204 * and SEG_CLOSE will not return any points. 205 * @see #SEG_MOVETO 206 * @see #SEG_LINETO 207 * @see #SEG_QUADTO 208 * @see #SEG_CUBICTO 209 * @see #SEG_CLOSE 210 */ 211 public int currentSegment(float[] coords) { 212 if (isDone()) { 213 throw new NoSuchElementException("arc iterator out of bounds"); 214 } 215 double angle = angStRad; 216 if (index == 0) { 217 coords[0] = (float) (x + Math.cos(angle) * w); 218 coords[1] = (float) (y + Math.sin(angle) * h); 219 if (affine != null) { 220 affine.transform(coords, 0, coords, 0, 1); 221 } 222 return SEG_MOVETO; 223 } 224 if (index > arcSegs) { 225 if (index == arcSegs + lineSegs) { 226 return SEG_CLOSE; 227 } 228 coords[0] = (float) x; 229 coords[1] = (float) y; 230 if (affine != null) { 231 affine.transform(coords, 0, coords, 0, 1); 232 } 233 return SEG_LINETO; 234 } 235 angle += increment * (index - 1); 236 double relx = Math.cos(angle); 237 double rely = Math.sin(angle); 238 coords[0] = (float) (x + (relx - cv * rely) * w); 239 coords[1] = (float) (y + (rely + cv * relx) * h); 240 angle += increment; 241 relx = Math.cos(angle); 242 rely = Math.sin(angle); 243 coords[2] = (float) (x + (relx + cv * rely) * w); 244 coords[3] = (float) (y + (rely - cv * relx) * h); 245 coords[4] = (float) (x + relx * w); 246 coords[5] = (float) (y + rely * h); 247 if (affine != null) { 248 affine.transform(coords, 0, coords, 0, 3); 249 } 250 return SEG_CUBICTO; 251 } 252 253 /** 254 * Returns the coordinates and type of the current path segment in 255 * the iteration. 256 * The return value is the path segment type: 257 * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE. 258 * A double array of length 6 must be passed in and may be used to 259 * store the coordinates of the point(s). 260 * Each point is stored as a pair of double x,y coordinates. 261 * SEG_MOVETO and SEG_LINETO types will return one point, 262 * SEG_QUADTO will return two points, 263 * SEG_CUBICTO will return 3 points 264 * and SEG_CLOSE will not return any points. 265 * @see #SEG_MOVETO 266 * @see #SEG_LINETO 267 * @see #SEG_QUADTO 268 * @see #SEG_CUBICTO 269 * @see #SEG_CLOSE 270 */ 271 public int currentSegment(double[] coords) { 272 if (isDone()) { 273 throw new NoSuchElementException("arc iterator out of bounds"); 274 } 275 double angle = angStRad; 276 if (index == 0) { 277 coords[0] = x + Math.cos(angle) * w; 278 coords[1] = y + Math.sin(angle) * h; 279 if (affine != null) { 280 affine.transform(coords, 0, coords, 0, 1); 281 } 282 return SEG_MOVETO; 283 } 284 if (index > arcSegs) { 285 if (index == arcSegs + lineSegs) { 286 return SEG_CLOSE; 287 } 288 coords[0] = x; 289 coords[1] = y; 290 if (affine != null) { 291 affine.transform(coords, 0, coords, 0, 1); 292 } 293 return SEG_LINETO; 294 } 295 angle += increment * (index - 1); 296 double relx = Math.cos(angle); 297 double rely = Math.sin(angle); 298 coords[0] = x + (relx - cv * rely) * w; 299 coords[1] = y + (rely + cv * relx) * h; 300 angle += increment; 301 relx = Math.cos(angle); 302 rely = Math.sin(angle); 303 coords[2] = x + (relx + cv * rely) * w; 304 coords[3] = y + (rely - cv * relx) * h; 305 coords[4] = x + relx * w; 306 coords[5] = y + rely * h; 307 if (affine != null) { 308 affine.transform(coords, 0, coords, 0, 3); 309 } 310 return SEG_CUBICTO; 311 } 312} 313