package cse1030.drawing;
/**
* A class that represents two dimensional points. A Point2D
has an
* x and a y coordinate.
*
* @author CSE1030Z
*
*/
public final class Point2D {
private double x;
private double y;
/**
* Creates a point with coordinates (0.0, 0.0)
.
*/
public Point2D() {
this(0., 0.);
}
/**
* Creates a point that has the same coordinates as another point.
*
* @param other
* the Point2D
object to copy
*/
public Point2D(Point2D other) {
this(other.getX(), other.getY());
}
/**
* Creates a point with coordinates (x, y)
.
*/
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Get the x coordinate point.
*
* @return the x coordinate of the point
*/
public double getX() {
return x;
}
/**
* Set the x coordinate of the point.
*
* @param x
* the new value for the x coordinate
*/
public void setX(double x) {
this.x = x;
}
/**
* Get the y coordinate of the point.
*
* @return the y coordinate of the point
*/
public double getY() {
return y;
}
/**
* Set the y coordinate of the point.
*
* @param y
* the new value for the y coordinate
*/
public void setY(double y) {
this.y = y;
}
/**
* Computes a hash code for this point.
*
* @return a hash code for this point
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/**
* Compare this point to another object for equality. The result is
* true
if and only if the argument is not null
and
* is a Point2D
object having the same coordinates as this point.
*
* @param obj
* the object to compare this point against
* @return true
if the given object represents a
* Point2D
equivalent to this point, false
* otherwise
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Point2D other = (Point2D) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) {
return false;
}
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) {
return false;
}
return true;
}
/**
* Returns a string representation of the point. The returned string has the
* form:
*
*
* (X, Y)
where X
is the x coordinate of the point
* and Y
is the y coordinate of the point.
*
* @return a string representation of the point
*/
public String toString() {
return "(" + this.getX() + ", " + this.getY() + ")";
}
}