package ca.uoguelph.squire;
/**
* A class that describes a point in 2-dimensional space using double precision
* floating point.
*
* @author Finnegan Southey
* @version $Name: $ $Revision: 1.11 $, $Date: 2000/11/02 20:45:23 $
* @legal Copyright (c) Finnegan Southey, 1996-1999
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. This program is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Library General Public License for more details. You should
* have received a copy of the GNU Library General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
public final class Point2D
{
/** The x coordinate of this point. **/
public double x;
/** The y coordinate of this point. **/
public double y;
/**
* Constructs a new point that has the coordinates (0, 0).
*/
public Point2D()
{
}
/**
* Constructs a new point that has the specified x and y coordinates.
*
* @param newX the x coordinate of the new point.
* @param newY the y coordinate of the new point.
*/
public Point2D(double newX, double newY)
{
x = newX;
y = newY;
}
/**
* Constructs a new point that has same coordinates as the specified point.
*
* @param point the point from which to copy the coordinates.
*/
public Point2D(Point2D point)
{
this(point.x, point.y);
}
/**
* Sets this coordinates to be the same as that of the specified point.
*
* @param point the point from which to copy the coordinates.
*/
public void copyFrom(Point2D point)
{
x = point.x;
y = point.y;
}
/**
* Returns the x coordinate of this point.
*
* @return the x coordinate of this point.
*/
public double getX()
{
return x;
}
/**
* Returns the y coordinate of this point.
*
* @return the y coordinate of this point.
*/
public double getY()
{
return y;
}
/**
* Sets the x and y coordinates of this point to the specified values.
*
* @param newX the x coordinate of this point.
* @param newY the y coordinate of this point.
*/
public void setCoordinates(double newX, double newY)
{
x = newX;
y = newY;
}
/**
* Translates this point by the specified values.
* Translation acts like x = x + dx and y = y + dy.
*
* @param dx the distance along the axis to translate the x coordinate of this point.
* @param dy the distance along the axis to translate the y coordinate of this point.
*/
public void translate(double dx, double dy)
{
x += dx;
y += dy;
}
/**
* Translates this point by summing it with the specified point.
* Translation acts like x = x + d.x and y = y + d.y.
*
* @param d the point containing the offsets.
*/
public void translate(Point2D d)
{
x += d.x;
y += d.y;
}
/**
* A T T E TN T I O N : this routines might be a break in Philosophy
* and migth get erased again
* translate point with in a "sized world".
* The edges on the x-axes are touching each other as well
* as the edges of the y-axes. So it is someting like a tube Ring
* This routine was put in 4th of May 2008
* @param d the point containing the offsets.
* @param worldSizePoint the point giving the may x and y value of the imagined world
*/
public void translateWithinTubeRing(Point2D d, Point2D worldSizePoint)
{
x = Math.abs(worldSizePoint.x % (x + d.x));
y = Math.abs(worldSizePoint.y % (y + d.y));
}
/**
* A T T E TN T I O N : this routines might be a break in Philosophy
* and migth get erased again
* translate point with in a "sized world".
* The edges on the x-axes are touching each other as well
* as the edges of the y-axes. So it is someting like a tube ring
* This routine was put in 4th of May 2008
* @param dx the distance along the axis to translate the x coordinate of this point.
* @param dy the distance along the axis to translate the y coordinate of this point.
* @param worldSizePoint the point giving the may x and y value of the imagined world
*/
public void translateWithinTubeRing(double dx, double dy, Point2D worldSizePoint)
{
x = Math.abs(worldSizePoint.x % (x + dx));
y = Math.abs(worldSizePoint.y % (y + dy));
}
/**
* A T T E TN T I O N : this routines might be a break in Philosophy
* and migth get erased again
* translate point with in a "sized world".
* The edges on the x-axes are touching each other as well
* as the edges of the y-axes. So it is someting like a tube Ring
* This routine was put in 4th of May 2008
* @param dx the distance along the axis to translate the x coordinate of this point.
* @param dy the distance along the axis to translate the y coordinate of this point.
* @param wolrdSizeX witdth of the imagined world
* @param wolrdSizeX witdth of the imagined world
*/
public void translateWithinTubeRing(double dx, double dy, double wolrdSizeX, double worldSizeY)
{
x = Math.abs(wolrdSizeX % (x + dx));
y = Math.abs(worldSizeY % (y + dy));
}
/**
* Rotates this point around the specified center point by the specified angle.
*
* @param centerX the x coordinate of the point around which to rotate this point.
* @param centerY the y coordinate of the point around which to rotate this point.
* @param angle the angle, in radians, of the rotation.
*/
public void rotate(double centerX, double centerY, double angle)
{
double tx = x - centerX;
double ty = y - centerY;
double cosA, sinA;
cosA = Math.cos(angle);
sinA = Math.sin(angle);
x = tx * cosA - ty * sinA + centerX;
y = tx * sinA + ty * cosA + centerY;
}
/**
* Rotates this point around the specified center point by the specified angle.
*
* @param centerPoint the Point2D around which to rotate this point.
* @param angle the angle, in radians, of the rotation.
*/
public void rotate(Point2D centerPoint, double angle)
{
rotate(centerPoint.x, centerPoint.y, angle);
}
/**
* Rotates this point around (0, 0) by the specified angle.
*
* @param angle the angle, in radians, of the rotation.
*/
public void rotate(double angle)
{
double tx = x;
double ty = y;
double cosA, sinA;
cosA = Math.cos(angle);
sinA = Math.sin(angle);
x = tx * cosA - ty * sinA;
y = tx * sinA + ty * cosA;
}
/**
* Returns the distance from this point to the specified point.
*
* @param targetX the x coordinate of the point whose distance from this point is
* to be computed.
* @param targetY the y coordinate of the point whose distance from this point is
* to be computed.
* @return the computed distance.
*/
public double getDistanceTo(double targetX, double targetY)
{
double dx, dy;
dx = targetX - x;
dy = targetY - y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Returns the distance from this point to the specified point.
*
* @param point the point whose distance from this point is to be computed.
* @return the computed distance.
*/
public double getDistanceTo(Point2D point)
{
double dx, dy;
dx = point.x - x;
dy = point.y - y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Returns the squared distance from this point to the specified point.
* This is provided because the squared distance may be calculated faster than
* the true distance and may be sufficient for many cases.
*
* @param point the point whose squared distance from this point is to be computed.
* @return the computed squared distance.
*/
public double getSquaredDistanceTo(Point2D point)
{
double dx, dy;
dx = point.x - x;
dy = point.y - y;
return dx * dx + dy * dy;
}
/**
* Returns the squared distance from this point to the specified point.
* This is provided because the squared distance may be calculated faster than
* the true distance and may be sufficient for many cases.
*
* @param targetX the x coordinate of the point whose distance from this point is
* to be computed.
* @param targetY the y coordinate of the point whose distance from this point is
* to be computed.
* @return the computed squared distance.
*/
public double getSquaredDistanceTo(double targetX, double targetY)
{
double dx, dy;
dx = targetX - x;
dy = targetY - y;
return dx * dx + dy * dy;
}
public String toString()
{
return "(" + x + "," + y + ")";
}
/**
* Returns true if the this point has the same coordinates as the specified point.
*
* @return true if the this point has the same coordinates as the specified point.
*/
public boolean equals(Point2D otherPoint)
{
return ( x == otherPoint.x ) && ( y == otherPoint.y );
}
}