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