1. /*
  2. * @(#)ArcIterator.java 1.14 03/04/10
  3. *
  4. * Copyright 2003 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. return 4.0 / 3.0 * Math.sin(increment) / (1.0 + Math.cos(increment));
  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("arc iterator out of bounds");
  100. }
  101. double angle = angStRad;
  102. if (index == 0) {
  103. coords[0] = (float) (x + Math.cos(angle) * w);
  104. coords[1] = (float) (y + Math.sin(angle) * h);
  105. if (affine != null) {
  106. affine.transform(coords, 0, coords, 0, 1);
  107. }
  108. return SEG_MOVETO;
  109. }
  110. if (index > arcSegs) {
  111. if (index == arcSegs + lineSegs) {
  112. return SEG_CLOSE;
  113. }
  114. coords[0] = (float) x;
  115. coords[1] = (float) y;
  116. if (affine != null) {
  117. affine.transform(coords, 0, coords, 0, 1);
  118. }
  119. return SEG_LINETO;
  120. }
  121. double increment = angExtDeg;
  122. if (increment > 360.0) {
  123. increment = 360.0;
  124. } else if (increment < -360.0) {
  125. increment = -360.0;
  126. }
  127. increment /= arcSegs;
  128. increment = Math.toRadians(increment);
  129. angle += increment * (index - 1);
  130. double relx = Math.cos(angle);
  131. double rely = Math.sin(angle);
  132. double z = btan(increment);
  133. coords[0] = (float) (x + (relx - z * rely) * w);
  134. coords[1] = (float) (y + (rely + z * relx) * h);
  135. angle += increment;
  136. relx = Math.cos(angle);
  137. rely = Math.sin(angle);
  138. coords[2] = (float) (x + (relx + z * rely) * w);
  139. coords[3] = (float) (y + (rely - z * relx) * h);
  140. coords[4] = (float) (x + relx * w);
  141. coords[5] = (float) (y + rely * h);
  142. if (affine != null) {
  143. affine.transform(coords, 0, coords, 0, 3);
  144. }
  145. return SEG_CUBICTO;
  146. }
  147. /**
  148. * Returns the coordinates and type of the current path segment in
  149. * the iteration.
  150. * The return value is the path segment type:
  151. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  152. * A double array of length 6 must be passed in and may be used to
  153. * store the coordinates of the point(s).
  154. * Each point is stored as a pair of double x,y coordinates.
  155. * SEG_MOVETO and SEG_LINETO types will return one point,
  156. * SEG_QUADTO will return two points,
  157. * SEG_CUBICTO will return 3 points
  158. * and SEG_CLOSE will not return any points.
  159. * @see #SEG_MOVETO
  160. * @see #SEG_LINETO
  161. * @see #SEG_QUADTO
  162. * @see #SEG_CUBICTO
  163. * @see #SEG_CLOSE
  164. */
  165. public int currentSegment(double[] coords) {
  166. if (isDone()) {
  167. throw new NoSuchElementException("arc iterator out of bounds");
  168. }
  169. double angle = angStRad;
  170. if (index == 0) {
  171. coords[0] = x + Math.cos(angle) * w;
  172. coords[1] = y + Math.sin(angle) * h;
  173. if (affine != null) {
  174. affine.transform(coords, 0, coords, 0, 1);
  175. }
  176. return SEG_MOVETO;
  177. }
  178. if (index > arcSegs) {
  179. if (index == arcSegs + lineSegs) {
  180. return SEG_CLOSE;
  181. }
  182. coords[0] = x;
  183. coords[1] = y;
  184. if (affine != null) {
  185. affine.transform(coords, 0, coords, 0, 1);
  186. }
  187. return SEG_LINETO;
  188. }
  189. double increment = angExtDeg;
  190. if (increment > 360.0) {
  191. increment = 360.0;
  192. } else if (increment < -360.0) {
  193. increment = -360.0;
  194. }
  195. increment /= arcSegs;
  196. increment = Math.toRadians(increment);
  197. angle += increment * (index - 1);
  198. double relx = Math.cos(angle);
  199. double rely = Math.sin(angle);
  200. double z = btan(increment);
  201. coords[0] = x + (relx - z * rely) * w;
  202. coords[1] = y + (rely + z * relx) * h;
  203. angle += increment;
  204. relx = Math.cos(angle);
  205. rely = Math.sin(angle);
  206. coords[2] = x + (relx + z * rely) * w;
  207. coords[3] = y + (rely - z * relx) * h;
  208. coords[4] = x + relx * w;
  209. coords[5] = y + rely * h;
  210. if (affine != null) {
  211. affine.transform(coords, 0, coords, 0, 3);
  212. }
  213. return SEG_CUBICTO;
  214. }
  215. }