appletbots.geometry
Class Vector

java.lang.Object
  extended by appletbots.geometry.Vector
All Implemented Interfaces:
java.lang.Comparable

public class Vector
extends java.lang.Object
implements java.lang.Comparable

This class represents an immutable two-dimensional vector

Author:
Erik Rasmussen

Field Summary
 double x
          The x-component of the vector
static Vector X_AXIS
          A normalized vector in the direction of the x-axis
 double y
          The y-component of the vector
static Vector Y_AXIS
          A normalized vector in the direction of the y-axis
static Vector ZERO
          A zero vector
 
Constructor Summary
Vector(double x, double y)
          Creates a new vector with the given x and y components
Vector(Point start, Point end)
          Constructs a new vector from one point to another
 
Method Summary
 Vector add(Vector v)
          Returns the result of adding this vector to another vector
static Vector average(java.util.Collection vectors)
          Returns the average of a collection of vectors
 java.lang.Object clone()
          Returns a copy of this vector
 int compareTo(java.lang.Object o)
          Compares this vector to another based solely on magnitude.
 Vector divide(double divisor)
          Returns the result of dividinig this vector by a scalar number
 double dotProduct(Vector v)
          Returns the dot product of this vector and another vector
 boolean equals(java.lang.Object obj)
          Returns whether or not this vector equals another vector
 double getAngleToVector(Vector v)
          Returns the angle (in radians) from this vector to the given vector
 double getLength()
          Returns the length of the vector
static Vector getRandom(double length)
          Returns a random vector of the given length
 boolean isAligned(Vector v)
          Returns whether or not this vector is aligned (pointing in same direction) to the given vector
 boolean isParallel(Vector v)
          Returns whether or not this vector is parellel to the given vector
 Vector multiply(double multiplicand)
          Returns the result of multiplying this vector times a scalar number
 Vector normalize()
          Creates a new vector aligned with this one with a length of 1
 Vector rotate(double theta)
          Returns the result of rotating this vector by theta radians
 Vector setLength(double length)
          Creates a new vector aligned with this one with the given length
 Vector subtract(Vector v)
          Returns the result of subtracting another vector from this vector
 java.lang.String toString()
          Returns a string representation of this vector
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public double x
The x-component of the vector


y

public double y
The y-component of the vector


ZERO

public static Vector ZERO
A zero vector


X_AXIS

public static Vector X_AXIS
A normalized vector in the direction of the x-axis


Y_AXIS

public static Vector Y_AXIS
A normalized vector in the direction of the y-axis

Constructor Detail

Vector

public Vector(double x,
              double y)
Creates a new vector with the given x and y components

Parameters:
x - The x-component of the vector
y - The y-component of the vector

Vector

public Vector(Point start,
              Point end)
Constructs a new vector from one point to another

Parameters:
start - The start point
end - The end point
Method Detail

getRandom

public static Vector getRandom(double length)
Returns a random vector of the given length

Parameters:
length - The length of the vector
Returns:
A random vector of the given length

average

public static Vector average(java.util.Collection vectors)
Returns the average of a collection of vectors

Parameters:
vectors - A collection of vectors
Returns:
The average of the collection of vectors

getLength

public double getLength()
Returns the length of the vector

Returns:
The length of the vector

setLength

public Vector setLength(double length)
Creates a new vector aligned with this one with the given length

Parameters:
length - The length of the new vector
Returns:
A new vector aligned with this one with the given length

normalize

public Vector normalize()
Creates a new vector aligned with this one with a length of 1

Returns:
A new vector aligned with this one with a length of 1

multiply

public Vector multiply(double multiplicand)
Returns the result of multiplying this vector times a scalar number

Parameters:
multiplicand - The scalar number to multiply the vector by
Returns:
The result of multiplying this vector times a scalar number

divide

public Vector divide(double divisor)
Returns the result of dividinig this vector by a scalar number

Parameters:
divisor - The scalar number to divide the vector by
Returns:
The result of dividing this vector by a scalar number

add

public Vector add(Vector v)
Returns the result of adding this vector to another vector

Parameters:
v - Another vector
Returns:
The result of adding this vector to another vector

subtract

public Vector subtract(Vector v)
Returns the result of subtracting another vector from this vector

Parameters:
v - Another vector
Returns:
The result of subtracting another vector from this vector

dotProduct

public double dotProduct(Vector v)
Returns the dot product of this vector and another vector

Parameters:
v - Another vector
Returns:
The dot product of this vector and another vector

rotate

public Vector rotate(double theta)
Returns the result of rotating this vector by theta radians

Parameters:
theta - The number of radians to rotate the vector
Returns:
The result of rotating this vector by theta radians

getAngleToVector

public double getAngleToVector(Vector v)
Returns the angle (in radians) from this vector to the given vector

Parameters:
v - Another vector
Returns:
The angle (in radians) from this vector to the given vector

isParallel

public boolean isParallel(Vector v)
Returns whether or not this vector is parellel to the given vector

Parameters:
v - Another vector
Returns:
Whether or not this vector is parellel to the given vector

isAligned

public boolean isAligned(Vector v)
Returns whether or not this vector is aligned (pointing in same direction) to the given vector

Parameters:
v - Another vector
Returns:
Whether or not this vector is aligned (pointing in same direction) to the given vector

equals

public boolean equals(java.lang.Object obj)
Returns whether or not this vector equals another vector

Overrides:
equals in class java.lang.Object
Parameters:
obj - Another vector
Returns:
Whether or not this vector equals another vector

clone

public java.lang.Object clone()
Returns a copy of this vector

Overrides:
clone in class java.lang.Object
Returns:
A copy of this vector

toString

public java.lang.String toString()
Returns a string representation of this vector

Overrides:
toString in class java.lang.Object
Returns:
A string representation of this vector

compareTo

public int compareTo(java.lang.Object o)
Compares this vector to another based solely on magnitude.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - the Object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
java.lang.ClassCastException - if the specified object's type prevents it from being compared to this Object.