Ellipse2D.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Ellipse2D.java -- represents an ellipse in 2-D space
  2. Copyright (C) 2000, 2002 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt.geom;
  32. /**
  33. * @author Tom Tromey <tromey@cygnus.com>
  34. * @author Eric Blake <ebb9@email.byu.edu>
  35. * @since 1.2
  36. * @status still needs documentation
  37. */
  38. public abstract class Ellipse2D extends RectangularShape
  39. {
  40. protected Ellipse2D()
  41. {
  42. }
  43. public boolean contains(double x, double y)
  44. {
  45. double rx = getWidth() / 2;
  46. double ry = getHeight() / 2;
  47. double tx = (x - getCenterX()) / rx;
  48. double ty = (y - getCenterY()) / ry;
  49. return tx * tx + ty * ty <= 1.0;
  50. }
  51. public boolean contains(double x, double y, double w, double h)
  52. {
  53. double x2 = x + w;
  54. double y2 = y + h;
  55. return (contains(x, y) && contains(x, y2)
  56. && contains(x2, y) && contains(x2, y2));
  57. }
  58. public PathIterator getPathIterator(AffineTransform at)
  59. {
  60. // An ellipse is just a complete arc.
  61. return new Arc2D.ArcIterator(this, at);
  62. }
  63. public boolean intersects(double x, double y, double w, double h)
  64. {
  65. // fixme
  66. return false;
  67. }
  68. public static class Double extends Ellipse2D
  69. {
  70. public double height;
  71. public double width;
  72. public double x;
  73. public double y;
  74. public Double()
  75. {
  76. }
  77. public Double(double x, double y, double w, double h)
  78. {
  79. this.x = x;
  80. this.y = y;
  81. height = h;
  82. width = w;
  83. }
  84. public Rectangle2D getBounds2D()
  85. {
  86. return new Rectangle2D.Double(x, y, width, height);
  87. }
  88. public double getHeight()
  89. {
  90. return height;
  91. }
  92. public double getWidth()
  93. {
  94. return width;
  95. }
  96. public double getX()
  97. {
  98. return x;
  99. }
  100. public double getY()
  101. {
  102. return y;
  103. }
  104. public boolean isEmpty()
  105. {
  106. return height <= 0 || width <= 0;
  107. }
  108. public void setFrame(double x, double y, double w, double h)
  109. {
  110. this.x = x;
  111. this.y = y;
  112. height = h;
  113. width = w;
  114. }
  115. } // class Double
  116. public static class Float extends Ellipse2D
  117. {
  118. public float height;
  119. public float width;
  120. public float x;
  121. public float y;
  122. public Float()
  123. {
  124. }
  125. public Float(float x, float y, float w, float h)
  126. {
  127. this.x = x;
  128. this.y = y;
  129. this.height = h;
  130. this.width = w;
  131. }
  132. public Rectangle2D getBounds2D()
  133. {
  134. return new Rectangle2D.Float(x, y, width, height);
  135. }
  136. public double getHeight()
  137. {
  138. return height;
  139. }
  140. public double getWidth()
  141. {
  142. return width;
  143. }
  144. public double getX()
  145. {
  146. return x;
  147. }
  148. public double getY()
  149. {
  150. return y;
  151. }
  152. public boolean isEmpty()
  153. {
  154. return height <= 0 || width <= 0;
  155. }
  156. public void setFrame(float x, float y, float w, float h)
  157. {
  158. this.x = x;
  159. this.y = y;
  160. height = h;
  161. width = w;
  162. }
  163. public void setFrame(double x, double y, double w, double h)
  164. {
  165. this.x = (float) x;
  166. this.y = (float) y;
  167. height = (float) h;
  168. width = (float) w;
  169. }
  170. } // class Float
  171. } // class Ellipse2D