1. /*
  2. * @(#)EllipseIterator.java 1.10 03/01/23
  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 ellipse
  11. * through the PathIterator interface.
  12. *
  13. * @version 10 Feb 1997
  14. * @author Jim Graham
  15. */
  16. class EllipseIterator implements PathIterator {
  17. double x, y, w, h;
  18. AffineTransform affine;
  19. int index;
  20. EllipseIterator(Ellipse2D e, AffineTransform at) {
  21. this.x = e.getX();
  22. this.y = e.getY();
  23. this.w = e.getWidth();
  24. this.h = e.getHeight();
  25. this.affine = at;
  26. if (w < 0 || h < 0) {
  27. index = 6;
  28. }
  29. }
  30. /**
  31. * Return the winding rule for determining the insideness of the
  32. * path.
  33. * @see #WIND_EVEN_ODD
  34. * @see #WIND_NON_ZERO
  35. */
  36. public int getWindingRule() {
  37. return WIND_NON_ZERO;
  38. }
  39. /**
  40. * Tests if there are more points to read.
  41. * @return true if there are more points to read
  42. */
  43. public boolean isDone() {
  44. return index > 5;
  45. }
  46. /**
  47. * Moves the iterator to the next segment of the path forwards
  48. * along the primary direction of traversal as long as there are
  49. * more points in that direction.
  50. */
  51. public void next() {
  52. index++;
  53. }
  54. private static final double angle = Math.PI / 4.0;
  55. private static final double a = 1.0 - Math.cos(angle);
  56. private static final double b = Math.tan(angle);
  57. private static final double c = Math.sqrt(1.0 + b * b) - 1 + a;
  58. private static final double cv = 4.0 / 3.0 * a * b / c;
  59. private static double ctrlpts[][] = {
  60. { 1.0, cv, cv, 1.0, 0.0, 1.0 },
  61. { -cv, 1.0, -1.0, cv, -1.0, 0.0 },
  62. { -1.0, -cv, -cv, -1.0, 0.0, -1.0 },
  63. { cv, -1.0, 1.0, -cv, 1.0, 0.0 }
  64. };
  65. static {
  66. for (int i = 0; i < 4; i++) {
  67. for (int j = 0; j < 6; j++) {
  68. ctrlpts[i][j] = (ctrlpts[i][j] * 0.5) + 0.5;
  69. }
  70. }
  71. }
  72. /**
  73. * Returns the coordinates and type of the current path segment in
  74. * the iteration.
  75. * The return value is the path segment type:
  76. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  77. * A float array of length 6 must be passed in and may be used to
  78. * store the coordinates of the point(s).
  79. * Each point is stored as a pair of float x,y coordinates.
  80. * SEG_MOVETO and SEG_LINETO types will return one point,
  81. * SEG_QUADTO will return two points,
  82. * SEG_CUBICTO will return 3 points
  83. * and SEG_CLOSE will not return any points.
  84. * @see #SEG_MOVETO
  85. * @see #SEG_LINETO
  86. * @see #SEG_QUADTO
  87. * @see #SEG_CUBICTO
  88. * @see #SEG_CLOSE
  89. */
  90. public int currentSegment(float[] coords) {
  91. if (isDone()) {
  92. throw new NoSuchElementException("ellipse iterator out of bounds");
  93. }
  94. if (index == 5) {
  95. return SEG_CLOSE;
  96. }
  97. if (index == 0) {
  98. double ctrls[] = ctrlpts[3];
  99. coords[0] = (float) (x + ctrls[4] * w);
  100. coords[1] = (float) (y + ctrls[5] * h);
  101. if (affine != null) {
  102. affine.transform(coords, 0, coords, 0, 1);
  103. }
  104. return SEG_MOVETO;
  105. }
  106. double ctrls[] = ctrlpts[index - 1];
  107. coords[0] = (float) (x + ctrls[0] * w);
  108. coords[1] = (float) (y + ctrls[1] * h);
  109. coords[2] = (float) (x + ctrls[2] * w);
  110. coords[3] = (float) (y + ctrls[3] * h);
  111. coords[4] = (float) (x + ctrls[4] * w);
  112. coords[5] = (float) (y + ctrls[5] * h);
  113. if (affine != null) {
  114. affine.transform(coords, 0, coords, 0, 3);
  115. }
  116. return SEG_CUBICTO;
  117. }
  118. /**
  119. * Returns the coordinates and type of the current path segment in
  120. * the iteration.
  121. * The return value is the path segment type:
  122. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  123. * A double array of length 6 must be passed in and may be used to
  124. * store the coordinates of the point(s).
  125. * Each point is stored as a pair of double x,y coordinates.
  126. * SEG_MOVETO and SEG_LINETO types will return one point,
  127. * SEG_QUADTO will return two points,
  128. * SEG_CUBICTO will return 3 points
  129. * and SEG_CLOSE will not return any points.
  130. * @see #SEG_MOVETO
  131. * @see #SEG_LINETO
  132. * @see #SEG_QUADTO
  133. * @see #SEG_CUBICTO
  134. * @see #SEG_CLOSE
  135. */
  136. public int currentSegment(double[] coords) {
  137. if (isDone()) {
  138. throw new NoSuchElementException("ellipse iterator out of bounds");
  139. }
  140. if (index == 5) {
  141. return SEG_CLOSE;
  142. }
  143. if (index == 0) {
  144. double ctrls[] = ctrlpts[3];
  145. coords[0] = x + ctrls[4] * w;
  146. coords[1] = y + ctrls[5] * h;
  147. if (affine != null) {
  148. affine.transform(coords, 0, coords, 0, 1);
  149. }
  150. return SEG_MOVETO;
  151. }
  152. double ctrls[] = ctrlpts[index - 1];
  153. coords[0] = x + ctrls[0] * w;
  154. coords[1] = y + ctrls[1] * h;
  155. coords[2] = x + ctrls[2] * w;
  156. coords[3] = y + ctrls[3] * h;
  157. coords[4] = x + ctrls[4] * w;
  158. coords[5] = y + ctrls[5] * h;
  159. if (affine != null) {
  160. affine.transform(coords, 0, coords, 0, 3);
  161. }
  162. return SEG_CUBICTO;
  163. }
  164. }