edu.ou.utils
Class Vector2D

java.lang.Object
  extended by edu.ou.utils.Vector2D
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable<Vector2D>

public final class Vector2D
extends java.lang.Object
implements java.lang.Comparable<Vector2D>, java.lang.Cloneable

Vector2D provides an immutable vector representation as well as a collection of useful vector operations.


Field Summary
static float HALFPI
           
static float THREEHALFPI
           
static float TWOPI
           
static Vector2D X_NEG_UNIT_VECTOR
           
static Vector2D X_UNIT_VECTOR
           
static Vector2D Y_NEG_UNIT_VECTOR
           
static Vector2D Y_UNIT_VECTOR
           
static Vector2D ZERO_VECTOR
           
 
Constructor Summary
Vector2D()
          Create the zero vector
Vector2D(float x, float y)
          Create a vector with the given x and y values.
Vector2D(Vector2D b)
          Create a new vector from an old one;
 
Method Summary
 Vector2D add(Vector2D v)
          Add these two vectors together.
 float angleBetween(Vector2D v)
          The angle (in radians) between this vector and the given vector.
 int compareTo(Vector2D other)
          Compare the vectors on the basis of magnitude.
 float cross(Vector2D v)
          Get the cross product of the two vectors.
 Vector2D divide(float f)
          Divide this vector by the given scalar.
 float dot(Vector2D v)
          Get the dot product of the two vectors.
 boolean equals(java.lang.Object obj)
           
 boolean equals(Vector2D v)
          Determine if two vectors are equal (have the same components)
 Vector2D fastRotate(float cos, float sin)
          Rotate this vector using the given sine and cosine values.
static Vector2D fromAngle(float angle, float magnitude)
          Create a vector from the given angle (in radians) and magnitude
 float getAngle()
          The angle of the vector
 float getMagnitude()
          The magnitude of the vector.
static Vector2D getRandom(java.util.Random rand, float maxMagnitude)
          Create a random vector with a magnitude no greater than specified
 float getX()
          The X component of the vector.
 float getY()
          The Y component of the vector.
 int hashCode()
           
static void main(java.lang.String[] args)
           
 Vector2D multiply(float f)
          Multiply this vector by the given scalar.
 Vector2D negate()
          Reverse this vector.
 Vector2D project(Vector2D v)
          Project the given vector onto this vector.
 Vector2D rotate(float f)
          Rotate this vector by the specified angle (in radians)
 Vector2D subtract(Vector2D v)
          Subtract the other vector from this vector.
 Vector2D subtractAndRotate(Vector2D v, float cos, float sin)
          Subtract the other vector from this vector and rotate the result using the given sine and cosine values.
 java.lang.String toString()
           
 Vector2D unit()
          The unit vector derived from this vector, or an arbitrary unit vector if this is the zero vector
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO_VECTOR

public static final Vector2D ZERO_VECTOR

X_UNIT_VECTOR

public static final Vector2D X_UNIT_VECTOR

X_NEG_UNIT_VECTOR

public static final Vector2D X_NEG_UNIT_VECTOR

Y_UNIT_VECTOR

public static final Vector2D Y_UNIT_VECTOR

Y_NEG_UNIT_VECTOR

public static final Vector2D Y_NEG_UNIT_VECTOR

HALFPI

public static final float HALFPI
See Also:
Constant Field Values

THREEHALFPI

public static final float THREEHALFPI
See Also:
Constant Field Values

TWOPI

public static final float TWOPI
See Also:
Constant Field Values
Constructor Detail

Vector2D

public Vector2D(float x,
                float y)
Create a vector with the given x and y values.

Parameters:
x -
y -

Vector2D

public Vector2D(Vector2D b)
Create a new vector from an old one;


Vector2D

public Vector2D()
Create the zero vector

Method Detail

fromAngle

public static Vector2D fromAngle(float angle,
                                 float magnitude)
Create a vector from the given angle (in radians) and magnitude

Parameters:
angle - In radians
magnitude -
Returns:
A new Vector2D

getRandom

public static Vector2D getRandom(java.util.Random rand,
                                 float maxMagnitude)
Create a random vector with a magnitude no greater than specified

Parameters:
rand - The source of randomness to use.
maxMagnitude -
Returns:
A new random Vector2D

getX

public final float getX()
The X component of the vector.

Returns:
The X component of the vector.

getY

public final float getY()
The Y component of the vector.

Returns:
The Y component of the vector.

getMagnitude

public final float getMagnitude()
The magnitude of the vector. This is now a stored value.

Returns:
The magnitude of the vector.

getAngle

public final float getAngle()
The angle of the vector

Returns:
The angle of the vector.

angleBetween

public final float angleBetween(Vector2D v)
The angle (in radians) between this vector and the given vector. The angle is positive if v is to the left of this vector, and negative if v is to the right of this vector (right hand coords)

Parameters:
v - A given vector.
Returns:
The angle between the two vectors in radians.

unit

public final Vector2D unit()
The unit vector derived from this vector, or an arbitrary unit vector if this is the zero vector

Returns:
A unit vector with the same orientation as this vector.

negate

public final Vector2D negate()
Reverse this vector.

Returns:
The reverse of this vector.

add

public final Vector2D add(Vector2D v)
Add these two vectors together.

Parameters:
v - Vector to add
Returns:
The sum of the vectors.

subtract

public final Vector2D subtract(Vector2D v)
Subtract the other vector from this vector.

Parameters:
v -
Returns:
The vector resulting from subtracting the other vector from this vector.

multiply

public final Vector2D multiply(float f)
Multiply this vector by the given scalar.

Parameters:
f -
Returns:
The scaled vector.

divide

public final Vector2D divide(float f)
Divide this vector by the given scalar.

Parameters:
f -
Returns:
The scaled vector

dot

public final float dot(Vector2D v)
Get the dot product of the two vectors.

Parameters:
v -
Returns:
The dot product

cross

public final float cross(Vector2D v)
Get the cross product of the two vectors.

Parameters:
v -
Returns:
The cross product.

fastRotate

public final Vector2D fastRotate(float cos,
                                 float sin)
Rotate this vector using the given sine and cosine values.

Parameters:
cos -
sin -
Returns:
The rotated vector.

subtractAndRotate

public final Vector2D subtractAndRotate(Vector2D v,
                                        float cos,
                                        float sin)
Subtract the other vector from this vector and rotate the result using the given sine and cosine values.

Parameters:
v -
cos -
sin -
Returns:
The subtracted and rotated vector.

rotate

public final Vector2D rotate(float f)
Rotate this vector by the specified angle (in radians)

Parameters:
f -
Returns:
the rotated vector

project

public final Vector2D project(Vector2D v)
Project the given vector onto this vector.

Parameters:
v -
Returns:
The projection result vector.

equals

public final boolean equals(Vector2D v)
Determine if two vectors are equal (have the same components)

Parameters:
v -
Returns:
True if the components match, false otherwise.

compareTo

public int compareTo(Vector2D other)
Compare the vectors on the basis of magnitude.

Specified by:
compareTo in interface java.lang.Comparable<Vector2D>
Parameters:
other -
Returns:
-1 if this is smaller than, 0 if equal, 1 if this is greater than

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object