Class Vector2D

Hierarchy: Object , Vector2D
public class Vector2D
A 2-dimensional mathematical vector represented by double-precision X and Y components.
Authors:
mbdavis
public Vector2D()
public Vector2D(double x, double y)
public Vector2D(Vector2D v)
public Vector2D(Coordinate from, Coordinate to)
public Vector2D(Coordinate v)
public static Vector2D create(double x, double y)
Creates a new vector with given X and Y components.
Parameters:
x - x the x component
y - y the y component
Returns:
a new vector
public static Vector2D create(Vector2D v)
Creates a new vector from an existing one.
Parameters:
v - v the vector to copy
Returns:
a new vector
public static Vector2D create(Coordinate coord)
Creates a vector from a Coordinate.
Parameters:
coord - coord the Coordinate to copy
Returns:
a new vector
public static Vector2D create(Coordinate from, Coordinate to)
Creates a vector with the direction and magnitude of the difference between the to and from Coordinates.
Parameters:
from - from the origin Coordinate
to - to the destination Coordinate
Returns:
a new vector
public double getX()
public double getY()
public double getComponent(int index)
public Vector2D add(Vector2D v)
public Vector2D subtract(Vector2D v)
public Vector2D multiply(double d)
Multiplies the vector by a scalar value.
Parameters:
d - d the value to multiply by
Returns:
a new vector with the value v * d
public Vector2D divide(double d)
Divides the vector by a scalar value.
Parameters:
d - d the value to divide by
Returns:
a new vector with the value v / d
public Vector2D negate()
public double length()
public double lengthSquared()
public Vector2D normalize()
public Vector2D average(Vector2D v)
public Vector2D weightedSum(Vector2D v, double frac)
Computes the weighted sum of this vector with another vector, with this vector contributing a fraction of frac to the total.

In other words,

 sum = frac * this + (1 - frac) * v
 
Parameters:
v - v the vector to sum
frac - frac the fraction of the total contributed by this vector
Returns:
the weighted sum of the two vectors
public double distance(Vector2D v)
Computes the distance between this vector and another one.
Parameters:
v - v a vector
Returns:
the distance between the vectors
public double dot(Vector2D v)
Computes the dot-product of two vectors
Parameters:
v - v a vector
Returns:
the dot product of the vectors
public double angle()
public double angle(Vector2D v)
public double angleTo(Vector2D v)
public Vector2D rotate(double angle)
public Vector2D rotateByQuarterCircle(int numQuarters)
Rotates a vector by a given number of quarter-circles (i.e. multiples of 90 degrees or Pi/2 radians). A positive number rotates counter-clockwise, a negative number rotates clockwise. Under this operation the magnitude of the vector and the absolute values of the ordinates do not change, only their sign and ordinate index.
Parameters:
numQuarters - numQuarters the number of quarter-circles to rotate by
Returns:
the rotated vector.
public boolean isParallel(Vector2D v)
public Coordinate translate(Coordinate coord)
public Coordinate toCoordinate()
public Object clone()
Creates a copy of this vector
Returns:
a copy of this vector
public String toString()
Gets a string representation of this vector
Returns:
a string representing this vector
public boolean equals(Object o)
Tests if a vector o has the same values for the x and y components.
Parameters:
o - o a Vector2D with which to do the comparison.
Returns:
true if other is a Vector2D with the same values for the x and y components.
public int hashCode()
Gets a hashcode for this vector.
Returns:
a hashcode for this vector