home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 7.2 KB | 324 lines |
- /*
- * @(#)Ellipse2D.java 1.5 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt.geom;
-
- /**
- * An ellipse defined by a bounding rectangle.
- * <p>
- * This class is only the abstract superclass for all objects which
- * store a 2D ellipse.
- * The actual storage representation of the coordinates is left to
- * the subclass.
- *
- * @version 10 Feb 1997
- * @author Jim Graham
- */
- public abstract class Ellipse2D extends RectangularShape {
- /**
- * An ellipse specified in float precision.
- */
- public static class Float extends Ellipse2D {
- /**
- * The x coordinate of the upper left corner of the ellipse.
- */
- public float x;
-
- /**
- * The y coordinate of the upper left corner of the ellipse.
- */
- public float y;
-
- /**
- * The overall width of the ellipse.
- */
- public float width;
-
- /**
- * The overall height of the ellipse.
- */
- public float height;
-
- /**
- * Constructs a new ellipse, initialized to location (0, 0) and
- * size (0, 0).
- */
- public Float() {
- }
-
- /**
- * Constructs and initializes an ellipse from the specified coordinates.
- * @param x the x coordinate
- * @param y the y coordinate
- * @param width the width of the rectangle
- * @param height the height of the rectangle
- */
- public Float(float x, float y, float w, float h) {
- setBounds(x, y, w, h);
- }
-
- /**
- * Returns the X coordinate of the upper left corner of the ellipse
- * in double precision.
- */
- public double getX() {
- return (double) x;
- }
-
- /**
- * Returns the Y coordinate of the upper left corner of the ellipse
- * in double precision.
- */
- public double getY() {
- return (double) y;
- }
-
- /**
- * Returns the overall width of the ellipse in double precision.
- */
- public double getWidth() {
- return (double) width;
- }
-
- /**
- * Returns the overall height of the ellipse in double precision.
- */
- public double getHeight() {
- return (double) height;
- }
-
- /**
- * Determines whether the rectangular shape is empty.
- */
- public boolean isEmpty() {
- return (width <= 0.0 || height <= 0.0);
- }
-
- /**
- * Sets the location and size of this ellipse to the specified
- * float values.
- */
- public void setBounds(float x, float y, float w, float h) {
- this.x = x;
- this.y = y;
- this.width = w;
- this.height = h;
- }
-
- /**
- * Sets the location and size of this ellipse to the specified
- * double values.
- */
- public void setBounds(double x, double y, double w, double h) {
- this.x = (float) x;
- this.y = (float) y;
- this.width = (float) w;
- this.height = (float) h;
- }
-
- /**
- * Return the high precision bounding box of the shape.
- */
- public Rectangle2D getBounds2D() {
- return new Rectangle2D.Float(x, y, width, height);
- }
- }
-
- /**
- * An ellipse specified in double precision.
- */
- public static class Double extends Ellipse2D {
- /**
- * The x coordinate of the upper left corner of the ellipse.
- */
- public double x;
-
- /**
- * The y coordinate of the upper left corner of the ellipse.
- */
- public double y;
-
- /**
- * The overall width of the ellipse.
- */
- public double width;
-
- /**
- * The overall height of the ellipse.
- */
- public double height;
-
- /**
- * Constructs a new ellipse, initialized to location (0, 0) and
- * size (0, 0).
- */
- public Double() {
- }
-
- /**
- * Constructs and initializes an ellipse from the specified coordinates.
- * @param x the x coordinate
- * @param y the y coordinate
- * @param width the width of the rectangle
- * @param height the height of the rectangle
- */
- public Double(double x, double y, double w, double h) {
- setBounds(x, y, w, h);
- }
-
- /**
- * Returns the X coordinate of the upper left corner of the ellipse
- * in double precision.
- */
- public double getX() {
- return x;
- }
-
- /**
- * Returns the Y coordinate of the upper left corner of the ellipse
- * in double precision.
- */
- public double getY() {
- return y;
- }
-
- /**
- * Returns the overall width of the ellipse in double precision.
- */
- public double getWidth() {
- return width;
- }
-
- /**
- * Returns the overall height of the ellipse in double precision.
- */
- public double getHeight() {
- return height;
- }
-
- /**
- * Determines whether the rectangular shape is empty.
- */
- public boolean isEmpty() {
- return (width <= 0.0 || height <= 0.0);
- }
-
- /**
- * Sets the location and size of this ellipse to the specified
- * double values.
- */
- public void setBounds(double x, double y, double w, double h) {
- this.x = x;
- this.y = y;
- this.width = w;
- this.height = h;
- }
-
- /**
- * Return the high precision bounding box of the shape.
- */
- public Rectangle2D getBounds2D() {
- return new Rectangle2D.Double(x, y, width, height);
- }
- }
-
- protected Ellipse2D() {
- }
-
- /**
- * Test if a given Point is inside the boundary of the shape.
- */
- public boolean contains(double x, double y) {
- // Normalize the coordinates compared to the ellipse
- // having a center at 0,0 and a radius of 0.5.
- double ellw = getWidth();
- if (ellw <= 0.0) {
- return false;
- }
- double normx = (x - getX()) / ellw - 0.5;
- double ellh = getHeight();
- if (ellh <= 0.0) {
- return false;
- }
- double normy = (y - getY()) / ellh - 0.5;
- return (normx * normx + normy * normy) < 0.25;
- }
-
- /**
- * Test if the interior of the Shape intersects the interior of a given
- * Rectangle.
- */
- public boolean intersects(double x, double y, double w, double h) {
- if (w <= 0.0 || h <= 0.0) {
- return false;
- }
- // Normalize the rectangular coordinates compared to the ellipse
- // having a center at 0,0 and a radius of 0.5.
- double ellw = getWidth();
- if (ellw <= 0.0) {
- return false;
- }
- double normx0 = (x - getX()) / ellw - 0.5;
- double normx1 = normx0 + w / ellw;
- double ellh = getHeight();
- if (ellh <= 0.0) {
- return false;
- }
- double normy0 = (y - getY()) / ellh - 0.5;
- double normy1 = normy0 + h / ellh;
- // find nearest x (left edge, right edge, 0.0)
- // find nearest y (top edge, bottom edge, 0.0)
- // if nearest x,y is inside circle of radius 0.5, then intersects
- double nearx, neary;
- if (normx0 > 0.0) {
- // center to left of X extents
- nearx = normx0;
- } else if (normx1 < 0.0) {
- // center to right of X extents
- nearx = normx1;
- } else {
- nearx = 0.0;
- }
- if (normy0 > 0.0) {
- // center above Y extents
- neary = normy0;
- } else if (normy1 < 0.0) {
- // center below Y extents
- neary = normy1;
- } else {
- neary = 0.0;
- }
- return (nearx * nearx + neary * neary) < 0.25;
- }
-
- /**
- * Test if the interior of the Shape entirely contains the given
- * Rectangle.
- */
- public boolean contains(double x, double y, double w, double h) {
- return (contains(x, y) &&
- contains(x + w, y) &&
- contains(x, y + h) &&
- contains(x + w, y + h));
- }
-
- /**
- * Return an iteration object that defines the boundary of the
- * shape.
- */
- public PathIterator getPathIterator(AffineTransform at) {
- return new EllipseIterator(this, at);
- }
- }
-