java.awt.geom

Class Point2D

public abstract class Point2D extends Object implements Cloneable

This class implements a generic point in 2D Cartesian space. The storage representation is left up to the subclass. Point includes two useful nested classes, for float and double storage respectively.

Since: 1.2

UNKNOWN: updated to 1.4

Nested Class Summary
static classPoint2D.Double
This class defines a point in double precision.
static classPoint2D.Float
This class defines a point in float precision.
Constructor Summary
protected Point2D()
The default constructor.
Method Summary
Objectclone()
Create a new point of the same run-time type with the same contents as this one.
static doubledistance(double x1, double y1, double x2, double y2)
Return the distance between two points.
doubledistance(double x, double y)
Return the distance from this point to the given one.
doubledistance(Point2D p)
Return the distance from this point to the given one.
static doubledistanceSq(double x1, double y1, double x2, double y2)
Return the square of the distance between two points.
doubledistanceSq(double x, double y)
Return the square of the distance from this point to the given one.
doubledistanceSq(Point2D p)
Return the square of the distance from this point to the given one.
booleanequals(Object o)
Compares two points for equality.
abstract doublegetX()
Get the X coordinate, in double precision.
abstract doublegetY()
Get the Y coordinate, in double precision.
inthashCode()
Return the hashcode for this point.
abstract voidsetLocation(double x, double y)
Set the location of this point to the new coordinates.
voidsetLocation(Point2D p)
Set the location of this point to the new coordinates.

Constructor Detail

Point2D

protected Point2D()
The default constructor.

See Also: Point Float Double

Method Detail

clone

public Object clone()
Create a new point of the same run-time type with the same contents as this one.

Returns: the clone

distance

public static double distance(double x1, double y1, double x2, double y2)
Return the distance between two points.

Parameters: x1 the x coordinate of point 1 y1 the y coordinate of point 1 x2 the x coordinate of point 2 y2 the y coordinate of point 2

Returns: the distance from (x1,y1) to (x2,y2)

distance

public double distance(double x, double y)
Return the distance from this point to the given one.

Parameters: x the x coordinate of the other point y the y coordinate of the other point

Returns: the distance

distance

public double distance(Point2D p)
Return the distance from this point to the given one.

Parameters: p the other point

Returns: the distance

Throws: NullPointerException if p is null

distanceSq

public static double distanceSq(double x1, double y1, double x2, double y2)
Return the square of the distance between two points.

Parameters: x1 the x coordinate of point 1 y1 the y coordinate of point 1 x2 the x coordinate of point 2 y2 the y coordinate of point 2

Returns: (x2 - x1)^2 + (y2 - y1)^2

distanceSq

public double distanceSq(double x, double y)
Return the square of the distance from this point to the given one.

Parameters: x the x coordinate of the other point y the y coordinate of the other point

Returns: the square of the distance

distanceSq

public double distanceSq(Point2D p)
Return the square of the distance from this point to the given one.

Parameters: p the other point

Returns: the square of the distance

Throws: NullPointerException if p is null

equals

public boolean equals(Object o)
Compares two points for equality. This returns true if they have the same coordinates.

Parameters: o the point to compare

Returns: true if it is equal

getX

public abstract double getX()
Get the X coordinate, in double precision.

Returns: the x coordinate

getY

public abstract double getY()
Get the Y coordinate, in double precision.

Returns: the y coordinate

hashCode

public int hashCode()
Return the hashcode for this point. The formula is not documented, but appears to be the same as:
 long l = Double.doubleToLongBits(getY());
 l = l * 31 ^ Double.doubleToLongBits(getX());
 return (int) ((l >> 32) ^ l);
 

Returns: the hashcode

setLocation

public abstract void setLocation(double x, double y)
Set the location of this point to the new coordinates. There may be a loss of precision.

Parameters: x the new x coordinate y the new y coordinate

setLocation

public void setLocation(Point2D p)
Set the location of this point to the new coordinates. There may be a loss of precision.

Parameters: p the point to copy

Throws: NullPointerException if p is null