1. /*
  2. * @(#)ArcIterator.java 1.10 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.geom;
  8. import java.util.*;
  9. /**
  10. * A utility class to iterate over the path segments of an arc
  11. * through the PathIterator interface.
  12. *
  13. * @version 10 Feb 1997
  14. * @author Jim Graham
  15. */
  16. class ArcIterator implements PathIterator {
  17. double x, y, w, h, angStRad, angExtDeg;
  18. AffineTransform affine;
  19. int index;
  20. int arcSegs;
  21. int lineSegs;
  22. ArcIterator(Arc2D a, AffineTransform at) {
  23. this.w = a.getWidth() / 2;
  24. this.h = a.getHeight() / 2;
  25. this.x = a.getX() + w;
  26. this.y = a.getY() + h;
  27. this.angStRad = -Math.toRadians(a.getAngleStart());
  28. this.angExtDeg = -a.getAngleExtent();
  29. this.affine = at;
  30. double ext = Math.abs(angExtDeg);
  31. if (ext >= 360.0) {
  32. arcSegs = 4;
  33. } else {
  34. arcSegs = (int) Math.ceil(ext / 90.0);
  35. }
  36. switch (a.getArcType()) {
  37. case Arc2D.OPEN:
  38. lineSegs = 0;
  39. break;
  40. case Arc2D.CHORD:
  41. lineSegs = 1;
  42. break;
  43. case Arc2D.PIE:
  44. lineSegs = 2;
  45. break;
  46. }
  47. if (w < 0 || h < 0) {
  48. arcSegs = lineSegs = -1;
  49. }
  50. }
  51. /**
  52. * Return the winding rule for determining the insideness of the
  53. * path.
  54. * @see #WIND_EVEN_ODD
  55. * @see #WIND_NON_ZERO
  56. */
  57. public int getWindingRule() {
  58. return WIND_NON_ZERO;
  59. }
  60. /**
  61. * Tests if there are more points to read.
  62. * @return true if there are more points to read
  63. */
  64. public boolean isDone() {
  65. return index > arcSegs + lineSegs;
  66. }
  67. /**
  68. * Moves the iterator to the next segment of the path forwards
  69. * along the primary direction of traversal as long as there are
  70. * more points in that direction.
  71. */
  72. public void next() {
  73. index++;
  74. }
  75. private static double btan(double increment) {
  76. increment /= 2.0;
  77. double a = 1.0 - Math.cos(increment);
  78. double b = Math.tan(increment);
  79. double c = Math.sqrt(1.0 + b * b) - 1.0 + a;
  80. return 4.0 / 3.0 * a * b / c;
  81. }
  82. /**
  83. * Returns the coordinates and type of the current path segment in
  84. * the iteration.
  85. * The return value is the path segment type:
  86. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  87. * A float array of length 6 must be passed in and may be used to
  88. * store the coordinates of the point(s).
  89. * Each point is stored as a pair of float x,y coordinates.
  90. * SEG_MOVETO and SEG_LINETO types will return one point,
  91. * SEG_QUADTO will return two points,
  92. * SEG_CUBICTO will return 3 points
  93. * and SEG_CLOSE will not return any points.
  94. * @see #SEG_MOVETO
  95. * @see #SEG_LINETO
  96. * @see #SEG_QUADTO
  97. * @see #SEG_CUBICTO
  98. * @see #SEG_CLOSE
  99. */
  100. public int currentSegment(float[] coords) {
  101. if (isDone()) {
  102. throw new NoSuchElementException("arc iterator out of bounds");
  103. }
  104. double angle = angStRad;
  105. if (index == 0) {
  106. coords[0] = (float) (x + Math.cos(angle) * w);
  107. coords[1] = (float) (y + Math.sin(angle) * h);
  108. if (affine != null) {
  109. affine.transform(coords, 0, coords, 0, 1);
  110. }
  111. return SEG_MOVETO;
  112. }
  113. if (index > arcSegs) {
  114. if (index == arcSegs + lineSegs) {
  115. return SEG_CLOSE;
  116. }
  117. coords[0] = (float) x;
  118. coords[1] = (float) y;
  119. if (affine != null) {
  120. affine.transform(coords, 0, coords, 0, 1);
  121. }
  122. return SEG_LINETO;
  123. }
  124. double increment = angExtDeg;
  125. if (increment > 360.0) {
  126. increment = 360.0;
  127. } else if (increment < -360.0) {
  128. increment = -360.0;
  129. }
  130. increment /= arcSegs;
  131. increment = Math.toRadians(increment);
  132. angle += increment * (index - 1);
  133. double relx = Math.cos(angle);
  134. double rely = Math.sin(angle);
  135. double z = btan(increment);
  136. coords[0] = (float) (x + (relx - z * rely) * w);
  137. coords[1] = (float) (y + (rely + z * relx) * h);
  138. angle += increment;
  139. relx = Math.cos(angle);
  140. rely = Math.sin(angle);
  141. coords[2] = (float) (x + (relx + z * rely) * w);
  142. coords[3] = (float) (y + (rely - z * relx) * h);
  143. coords[4] = (float) (x + relx * w);
  144. coords[5] = (float) (y + rely * h);
  145. if (affine != null) {
  146. affine.transform(coords, 0, coords, 0, 3);
  147. }
  148. return SEG_CUBICTO;
  149. }
  150. /**
  151. * Returns the coordinates and type of the current path segment in
  152. * the iteration.
  153. * The return value is the path segment type:
  154. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  155. * A double array of length 6 must be passed in and may be used to
  156. * store the coordinates of the point(s).
  157. * Each point is stored as a pair of double x,y coordinates.
  158. * SEG_MOVETO and SEG_LINETO types will return one point,
  159. * SEG_QUADTO will return two points,
  160. * SEG_CUBICTO will return 3 points
  161. * and SEG_CLOSE will not return any points.
  162. * @see #SEG_MOVETO
  163. * @see #SEG_LINETO
  164. * @see #SEG_QUADTO
  165. * @see #SEG_CUBICTO
  166. * @see #SEG_CLOSE
  167. */
  168. public int currentSegment(double[] coords) {
  169. if (isDone()) {
  170. throw new NoSuchElementException("arc iterator out of bounds");
  171. }
  172. double angle = angStRad;
  173. if (index == 0) {
  174. coords[0] = x + Math.cos(angle) * w;
  175. coords[1] = y + Math.sin(angle) * h;
  176. if (affine != null) {
  177. affine.transform(coords, 0, coords, 0, 1);
  178. }
  179. return SEG_MOVETO;
  180. }
  181. if (index > arcSegs) {
  182. if (index == arcSegs + lineSegs) {
  183. return SEG_CLOSE;
  184. }
  185. coords[0] = x;
  186. coords[1] = y;
  187. if (affine != null) {
  188. affine.transform(coords, 0, coords, 0, 1);
  189. }
  190. return SEG_LINETO;
  191. }
  192. double increment = angExtDeg;
  193. if (increment > 360.0) {
  194. increment = 360.0;
  195. } else if (increment < -360.0) {
  196. increment = -360.0;
  197. }
  198. increment /= arcSegs;
  199. increment = Math.toRadians(increment);
  200. angle += increment * (index - 1);
  201. double relx = Math.cos(angle);
  202. double rely = Math.sin(angle);
  203. double z = btan(increment);
  204. coords[0] = x + (relx - z * rely) * w;
  205. coords[1] = y + (rely + z * relx) * h;
  206. angle += increment;
  207. relx = Math.cos(angle);
  208. rely = Math.sin(angle);
  209. coords[2] = x + (relx + z * rely) * w;
  210. coords[3] = y + (rely - z * relx) * h;
  211. coords[4] = x + relx * w;
  212. coords[5] = y + rely * h;
  213. if (affine != null) {
  214. affine.transform(coords, 0, coords, 0, 3);
  215. }
  216. return SEG_CUBICTO;
  217. }
  218. }