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>Point2D</code> class defines a point representing a location
0N/A * in {@code (x,y)} coordinate space.
0N/A * <p>
0N/A * This class is only the abstract superclass for all objects that
0N/A * store a 2D coordinate.
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 Point2D implements Cloneable {
0N/A
0N/A /**
0N/A * The <code>Float</code> class defines a point specified in float
0N/A * precision.
0N/A * @since 1.2
0N/A */
0N/A public static class Float extends Point2D implements Serializable {
0N/A /**
0N/A * The X coordinate of this <code>Point2D</code>.
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 this <code>Point2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float y;
0N/A
0N/A /**
0N/A * Constructs and initializes a <code>Point2D</code> with
0N/A * coordinates (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 a <code>Point2D</code> with
0N/A * the specified coordinates.
0N/A *
0N/A * @param x the X coordinate of the newly
0N/A * constructed <code>Point2D</code>
0N/A * @param y the Y coordinate of the newly
0N/A * constructed <code>Point2D</code>
0N/A * @since 1.2
0N/A */
0N/A public Float(float x, float y) {
0N/A this.x = x;
0N/A this.y = y;
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 void setLocation(double x, double y) {
0N/A this.x = (float) x;
0N/A this.y = (float) y;
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of this <code>Point2D</code> to the
0N/A * specified <code>float</code> coordinates.
0N/A *
0N/A * @param x the new X coordinate of this {@code Point2D}
0N/A * @param y the new Y coordinate of this {@code Point2D}
0N/A * @since 1.2
0N/A */
0N/A public void setLocation(float x, float y) {
0N/A this.x = x;
0N/A this.y = y;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>String</code> that represents the value
0N/A * of this <code>Point2D</code>.
0N/A * @return a string representation of this <code>Point2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public String toString() {
0N/A return "Point2D.Float["+x+", "+y+"]";
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -2870572449815403710L;
0N/A }
0N/A
0N/A /**
0N/A * The <code>Double</code> class defines a point specified in
0N/A * <code>double</code> precision.
0N/A * @since 1.2
0N/A */
0N/A public static class Double extends Point2D implements Serializable {
0N/A /**
0N/A * The X coordinate of this <code>Point2D</code>.
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 this <code>Point2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double y;
0N/A
0N/A /**
0N/A * Constructs and initializes a <code>Point2D</code> with
0N/A * coordinates (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 a <code>Point2D</code> with the
0N/A * specified coordinates.
0N/A *
0N/A * @param x the X coordinate of the newly
0N/A * constructed <code>Point2D</code>
0N/A * @param y the Y coordinate of the newly
0N/A * constructed <code>Point2D</code>
0N/A * @since 1.2
0N/A */
0N/A public Double(double x, double y) {
0N/A this.x = x;
0N/A this.y = y;
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 void setLocation(double x, double y) {
0N/A this.x = x;
0N/A this.y = y;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>String</code> that represents the value
0N/A * of this <code>Point2D</code>.
0N/A * @return a string representation of this <code>Point2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public String toString() {
0N/A return "Point2D.Double["+x+", "+y+"]";
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 6150783262733311327L;
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.Point2D.Float
0N/A * @see java.awt.geom.Point2D.Double
0N/A * @see java.awt.Point
0N/A * @since 1.2
0N/A */
0N/A protected Point2D() {
0N/A }
0N/A
0N/A /**
0N/A * Returns the X coordinate of this <code>Point2D</code> in
0N/A * <code>double</code> precision.
0N/A * @return the X coordinate of this <code>Point2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getX();
0N/A
0N/A /**
0N/A * Returns the Y coordinate of this <code>Point2D</code> in
0N/A * <code>double</code> precision.
0N/A * @return the Y coordinate of this <code>Point2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getY();
0N/A
0N/A /**
0N/A * Sets the location of this <code>Point2D</code> to the
0N/A * specified <code>double</code> coordinates.
0N/A *
0N/A * @param x the new X coordinate of this {@code Point2D}
0N/A * @param y the new Y coordinate of this {@code Point2D}
0N/A * @since 1.2
0N/A */
0N/A public abstract void setLocation(double x, double y);
0N/A
0N/A /**
0N/A * Sets the location of this <code>Point2D</code> to the same
0N/A * coordinates as the specified <code>Point2D</code> object.
0N/A * @param p the specified <code>Point2D</code> to which to set
0N/A * this <code>Point2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setLocation(Point2D p) {
0N/A setLocation(p.getX(), p.getY());
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance between two points.
0N/A *
0N/A * @param x1 the X coordinate of the first specified point
0N/A * @param y1 the Y coordinate of the first specified point
0N/A * @param x2 the X coordinate of the second specified point
0N/A * @param y2 the Y coordinate of the second specified point
0N/A * @return the square of the distance between the two
0N/A * sets of specified coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static double distanceSq(double x1, double y1,
0N/A double x2, double y2)
0N/A {
0N/A x1 -= x2;
0N/A y1 -= y2;
0N/A return (x1 * x1 + y1 * y1);
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance between two points.
0N/A *
0N/A * @param x1 the X coordinate of the first specified point
0N/A * @param y1 the Y coordinate of the first specified point
0N/A * @param x2 the X coordinate of the second specified point
0N/A * @param y2 the Y coordinate of the second specified point
0N/A * @return the distance between the two sets of specified
0N/A * coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static double distance(double x1, double y1,
0N/A double x2, double y2)
0N/A {
0N/A x1 -= x2;
0N/A y1 -= y2;
0N/A return Math.sqrt(x1 * x1 + y1 * y1);
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance from this
0N/A * <code>Point2D</code> to a specified point.
0N/A *
0N/A * @param px the X coordinate of the specified point to be measured
0N/A * against this <code>Point2D</code>
0N/A * @param py the Y coordinate of the specified point to be measured
0N/A * against this <code>Point2D</code>
0N/A * @return the square of the distance between this
0N/A * <code>Point2D</code> and the specified point.
0N/A * @since 1.2
0N/A */
0N/A public double distanceSq(double px, double py) {
0N/A px -= getX();
0N/A py -= getY();
0N/A return (px * px + py * py);
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance from this
0N/A * <code>Point2D</code> to a specified <code>Point2D</code>.
0N/A *
0N/A * @param pt the specified point to be measured
0N/A * against this <code>Point2D</code>
0N/A * @return the square of the distance between this
0N/A * <code>Point2D</code> to a specified <code>Point2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public double distanceSq(Point2D pt) {
0N/A double px = pt.getX() - this.getX();
0N/A double py = pt.getY() - this.getY();
0N/A return (px * px + py * py);
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance from this <code>Point2D</code> to
0N/A * a specified point.
0N/A *
0N/A * @param px the X coordinate of the specified point to be measured
0N/A * against this <code>Point2D</code>
0N/A * @param py the Y coordinate of the specified point to be measured
0N/A * against this <code>Point2D</code>
0N/A * @return the distance between this <code>Point2D</code>
0N/A * and a specified point.
0N/A * @since 1.2
0N/A */
0N/A public double distance(double px, double py) {
0N/A px -= getX();
0N/A py -= getY();
0N/A return Math.sqrt(px * px + py * py);
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance from this <code>Point2D</code> to a
0N/A * specified <code>Point2D</code>.
0N/A *
0N/A * @param pt the specified point to be measured
0N/A * against this <code>Point2D</code>
0N/A * @return the distance between this <code>Point2D</code> and
0N/A * the specified <code>Point2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public double distance(Point2D pt) {
0N/A double px = pt.getX() - this.getX();
0N/A double py = pt.getY() - this.getY();
0N/A return Math.sqrt(px * px + py * py);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new object of the same class and with the
0N/A * same contents as this object.
0N/A * @return a clone of this instance.
0N/A * @exception OutOfMemoryError if there is not enough memory.
0N/A * @see java.lang.Cloneable
0N/A * @since 1.2
0N/A */
0N/A public Object clone() {
0N/A try {
0N/A return super.clone();
0N/A } catch (CloneNotSupportedException e) {
0N/A // this shouldn't happen, since we are Cloneable
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the hashcode for this <code>Point2D</code>.
0N/A * @return a hash code for this <code>Point2D</code>.
0N/A */
0N/A public int hashCode() {
0N/A long bits = java.lang.Double.doubleToLongBits(getX());
0N/A bits ^= java.lang.Double.doubleToLongBits(getY()) * 31;
0N/A return (((int) bits) ^ ((int) (bits >> 32)));
0N/A }
0N/A
0N/A /**
0N/A * Determines whether or not two points are equal. Two instances of
0N/A * <code>Point2D</code> are equal if the values of their
0N/A * <code>x</code> and <code>y</code> member fields, representing
0N/A * their position in the coordinate space, are the same.
0N/A * @param obj an object to be compared with this <code>Point2D</code>
0N/A * @return <code>true</code> if the object to be compared is
0N/A * an instance of <code>Point2D</code> and has
0N/A * the same values; <code>false</code> otherwise.
0N/A * @since 1.2
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj instanceof Point2D) {
0N/A Point2D p2d = (Point2D) obj;
0N/A return (getX() == p2d.getX()) && (getY() == p2d.getY());
0N/A }
0N/A return super.equals(obj);
0N/A }
0N/A}