0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.geom;
0N/A
0N/Aimport java.io.Serializable;
0N/A
0N/A/**
0N/A * The <code>Ellipse2D</code> class describes an ellipse that is defined
0N/A * by a framing rectangle.
0N/A * <p>
0N/A * This class is only the abstract superclass for all objects which
0N/A * store a 2D ellipse.
0N/A * The actual storage representation of the coordinates is left to
0N/A * the subclass.
0N/A *
0N/A * @author Jim Graham
0N/A * @since 1.2
0N/A */
0N/Apublic abstract class Ellipse2D extends RectangularShape {
0N/A
0N/A /**
0N/A * The <code>Float</code> class defines an ellipse specified
0N/A * in <code>float</code> precision.
0N/A * @since 1.2
0N/A */
0N/A public static class Float extends Ellipse2D implements Serializable {
0N/A /**
0N/A * The X coordinate of the upper-left corner of the
0N/A * framing rectangle of this {@code Ellipse2D}.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float x;
0N/A
0N/A /**
0N/A * The Y coordinate of the upper-left corner of the
0N/A * framing rectangle of this {@code Ellipse2D}.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float y;
0N/A
0N/A /**
0N/A * The overall width of this <code>Ellipse2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float width;
0N/A
0N/A /**
0N/A * The overall height of this <code>Ellipse2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float height;
0N/A
0N/A /**
0N/A * Constructs a new <code>Ellipse2D</code>, initialized to
0N/A * location (0,&nbsp;0) and size (0,&nbsp;0).
0N/A * @since 1.2
0N/A */
0N/A public Float() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes an <code>Ellipse2D</code> from the
0N/A * specified coordinates.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner
0N/A * of the framing rectangle
0N/A * @param y the Y coordinate of the upper-left corner
0N/A * of the framing rectangle
0N/A * @param w the width of the framing rectangle
0N/A * @param h the height of the framing rectangle
0N/A * @since 1.2
0N/A */
0N/A public Float(float x, float y, float w, float h) {
0N/A setFrame(x, y, w, h);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX() {
0N/A return (double) x;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY() {
0N/A return (double) y;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getWidth() {
0N/A return (double) width;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getHeight() {
0N/A return (double) height;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean isEmpty() {
0N/A return (width <= 0.0 || height <= 0.0);
0N/A }
0N/A
0N/A /**
0N/A * Sets the location and size of the framing rectangle of this
0N/A * <code>Shape</code> to the specified rectangular values.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner of the
0N/A * specified rectangular shape
0N/A * @param y the Y coordinate of the upper-left corner of the
0N/A * specified rectangular shape
0N/A * @param w the width of the specified rectangular shape
0N/A * @param h the height of the specified rectangular shape
0N/A * @since 1.2
0N/A */
0N/A public void setFrame(float x, float y, float w, float h) {
0N/A this.x = x;
0N/A this.y = y;
0N/A this.width = w;
0N/A this.height = h;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setFrame(double x, double y, double w, double h) {
0N/A this.x = (float) x;
0N/A this.y = (float) y;
0N/A this.width = (float) w;
0N/A this.height = (float) h;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A return new Rectangle2D.Float(x, y, width, height);
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -6633761252372475977L;
0N/A }
0N/A
0N/A /**
0N/A * The <code>Double</code> class defines an ellipse specified
0N/A * in <code>double</code> precision.
0N/A * @since 1.2
0N/A */
0N/A public static class Double extends Ellipse2D implements Serializable {
0N/A /**
0N/A * The X coordinate of the upper-left corner of the
0N/A * framing rectangle of this {@code Ellipse2D}.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double x;
0N/A
0N/A /**
0N/A * The Y coordinate of the upper-left corner of the
0N/A * framing rectangle of this {@code Ellipse2D}.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double y;
0N/A
0N/A /**
0N/A * The overall width of this <code>Ellipse2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double width;
0N/A
0N/A /**
0N/A * The overall height of the <code>Ellipse2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double height;
0N/A
0N/A /**
0N/A * Constructs a new <code>Ellipse2D</code>, initialized to
0N/A * location (0,&nbsp;0) and size (0,&nbsp;0).
0N/A * @since 1.2
0N/A */
0N/A public Double() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes an <code>Ellipse2D</code> from the
0N/A * specified coordinates.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner
0N/A * of the framing rectangle
0N/A * @param y the Y coordinate of the upper-left corner
0N/A * of the framing rectangle
0N/A * @param w the width of the framing rectangle
0N/A * @param h the height of the framing rectangle
0N/A * @since 1.2
0N/A */
0N/A public Double(double x, double y, double w, double h) {
0N/A setFrame(x, y, w, h);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX() {
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY() {
0N/A return y;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getWidth() {
0N/A return width;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getHeight() {
0N/A return height;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean isEmpty() {
0N/A return (width <= 0.0 || height <= 0.0);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setFrame(double x, double y, double w, double h) {
0N/A this.x = x;
0N/A this.y = y;
0N/A this.width = w;
0N/A this.height = h;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A return new Rectangle2D.Double(x, y, width, height);
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 5555464816372320683L;
0N/A }
0N/A
0N/A /**
0N/A * This is an abstract class that cannot be instantiated directly.
0N/A * Type-specific implementation subclasses are available for
0N/A * instantiation and provide a number of formats for storing
0N/A * the information necessary to satisfy the various accessor
0N/A * methods below.
0N/A *
0N/A * @see java.awt.geom.Ellipse2D.Float
0N/A * @see java.awt.geom.Ellipse2D.Double
0N/A * @since 1.2
0N/A */
0N/A protected Ellipse2D() {
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(double x, double y) {
0N/A // Normalize the coordinates compared to the ellipse
0N/A // having a center at 0,0 and a radius of 0.5.
0N/A double ellw = getWidth();
0N/A if (ellw <= 0.0) {
0N/A return false;
0N/A }
0N/A double normx = (x - getX()) / ellw - 0.5;
0N/A double ellh = getHeight();
0N/A if (ellh <= 0.0) {
0N/A return false;
0N/A }
0N/A double normy = (y - getY()) / ellh - 0.5;
0N/A return (normx * normx + normy * normy) < 0.25;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean intersects(double x, double y, double w, double h) {
0N/A if (w <= 0.0 || h <= 0.0) {
0N/A return false;
0N/A }
0N/A // Normalize the rectangular coordinates compared to the ellipse
0N/A // having a center at 0,0 and a radius of 0.5.
0N/A double ellw = getWidth();
0N/A if (ellw <= 0.0) {
0N/A return false;
0N/A }
0N/A double normx0 = (x - getX()) / ellw - 0.5;
0N/A double normx1 = normx0 + w / ellw;
0N/A double ellh = getHeight();
0N/A if (ellh <= 0.0) {
0N/A return false;
0N/A }
0N/A double normy0 = (y - getY()) / ellh - 0.5;
0N/A double normy1 = normy0 + h / ellh;
0N/A // find nearest x (left edge, right edge, 0.0)
0N/A // find nearest y (top edge, bottom edge, 0.0)
0N/A // if nearest x,y is inside circle of radius 0.5, then intersects
0N/A double nearx, neary;
0N/A if (normx0 > 0.0) {
0N/A // center to left of X extents
0N/A nearx = normx0;
0N/A } else if (normx1 < 0.0) {
0N/A // center to right of X extents
0N/A nearx = normx1;
0N/A } else {
0N/A nearx = 0.0;
0N/A }
0N/A if (normy0 > 0.0) {
0N/A // center above Y extents
0N/A neary = normy0;
0N/A } else if (normy1 < 0.0) {
0N/A // center below Y extents
0N/A neary = normy1;
0N/A } else {
0N/A neary = 0.0;
0N/A }
0N/A return (nearx * nearx + neary * neary) < 0.25;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(double x, double y, double w, double h) {
0N/A return (contains(x, y) &&
0N/A contains(x + w, y) &&
0N/A contains(x, y + h) &&
0N/A contains(x + w, y + h));
0N/A }
0N/A
0N/A /**
0N/A * Returns an iteration object that defines the boundary of this
0N/A * <code>Ellipse2D</code>.
0N/A * The iterator for this class is multi-threaded safe, which means
0N/A * that this <code>Ellipse2D</code> class guarantees that
0N/A * modifications to the geometry of this <code>Ellipse2D</code>
0N/A * object do not affect any iterations of that geometry that
0N/A * are already in process.
0N/A * @param at an optional <code>AffineTransform</code> to be applied to
0N/A * the coordinates as they are returned in the iteration, or
0N/A * <code>null</code> if untransformed coordinates are desired
0N/A * @return the <code>PathIterator</code> object that returns the
0N/A * geometry of the outline of this <code>Ellipse2D</code>,
0N/A * one segment at a time.
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at) {
0N/A return new EllipseIterator(this, at);
0N/A }
0N/A
0N/A /**
0N/A * Returns the hashcode for this <code>Ellipse2D</code>.
0N/A * @return the hashcode for this <code>Ellipse2D</code>.
0N/A * @since 1.6
0N/A */
0N/A public int hashCode() {
0N/A long bits = java.lang.Double.doubleToLongBits(getX());
0N/A bits += java.lang.Double.doubleToLongBits(getY()) * 37;
0N/A bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
0N/A bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
0N/A return (((int) bits) ^ ((int) (bits >> 32)));
0N/A }
0N/A
0N/A /**
0N/A * Determines whether or not the specified <code>Object</code> is
0N/A * equal to this <code>Ellipse2D</code>. The specified
0N/A * <code>Object</code> is equal to this <code>Ellipse2D</code>
0N/A * if it is an instance of <code>Ellipse2D</code> and if its
0N/A * location and size are the same as this <code>Ellipse2D</code>.
0N/A * @param obj an <code>Object</code> to be compared with this
0N/A * <code>Ellipse2D</code>.
0N/A * @return <code>true</code> if <code>obj</code> is an instance
0N/A * of <code>Ellipse2D</code> and has the same values;
0N/A * <code>false</code> otherwise.
0N/A * @since 1.6
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj == this) {
0N/A return true;
0N/A }
0N/A if (obj instanceof Ellipse2D) {
0N/A Ellipse2D e2d = (Ellipse2D) obj;
0N/A return ((getX() == e2d.getX()) &&
0N/A (getY() == e2d.getY()) &&
0N/A (getWidth() == e2d.getWidth()) &&
0N/A (getHeight() == e2d.getHeight()));
0N/A }
0N/A return false;
0N/A }
0N/A}