fang
Class Location2D

java.lang.Object
  extended by java.awt.geom.Point2D
      extended by java.awt.geom.Point2D.Double
          extended by fang.Location2D
All Implemented Interfaces:
Cloneable

public class Location2D
extends Point2D.Double

This class represents an (x, y) coordinate within some space. Both coordinates are double values so they can hold fractional parts. In FANG, Location2D is used to represent points on the screen (where coordinates go from 0.0 to 1.0). They can also be used to hold velocities and other two-dimensional vectors.

Author:
Robert C. Duvall, Jam Jenkins, Brian C. Ladd

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.geom.Point2D
Point2D.Double, Point2D.Float
 
Field Summary
 
Fields inherited from class java.awt.geom.Point2D.Double
x, y
 
Constructor Summary
Location2D()
          Creates a new Location2D instance at (0.0, 0.0).
Location2D(double x, double y)
          Creates a new Location2D instance at (x, y).
Location2D(Location2D original)
          Creates a new Location2D instance that is a copy of the given location.
Location2D(Point2D point)
          Creates a new Location2D instance with the same values as the given point.
 
Method Summary
 Location2D additiveInverse()
          Calculate the additive inverse of this vector
static double componentMagnitude(Location2D originalVector, Location2D directionVector)
          Determine the magnitude of originalVector in the direction of directionVector.
static Location2D componentVector(Location2D originalVector, Location2D directionVector)
          Return the projection of originalVector onto directionVector; this is a vector.
 Location2D difference(Location2D other)
          Calculate the difference of this vector and the other vector.
 double distance(double x, double y)
          Return the Euclidian distance from this location to the location (x, y).
 double distance(Location2D other)
          Return the Euclidian distance from this location to the given location.
 double distance(Sprite sprite)
          Return the Euclidian distance from this location to the location of the sprite.
 double dotProduct(Location2D other)
          Calculate the dot-product of this vector with the given other vector.
 double getX()
          Get the x-coordinate of this location.
 double getY()
          Get the y-coodinate of this location
 boolean intersects(Box2D box)
          Determine whether this location and the given box coincide.
 boolean intersects(Sprite sprite)
          Determine whether this location and the sprite coincide.
 double magnitude()
          Calculate the magnitude (or length) of this vector.
 Location2D norm()
          Return a normalized vector in the same direction as this vector.
 double radians()
          Calculate the rotation of this vector around the origin.
 void setX(double x)
          Set the x-coordinate of this location.
 void setY(double y)
          Set the y-coordinate of this location.
 Location2D sum(Location2D other)
          Calculate the sum of this vector and the other vector.
 String toString()
           
 
Methods inherited from class java.awt.geom.Point2D.Double
setLocation
 
Methods inherited from class java.awt.geom.Point2D
clone, distance, distance, distanceSq, distanceSq, distanceSq, equals, hashCode, setLocation
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Location2D

public Location2D()
Creates a new Location2D instance at (0.0, 0.0).


Location2D

public Location2D(double x,
                  double y)
Creates a new Location2D instance at (x, y).

Parameters:
x - the x-coordinate of the location
y - the y-coordinate of the location

Location2D

public Location2D(Location2D original)
Creates a new Location2D instance that is a copy of the given location.

Parameters:
original - the original location this is to copy

Location2D

public Location2D(Point2D point)
Creates a new Location2D instance with the same values as the given point.

Parameters:
point - the point this location is to copy
Method Detail

distance

public double distance(double x,
                       double y)
Return the Euclidian distance from this location to the location (x, y).

Overrides:
distance in class Point2D
Parameters:
x - x-coordinate of the location to measure distance to
y - y-coordinate of the location to measure distance to return distance (in screens) between this location and the location (x, y)

distance

public double distance(Location2D other)
Return the Euclidian distance from this location to the given location.

Parameters:
other - location to measure the distance to
Returns:
distance (in screens) between this location and other

distance

public double distance(Sprite sprite)
Return the Euclidian distance from this location to the location of the sprite. This is the distance from this location to the center of the sprite.

Parameters:
sprite - the Sprite from which the distance of this location is to be measured
Returns:
distance (in screens) between this location and the location of the sprite

getX

public double getX()
Get the x-coordinate of this location.

Overrides:
getX in class Point2D.Double
Returns:
location's x-coordinate

getY

public double getY()
Get the y-coodinate of this location

Overrides:
getY in class Point2D.Double
Returns:
location's y-coordinate

intersects

public boolean intersects(Box2D box)
Determine whether this location and the given box coincide.

Parameters:
box - the Box2D to check this location against
Returns:
true if the location is within the Box2D, false otherwise

intersects

public boolean intersects(Sprite sprite)
Determine whether this location and the sprite coincide.

Parameters:
sprite - the Sprite to check this location against
Returns:
true if the location is within the Sprite, false otherwise

setX

public void setX(double x)
Set the x-coordinate of this location. The y-coordinate remains unchanged.

Parameters:
x - the new x-coordinate for this location

setY

public void setY(double y)
Set the y-coordinate of this location. The x-coordinate remains unchanged.

Parameters:
y - the new y-coordinate for this location

toString

public String toString()
Overrides:
toString in class Point2D.Double

dotProduct

public double dotProduct(Location2D other)
Calculate the dot-product of this vector with the given other vector. The dot-product is the sum of the product of the components (x1 * x2 + y1 * y2 for vector1 and vector2).

Parameters:
other - Location2D as vector representing value for which we want the dot product
Returns:
dot-product of this and other

magnitude

public double magnitude()
Calculate the magnitude (or length) of this vector.

Returns:
magintude of this vector (the Pythagorean formula!)

norm

public Location2D norm()
Return a normalized vector in the same direction as this vector. That is, a vector with the same direction but a magnitude of 1. (Divide each component by the magintude of the vector).

Returns:
normalized vector in direction of this vector

sum

public Location2D sum(Location2D other)
Calculate the sum of this vector and the other vector.

Parameters:
other - the vector to add to this vector
Returns:
this + other (by component)

difference

public Location2D difference(Location2D other)
Calculate the difference of this vector and the other vector.

Parameters:
other - the vector to subtract from this vector
Returns:
this - other (by component)

additiveInverse

public Location2D additiveInverse()
Calculate the additive inverse of this vector

Returns:
-this (by component)

radians

public double radians()
Calculate the rotation of this vector around the origin.

Returns:

componentMagnitude

public static double componentMagnitude(Location2D originalVector,
                                        Location2D directionVector)
Determine the magnitude of originalVector in the direction of directionVector.

Parameters:
originalVector - the vector (in x, y basis) to be projected into the directionVector
directionVector - the vector onto which the original vector should be projected
Returns:
projected length of the originalVector in the directionVector direction

componentVector

public static Location2D componentVector(Location2D originalVector,
                                         Location2D directionVector)
Return the projection of originalVector onto directionVector; this is a vector.

Parameters:
originalVector - the vector (in x, y basis) to be projected onto the directionVector
directionVector - the vector onto which the originalVector should be projected
Returns:
projected image of originalVector onto directionVector