1 /* Ellipse2D.java -- represents an ellipse in 2-D space
2 Copyright (C) 2000, 2002, 2004 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.awt.geom;
42 * Ellipse2D is the shape of an ellipse.
44 * <img src="doc-files/Ellipse-1.png" width="347" height="221"
45 * alt="A drawing of an ellipse" /><BR>
46 * The ellipse is defined by it's bounding box (shown in red),
47 * and is defined by the implicit curve:<BR>
48 * <blockquote>(<i>x</i>/<i>a</i>)<sup>2</sup> +
49 * (<i>y</i>/<i>b</i>)<sup>2</sup> = 1<BR><BR></blockquote>
51 * @author Tom Tromey (tromey@cygnus.com)
52 * @author Eric Blake (ebb9@email.byu.edu)
56 public abstract class Ellipse2D extends RectangularShape
59 * Ellipse2D is defined as abstract.
60 * Implementing classes are Ellipse2D.Float and Ellipse2D.Double.
67 * Determines if a point is contained within the ellipse. <P>
68 * @param x - x coordinate of the point.
69 * @param y - y coordinate of the point.
70 * @return true if the point is within the ellipse, false otherwise.
72 public boolean contains(double x, double y)
74 double rx = getWidth() / 2;
75 double ry = getHeight() / 2;
76 double tx = (x - (getX() + rx)) / rx;
77 double ty = (y - (getY() + ry)) / ry;
78 return tx * tx + ty * ty < 1.0;
82 * Determines if a rectangle is completely contained within the
84 * @param x - x coordinate of the upper-left corner of the rectangle
85 * @param y - y coordinate of the upper-left corner of the rectangle
86 * @param w - width of the rectangle
87 * @param h - height of the rectangle
88 * @return true if the rectangle is completely contained, false otherwise.
90 public boolean contains(double x, double y, double w, double h)
94 return (contains(x, y) && contains(x, y2) && contains(x2, y)
99 * Returns a PathIterator object corresponding to the ellipse.<P>
101 * Note: An ellipse cannot be represented exactly in PathIterator
102 * segments, the outline is thefore approximated with cubic
105 * @param at an optional transform.
106 * @return A path iterator.
108 public PathIterator getPathIterator(AffineTransform at)
110 // An ellipse is just a complete arc.
111 return new Arc2D.ArcIterator(this, at);
115 * Determines if a rectangle intersects any part of the ellipse.<P>
116 * @param x - x coordinate of the upper-left corner of the rectangle
117 * @param y - y coordinate of the upper-left corner of the rectangle
118 * @param w - width of the rectangle
119 * @param h - height of the rectangle
120 * @return true if the rectangle intersects the ellipse, false otherwise.
122 public boolean intersects(double x, double y, double w, double h)
124 Rectangle2D r = new Rectangle2D.Double(x, y, w, h);
125 if (! r.intersects(getX(), getY(), getWidth(), getHeight()))
128 if (contains(x, y) || contains(x, y + h) || contains(x + w, y)
129 || contains(x + w, y + h))
132 Line2D l1 = new Line2D.Double(getX(), getY() + (getHeight() / 2),
134 getY() + (getHeight() / 2));
135 Line2D l2 = new Line2D.Double(getX() + (getWidth() / 2), getY(),
136 getX() + (getWidth() / 2),
137 getY() + getHeight());
139 if (l1.intersects(r) || l2.intersects(r))
146 * An {@link Ellipse2D} that stores its coordinates using <code>double</code>
149 public static class Double extends Ellipse2D
152 * The height of the ellipse.
154 public double height;
157 * The width of the ellipse.
162 * The upper-left x coordinate of the bounding-box
167 * The upper-left y coordinate of the bounding-box
172 * Creates a new Ellipse2D with an upper-left coordinate of (0,0)
180 * Creates a new Ellipse2D within a given rectangle
181 * using double-precision coordinates.<P>
182 * @param x - x coordinate of the upper-left of the bounding rectangle
183 * @param y - y coordinate of the upper-left of the bounding rectangle
184 * @param w - width of the ellipse
185 * @param h - height of the ellipse
187 public Double(double x, double y, double w, double h)
196 * Returns the bounding-box of the ellipse.
197 * @return The bounding box.
199 public Rectangle2D getBounds2D()
201 return new Rectangle2D.Double(x, y, width, height);
205 * Returns the height of the ellipse.
206 * @return The height of the ellipse.
208 public double getHeight()
214 * Returns the width of the ellipse.
215 * @return The width of the ellipse.
217 public double getWidth()
223 * Returns x coordinate of the upper-left corner of
224 * the ellipse's bounding-box.
225 * @return The x coordinate.
233 * Returns y coordinate of the upper-left corner of
234 * the ellipse's bounding-box.
235 * @return The y coordinate.
243 * Returns <code>true</code> if the ellipse encloses no area, and
244 * <code>false</code> otherwise.
248 public boolean isEmpty()
250 return height <= 0 || width <= 0;
254 * Sets the geometry of the ellipse's bounding box.<P>
256 * @param x - x coordinate of the upper-left of the bounding rectangle
257 * @param y - y coordinate of the upper-left of the bounding rectangle
258 * @param w - width of the ellipse
259 * @param h - height of the ellipse
261 public void setFrame(double x, double y, double w, double h)
271 * An {@link Ellipse2D} that stores its coordinates using <code>float</code>
274 public static class Float extends Ellipse2D
277 * The height of the ellipse.
282 * The width of the ellipse.
287 * The upper-left x coordinate of the bounding-box
292 * The upper-left y coordinate of the bounding-box
297 * Creates a new Ellipse2D with an upper-left coordinate of (0,0)
305 * Creates a new Ellipse2D within a given rectangle
306 * using floating-point precision.<P>
307 * @param x - x coordinate of the upper-left of the bounding rectangle
308 * @param y - y coordinate of the upper-left of the bounding rectangle
309 * @param w - width of the ellipse
310 * @param h - height of the ellipse
313 public Float(float x, float y, float w, float h)
322 * Returns the bounding-box of the ellipse.
323 * @return The bounding box.
325 public Rectangle2D getBounds2D()
327 return new Rectangle2D.Float(x, y, width, height);
331 * Returns the height of the ellipse.
332 * @return The height of the ellipse.
334 public double getHeight()
340 * Returns the width of the ellipse.
341 * @return The width of the ellipse.
343 public double getWidth()
349 * Returns x coordinate of the upper-left corner of
350 * the ellipse's bounding-box.
351 * @return The x coordinate.
359 * Returns y coordinate of the upper-left corner of
360 * the ellipse's bounding-box.
361 * @return The y coordinate.
369 * Returns <code>true</code> if the ellipse encloses no area, and
370 * <code>false</code> otherwise.
374 public boolean isEmpty()
376 return height <= 0 || width <= 0;
380 * Sets the geometry of the ellipse's bounding box.<P>
382 * @param x - x coordinate of the upper-left of the bounding rectangle
383 * @param y - y coordinate of the upper-left of the bounding rectangle
384 * @param w - width of the ellipse
385 * @param h - height of the ellipse
387 public void setFrame(float x, float y, float w, float h)
396 * Sets the geometry of the ellipse's bounding box.
398 * Note: This leads to a loss of precision.<P>
400 * @param x - x coordinate of the upper-left of the bounding rectangle
401 * @param y - y coordinate of the upper-left of the bounding rectangle
402 * @param w - width of the ellipse
403 * @param h - height of the ellipse
405 public void setFrame(double x, double y, double w, double h)