Vector2D< T > Class Template Reference

A templatized math class representing a 2-dimensional vector. More...

#include <rimPhysicsVector2D.h>

List of all members.

Public Member Functions

 Vector2D ()
 Create a new 2D vector with all elements equal to zero.
 Vector2D (T value)
 Create a new 2D vector with all elements equal to a single value.
 Vector2D (T *array)
 Create a new 2D vector from a pointer to a 2 element array.
 Vector2D (T newX, T newY)
 Create a new 2D vector by specifying it's x and y values.
 Vector2D (const Vector2D< T > &vector)
 Create a new 2D vector from an existing vector (copy it).
template<typename U>
 Vector2D (const Vector2D< U > &vector)
 Create a new 2D vector from an existing vector (copy it), templatized version.
getMagnitude () const
 Get the magnitude of this vector (the length).
getMagnitudeSquared () const
 Get the square of the magnitude of this vector.
Vector2D< T > normalize () const
 Return a normalized version of this vector.
Vector2D< T > projectOn (const Vector2D< T > &vector) const
 Project this vector on another vector and return the projected vector.
getDistanceTo (const Vector2D< T > &vector) const
 Get the distance from this vector to another in 2D space.
getDistanceToSquared (const Vector2D< T > &vector) const
 Get the square of the distance from this vector to another in 2D space.
const T * getCString () const
 Get a shallow array representation of this vector.
T * toArray () const
 Get an array copy of this vector.
getX () const
 Get the X coordinate of this vector.
getY () const
 Get the Y coordinate of this vector.
get (int index) const
 Get an indexed coordinate of this vector.
void setX (T newX)
 Set the X coordinate of the vector to the specified value.
void setY (T newY)
 Set the Y coordinate of the vector to the specified value.
void set (T newX, T newY)
 Set the X and Y coordinates of the vector to the specified values.
void setIndex (int index, T newValue)
 Set an indexed coordinate of this vector.
template<typename U>
 operator Vector2D< U > ()
 This operator casts this vector to another with different template paramter.
template<typename U>
 operator Vector2D< U > () const
 This operator casts this vector to another with different template paramter.
Vector2D< T > operator- () const
 Negate a vector.
Vector2D< T > operator+ () const
 Postive a vector, returning a copy of it. Operator does nothing.
Vector2D< T > operator+ (const Vector2D< T > &vector) const
 Add this vector to another and return the result.
Vector2D< T > operator+ (const T &value) const
 Add a value to every component of this vector.
Vector2D< T > operator- (const Vector2D< T > &vector) const
 Subtract a vector from this vector component-wise and return the result.
Vector2D< T > operator- (const T &value) const
 Subtract a value from every component of this vector.
Vector2D< T > operator * (const Vector2D< T > &vector) const
 Multiply component-wise this vector and another vector.
Vector2D< T > operator * (const T &value) const
 Multiply every component of this vector by a value and return the result.
Vector2D< T > operator/ (const T &value) const
 Divide every component of this vector by a value and return the result.
Vector2D< T > & operator+= (const Vector2D< T > &vector)
 Add a vector to this vector, modifying this original vector.
Vector2D< T > & operator-= (const Vector2D< T > &vector)
 Subtract a vector from this vector, modifying this original vector.
Vector2D< T > & operator *= (const Vector2D< T > &vector)
 Multiply component-wise this vector and another vector and modify this vector.
Vector2D< T > & operator+= (const T &value)
 Add a value to each component of this vector, modifying it.
Vector2D< T > & operator-= (const T &value)
 Subtract a value from each component of this vector, modifying it.
Vector2D< T > & operator *= (const T &value)
 Multiply a value with each component of this vector, modifying it.
Vector2D< T > & operator/= (const T &value)
 Divide each component of this vector by a value, modifying it.

Public Attributes

x
 The X coordinate of a 2D vector.
y
 The Y coordinate of a 2D vector.

Static Public Attributes

static const Vector2D< T > ZERO
 A constant vector with all elements equal to zero.


Detailed Description

template<typename T>
class Vector2D< T >

A templatized math class representing a 2-dimensional vector.

Constructor & Destructor Documentation

template<typename T>
Vector2D< T >::Vector2D (  )  [inline]

Create a new 2D vector with all elements equal to zero.

template<typename T>
Vector2D< T >::Vector2D ( value  )  [inline]

Create a new 2D vector with all elements equal to a single value.

This constructor creates a uniform 2D vector with all elements equal to each other and equal to the single constructor parameter value.

Parameters:
value - The value to set all elements of the vector to.

template<typename T>
Vector2D< T >::Vector2D ( T *  array  )  [inline]

Create a new 2D vector from a pointer to a 2 element array.

This constructor takes a pointer to an array of 2 values and sets it's x and y coordinates to be the 0th, 1th, indexed values in the array. No error checking is performed, so make sure to pass in a correct array of values or expect the worst.

Parameters:
array - An indexed array of 2 values for the vector's coordinates.

template<typename T>
Vector2D< T >::Vector2D ( newX,
newY 
) [inline]

Create a new 2D vector by specifying it's x and y values.

This constructor sets each of the vector's x, and y components values to be the 1st, and 2nd parameters of the constructor, respectively.

Parameters:
newX - The X coordinate of the new vector.
newY - The Y coordinate of the new vector.

template<typename T>
Vector2D< T >::Vector2D ( const Vector2D< T > &  vector  )  [inline]

Create a new 2D vector from an existing vector (copy it).

This constructor takes the x and y components of the vector parameter and sets the components of this vector to be the same.

Parameters:
vector - The vector to be copied.

template<typename T>
template<typename U>
Vector2D< T >::Vector2D ( const Vector2D< U > &  vector  )  [inline]

Create a new 2D vector from an existing vector (copy it), templatized version.

This constructor takes the x and y components of the vector parameter and sets the components of this vector to be the same. This is a templatized version of the above copy constructor.

Parameters:
vector - The vector to be copied.


Member Function Documentation

template<typename T>
T Vector2D< T >::getMagnitude (  )  const [inline]

Get the magnitude of this vector (the length).

This Method finds the magnitude of this vector using the formula: magnitude = sqrt( x*x + y*y ). It's value is always positive.

Returns:
the magnitude of the vector.

template<typename T>
T Vector2D< T >::getMagnitudeSquared (  )  const [inline]

Get the square of the magnitude of this vector.

This method is provided in addition to the above getMagnitude() method in order to provide a faster alternate way of computing a magnitude-like quantity for the vector. For instance, when computing whether or not the magnitude of a vector is greater than some value, one can instead compare the square of the magnitude to the square of the value (avoiding the square root operation), with the same result.

Returns:
the square of the magnitude of this vector.

template<typename T>
Vector2D<T> Vector2D< T >::normalize (  )  const [inline]

Return a normalized version of this vector.

This method normalizes this vector by dividing each component by the vector's magnitude and returning the result. This method does not modify the original vector.

Returns:
a normalized version of this vector.

template<typename T>
Vector2D<T> Vector2D< T >::projectOn ( const Vector2D< T > &  vector  )  const [inline]

Project this vector on another vector and return the projected vector.

This method computes the vectoral projection of this vector onto another, and returns the resulting vector. This result should be parallel (within the precision of the vectors) to the original parameter vector. This method does not modify either original vector.

Parameters:
vector - The vector to project this vector on.
Returns:
the vectoral projection onto the parameter vector of this vector.

template<typename T>
T Vector2D< T >::getDistanceTo ( const Vector2D< T > &  vector  )  const [inline]

Get the distance from this vector to another in 2D space.

This method essentially computes the magnitude of the subtraction of this vector minus the parameter vector. This treats each vector as a point in 2D space.

Parameters:
vector - the vector to query the distance to.
Returns:
the distance to the parameter vector.

template<typename T>
T Vector2D< T >::getDistanceToSquared ( const Vector2D< T > &  vector  )  const [inline]

Get the square of the distance from this vector to another in 2D space.

This method essentially computes the magnitude squared of the subtraction of this vector minus the parameter vector. This treats each vector as a point in 2D space. This function can be used for faster distance comparisions because it avoids the square root operation of the getDistanceTo() method.

Parameters:
vector - the vector to query the distance to.
Returns:
the distance to the parameter vector.

template<typename T>
const T* Vector2D< T >::getCString (  )  const [inline]

Get a shallow array representation of this vector.

This method returns a pointer to the address of the X coordinate of the vector and does not do any copying of the elements. Therefore, this method should only be used where one needs an array representation of a vector without having to allocate more memory and copy the vector.

Returns:
A pointer to a shallow array copy of this vector.

template<typename T>
T* Vector2D< T >::toArray (  )  const [inline]

Get an array copy of this vector.

This method copies the elements of this vector into a 2 element array. This array is a deep copy, and must be deleted when it is no longer needed, or else a memory leak will occur.

Returns:
A pointer to a deep array copy of this vector.

template<typename T>
T Vector2D< T >::getX (  )  const [inline]

Get the X coordinate of this vector.

template<typename T>
T Vector2D< T >::getY (  )  const [inline]

Get the Y coordinate of this vector.

template<typename T>
T Vector2D< T >::get ( int  index  )  const [inline]

Get an indexed coordinate of this vector.

This method takes an index with the possible values 0 or 1 and returns the X or Y coordinate of this vector, respectively. There is no error checking performed (for speed), so make sure that the index is in the valid bounds mentioned above, or else bad things could happen.

Parameters:
index - An index into the vector with 0 = X, 1 = Y.
Returns:
the value of the vector associated with the given index.

template<typename T>
void Vector2D< T >::setX ( newX  )  [inline]

Set the X coordinate of the vector to the specified value.

template<typename T>
void Vector2D< T >::setY ( newY  )  [inline]

Set the Y coordinate of the vector to the specified value.

template<typename T>
void Vector2D< T >::set ( newX,
newY 
) [inline]

Set the X and Y coordinates of the vector to the specified values.

This method takes 2 parameters representing the 2 coordinates of this vector and sets this vector's coordinates to have those values.

Parameters:
newX - The new X coordinate of the vector.
newY - The new Y coordinate of the vector.

template<typename T>
void Vector2D< T >::setIndex ( int  index,
newValue 
) [inline]

Set an indexed coordinate of this vector.

This method takes an index with the possible values 0 or 1, and sets the X or Y coordinate of this vector to be the new value, respectively. There is no error checking performed (for speed), so make sure that the index is in the valid bounds mentioned above, or else bad things could happen.

Parameters:
index - An index into the vector with 0 = X, 1 = Y.
newValue - The new value of the coordinate with the given index.

template<typename T>
template<typename U>
Vector2D< T >::operator Vector2D< U > (  )  [inline]

This operator casts this vector to another with different template paramter.

This method provides an operator for the casting of this vector to another vector with a potentially different template paramter.

Returns:
the cast version of this vector.

template<typename T>
template<typename U>
Vector2D< T >::operator Vector2D< U > (  )  const [inline]

This operator casts this vector to another with different template paramter.

This method provides an operator for the casting of this vector to another vector with a potentially different template paramter. This is the const version of this operator.

Returns:
the cast version of this vector.

template<typename T>
Vector2D<T> Vector2D< T >::operator- (  )  const [inline]

Negate a vector.

This method returns the negation of a vector, making it point in the opposite direction. It does not modify the original vector.

Returns:
the negation of the original vector.

template<typename T>
Vector2D<T> Vector2D< T >::operator+ (  )  const [inline]

Postive a vector, returning a copy of it. Operator does nothing.

This operator doesn't do anything but return the original value of the vector that it affects.

Returns:
the same vector as the orignal vector.

template<typename T>
Vector2D<T> Vector2D< T >::operator+ ( const Vector2D< T > &  vector  )  const [inline]

Add this vector to another and return the result.

This method adds another vector to this one, component-wise, and returns this addition. It does not modify either of the original vectors.

Parameters:
vector - The vector to add to this one.
Returns:
The addition of this vector and the parameter.

template<typename T>
Vector2D<T> Vector2D< T >::operator+ ( const T &  value  )  const [inline]

Add a value to every component of this vector.

This method adds the value parameter to every component of the vector, and returns a vector representing this result. It does not modifiy the original vector.

Parameters:
value - The value to add to all components of this vector.
Returns:
The resulting vector of this addition.

template<typename T>
Vector2D<T> Vector2D< T >::operator- ( const Vector2D< T > &  vector  )  const [inline]

Subtract a vector from this vector component-wise and return the result.

This method subtracts another vector from this one, component-wise, and returns this subtraction. It does not modify either of the original vectors.

Parameters:
vector - The vector to subtract from this one.
Returns:
The subtraction of the the parameter from this vector.

template<typename T>
Vector2D<T> Vector2D< T >::operator- ( const T &  value  )  const [inline]

Subtract a value from every component of this vector.

This method subtracts the value parameter from every component of the vector, and returns a vector representing this result. It does not modifiy the original vector.

Parameters:
value - The value to subtract from all components of this vector.
Returns:
The resulting vector of this subtraction.

template<typename T>
Vector2D<T> Vector2D< T >::operator * ( const Vector2D< T > &  vector  )  const [inline]

Multiply component-wise this vector and another vector.

This operator multiplies each component of this vector by the corresponding component of the other vector and returns a vector representing this result. It does not modify either original vector.

Parameters:
vector - The vector to multiply this vector by.
Returns:
The result of the multiplication.

template<typename T>
Vector2D<T> Vector2D< T >::operator * ( const T &  value  )  const [inline]

Multiply every component of this vector by a value and return the result.

This method multiplies the value parameter with every component of the vector, and returns a vector representing this result. It does not modifiy the original vector.

Parameters:
value - The value to multiplly with all components of this vector.
Returns:
The resulting vector of this multiplication.

template<typename T>
Vector2D<T> Vector2D< T >::operator/ ( const T &  value  )  const [inline]

Divide every component of this vector by a value and return the result.

This method Divides every component of the vector by the value parameter, and returns a vector representing this result. It does not modifiy the original vector.

Parameters:
value - The value to divide all components of this vector by.
Returns:
The resulting vector of this division.

template<typename T>
Vector2D<T>& Vector2D< T >::operator+= ( const Vector2D< T > &  vector  )  [inline]

Add a vector to this vector, modifying this original vector.

This method adds another vector to this vector, component-wise, and sets this vector to have the result of this addition.

Parameters:
vector - The vector to add to this vector.
Returns:
A reference to this modified vector.

template<typename T>
Vector2D<T>& Vector2D< T >::operator-= ( const Vector2D< T > &  vector  )  [inline]

Subtract a vector from this vector, modifying this original vector.

This method subtracts another vector from this vector, component-wise, and sets this vector to have the result of this subtraction.

Parameters:
vector - The vector to subtract from this vector.
Returns:
A reference to this modified vector.

template<typename T>
Vector2D<T>& Vector2D< T >::operator *= ( const Vector2D< T > &  vector  )  [inline]

Multiply component-wise this vector and another vector and modify this vector.

This operator multiplies each component of this vector by the corresponding component of the other vector and modifies this vector to contain the result.

Parameters:
vector - The vector to multiply this vector by.
Returns:
A reference to this modified vector.

template<typename T>
Vector2D<T>& Vector2D< T >::operator+= ( const T &  value  )  [inline]

Add a value to each component of this vector, modifying it.

This operator adds a value to each component of this vector and modifies this vector to store the result.

Parameters:
value - The value to add to every component of this vector.
Returns:
A reference to this modified vector.

template<typename T>
Vector2D<T>& Vector2D< T >::operator-= ( const T &  value  )  [inline]

Subtract a value from each component of this vector, modifying it.

This operator subtracts a value from each component of this vector and modifies this vector to store the result.

Parameters:
value - The value to subtract from every component of this vector.
Returns:
A reference to this modified vector.

template<typename T>
Vector2D<T>& Vector2D< T >::operator *= ( const T &  value  )  [inline]

Multiply a value with each component of this vector, modifying it.

This operator multiplies a value with each component of this vector and modifies this vector to store the result.

Parameters:
value - The value to multiply with every component of this vector.
Returns:
A reference to this modified vector.

template<typename T>
Vector2D<T>& Vector2D< T >::operator/= ( const T &  value  )  [inline]

Divide each component of this vector by a value, modifying it.

This operator Divides each component of this vector by value and modifies this vector to store the result.

Parameters:
value - The value to multiply with every component of this vector.
Returns:
A reference to this modified vector.


Member Data Documentation

template<typename T>
T Vector2D< T >::x

The X coordinate of a 2D vector.

template<typename T>
T Vector2D< T >::y

The Y coordinate of a 2D vector.

template<typename T>
const Vector2D< T > Vector2D< T >::ZERO [inline, static]

A constant vector with all elements equal to zero.


The documentation for this class was generated from the following file:
Generated on Tue Aug 26 00:03:21 2008 for Rim Physics by  doxygen 1.5.4