v0.1.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Pages
CGUL::Vector2T< Type > Struct Template Reference

A two dimensional 32 bit floating point vector. More...

#include <Vector2.hpp>

Public Member Functions

 Vector2T ()
 Initializes to (0, 0).
 
 Vector2T (const Vector2T &copy)
 Copies a vector into this vector. More...
 
 Vector2T (Type value)
 Initializes all components to a given value. More...
 
 Vector2T (Type x, Type y)
 Initializes the vector with individual component values. More...
 
Vector2Toperator= (const Vector2T &operand)
 Assigns another vector into this vector. More...
 
Type & operator[] (UInt32 index)
 Accesses an individual component in this vector. More...
 
Type operator[] (UInt32 index) const
 Accesses an individual (constant) component in this vector. More...
 
Vector2T operator- () const
 Gets the inverse of the vector. More...
 
bool operator== (const Vector2T &operand) const
 Checks if two vectors are exactly equal. More...
 
bool operator!= (const Vector2T &operand) const
 Checks if two vectors are not exactly equal. More...
 
Vector2T operator+ (const Vector2T &operand) const
 Performs component-based addition on two vectors. More...
 
Vector2Toperator+= (const Vector2T &operand)
 Adds the individual components of another vector to this vector's components. More...
 
Vector2T operator- (const Vector2T &operand) const
 Performs component-based subtraction on two vectors. More...
 
Vector2Toperator-= (const Vector2T &operand)
 Subtracts the individual components of another vector from this vector's components. More...
 
Vector2T operator* (Type operand) const
 Performs component-based multiplication on two vectors. More...
 
Vector2T operator* (const MatrixT< Type > &operand) const
 
Vector2Toperator*= (Type operand)
 Multiplies the individual components of another vector onto this vector's components. More...
 
Vector2Toperator*= (const MatrixT< Type > &operand)
 
Vector2T operator/ (Type operand) const
 Performs component-based division on two vectors. More...
 
Vector2Toperator/= (Type operand)
 Divides the individual components of another vector onto this vector's components. More...
 
template<typename OtherType >
 operator Vector2T< OtherType > ()
 
Type & At (UInt32 index)
 Accesses an individual component in this vector. More...
 
Type At (UInt32 index) const
 Accesses an individual (constant) component in this vector. More...
 
void Set (Type value)
 Sets all components to a given value. More...
 
void Set (Type x, Type y)
 Sets all components to the given values. More...
 
void Clear ()
 Clears the vector to (0, 0).
 
Vector2T< Type > Swizzle (UInt32 x, UInt32 y) const
 Performs a two dimensional swizzle. More...
 
Vector3T< Type > Swizzle (UInt32 x, UInt32 y, UInt32 z) const
 Performs a three dimensional swizzle. More...
 
Vector4T< Type > Swizzle (UInt32 x, UInt32 y, UInt32 z, UInt32 w) const
 Performs a four dimensional swizzle. More...
 
void Normalize ()
 Normalizes the vector resulting in a length of 1. More...
 
Boolean IsNormalized (Type epsilon=0.00001f) const
 Checks if the vector is normalizes within a given epsilon. More...
 
Type GetMagnitude () const
 Gets the magnitude of the vector. More...
 
Type GetSquaredMagnitude () const
 Gets the squared magnitude of the vector. More...
 
Type GetManhattanMagnitude () const
 Gets the manhattan magnitude of the vector. More...
 
Type GetDistance (const Vector2T &other) const
 Gets the distance between two vectors. More...
 
Type GetSquaredDistance (const Vector2T &other) const
 Gets the squared distance between two vectors. More...
 
Type GetManhattanDistance (const Vector2T &other) const
 Gets the manhattan distance between two vectors. More...
 
void SetAngle (Type angle)
 Sets the two dimensional angle of the vector. More...
 
Type GetAngle () const
 Gets the two dimensional angle of the vector. More...
 
Vector2T GetPerpendicular () const
 Gets a vector perpendicular to this one. More...
 
void Floor ()
 Rounds down each component to the nearest whole number.
 
void Round ()
 Rounds each component to the nearest whole number.
 
void Ceil ()
 Rounds up each component to the nearest whole number.
 
Vector2T Floored () const
 Returns a vector with each component rounded down. More...
 
Vector2T Rounded () const
 Returns a vector with each component rounded. More...
 
Vector2T Ceiled () const
 Returns a vector with each component rounded up. More...
 
Type SumComponents () const
 Gets the sum of the elements. More...
 
Type MultiplyComponents () const
 Gets the product of the elements. More...
 
Type IsNaN () const
 

Static Public Member Functions

static Vector2T FromAngle (Type angle, Type magnitude=1.0f)
 Creates a vector based on a given two dimensional angle with an optional magnitude. More...
 
static Type DotProduct (const Vector2T &valueA, const Vector2T &valueB)
 
static Type CrossProduct (const Vector2T &valueA, const Vector2T &valueB)
 
static Vector2T Normalized (const Vector2T &value)
 
static Type Distance (const Vector2T &vectorA, const Vector2T &vectorB)
 
static Type DistanceSquared (const Vector2T &valueA, const Vector2T &vectorB)
 
static Type DistanceManhattan (const Vector2T &valueA, const Vector2T &vectorB)
 
static Vector2T Lerp (const Vector2T &from, const Vector2T &to, Type t)
 Performs linear interpolation on a vector2.
 

Public Attributes

Type x
 The x component.
 
Type y
 The y component.
 

Static Public Attributes

static const Vector2T zero
 Zero vector, defined as (0, 0).
 
static const Vector2T one
 One vector, defined as (1, 1).
 
static const Vector2T unitX
 Unit X vector, defined as (1, 0).
 
static const Vector2T unitY
 Unit Y vector, defined as (0, 1).
 
static const Vector2T nan
 NaN vector, defined as (NaN, NaN).
 

Friends

std::ostream & operator<< (std::ostream &stream, const Vector2T< Type > &vector)
 An operator to output this vector on an output stream.
 

Detailed Description

template<typename Type>
struct CGUL::Vector2T< Type >

A two dimensional 32 bit floating point vector.

Todo:
Template this to allow 64 bit floating point or integer as well?

Constructor & Destructor Documentation

template<typename Type >
CGUL::Vector2T< Type >::Vector2T ( const Vector2T< Type > &  copy)
inline

Copies a vector into this vector.

Parameters
copyThe vector to copy.
template<typename Type>
CGUL::Vector2T< Type >::Vector2T ( Type  value)
inline

Initializes all components to a given value.

Parameters
valueThe value for each component.
template<typename Type>
CGUL::Vector2T< Type >::Vector2T ( Type  x,
Type  y 
)
inline

Initializes the vector with individual component values.

Parameters
xThe x component.
yThe y component.

Member Function Documentation

template<typename Type >
Type & CGUL::Vector2T< Type >::At ( UInt32  index)
inline

Accesses an individual component in this vector.

Parameters
indexThe component index, so 0 for x, and 1 for y.
Returns
The value at that index.
template<typename Type >
Type CGUL::Vector2T< Type >::At ( UInt32  index) const
inline

Accesses an individual (constant) component in this vector.

Parameters
indexThe component index, so 0 for x, and 1 for y.
Returns
The value at that index.
template<typename Type >
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::Ceiled ( ) const
inline

Returns a vector with each component rounded up.

Returns
The resulting vector.
template<typename Type >
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::Floored ( ) const
inline

Returns a vector with each component rounded down.

Returns
The resulting vector.
template<typename Type>
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::FromAngle ( Type  angle,
Type  magnitude = 1.0f 
)
inlinestatic

Creates a vector based on a given two dimensional angle with an optional magnitude.

Defined as (cos(angle) * magnitude, sin(angle) * magnitude). If no magnitude is given it will default to one.

Parameters
angleThe angle in radians.
magnitudeAn optional magnitude.
Returns
The resulting vector.
template<typename Type >
Type CGUL::Vector2T< Type >::GetAngle ( ) const
inline

Gets the two dimensional angle of the vector.

Gets an angle from the vector.

Todo:
Check, does the vector have to be normalized for this to work?
template<typename Type >
Type CGUL::Vector2T< Type >::GetDistance ( const Vector2T< Type > &  other) const
inline

Gets the distance between two vectors.

The distance between two vectors is defined as the magnitude of one vector subtracted by another. In other words: GetMagnitude(Vec1 - Vec2). Because of this the same notes in GetMagnitude apply to this method.

Warning
This method uses sqrt() which can be slow!
Parameters
otherThe other vector.
Returns
The distance between the two vectors.
See Also
GetMagnitude
GetSquaredDistance
GetManhattanDistance
template<typename Type >
Type CGUL::Vector2T< Type >::GetMagnitude ( ) const
inline

Gets the magnitude of the vector.

Faster implementations of this method exist but are less accurate. For more information, see GetSquaredMagnitude and GetManhattanMagnitude.

Warning
This method uses sqrt() which can be slow!
Returns
The magnitude.
See Also
GetSquaredMagnitude
GetManhattanMagnitude
template<typename Type >
Type CGUL::Vector2T< Type >::GetManhattanDistance ( const Vector2T< Type > &  other) const
inline

Gets the manhattan distance between two vectors.

Parameters
otherThe other vector.
Returns
The manhattan distance between the two vectors.
See Also
GetDistance
Todo:
Check the implementation because this is actually slower than the squared distance.
template<typename Type >
Type CGUL::Vector2T< Type >::GetManhattanMagnitude ( ) const
inline

Gets the manhattan magnitude of the vector.

The Manhattan magnitude is even faster than the GetMagnitudeSquared alternative, but far less accurate. The squared magnitude cannot be used to determine if one magnitude is larger than another, nor can it be used to compare magnitudes. The purpose of the Manhattan magnitude is mostly used as a guess to speed up algorithms that may use it as a heuristic.

Note
The name Manhattan is a reference to the city Manhattan and how all the streets are laid out in blocks. To walk one block you would have to walk the length of the block, then the width. The result of this function is just that, the sum of the x and y components. A call to make the values absolute is also necessary to ensure an always positive result.
Returns
The manhattan magnitude.
template<typename Type >
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::GetPerpendicular ( ) const
inline

Gets a vector perpendicular to this one.

Returns
A vector perpendicular to this one.
template<typename Type >
Type CGUL::Vector2T< Type >::GetSquaredDistance ( const Vector2T< Type > &  other) const
inline

Gets the squared distance between two vectors.

Parameters
otherThe other vector.
Returns
The squared distance between the two vectors.
See Also
GetDistance
template<typename Type >
Type CGUL::Vector2T< Type >::GetSquaredMagnitude ( ) const
inline

Gets the squared magnitude of the vector.

The squared magnitude is a less accurate, but more quickly calculated alternative to the GetMagnitude method. A squared magnitude can be used to measure if one magnitude is larger than another, but cannot be used to accurately determine differences between magnitudes.

Returns
The squared magnitude.
template<typename Type >
Type CGUL::Vector2T< Type >::IsNaN ( ) const
inline
Returns
True if the vector contains NaN values, false otherwise.
template<typename Type>
CGUL::Boolean CGUL::Vector2T< Type >::IsNormalized ( Type  epsilon = 0.00001f) const
inline

Checks if the vector is normalizes within a given epsilon.

Warning
This method uses sqrt() which can be slow!
template<typename Type >
Type CGUL::Vector2T< Type >::MultiplyComponents ( ) const
inline

Gets the product of the elements.

Returns
x * y
template<typename Type >
void CGUL::Vector2T< Type >::Normalize ( )
inline

Normalizes the vector resulting in a length of 1.

Warning
This method uses sqrt() which can be slow!
template<typename Type >
bool CGUL::Vector2T< Type >::operator!= ( const Vector2T< Type > &  operand) const
inline

Checks if two vectors are not exactly equal.

Due to floating point imprecision, vectors are rarely ever exactly equal. Use with care.

Parameters
operandThe other vector.
Returns
True if they are not equal, false otherwise.
template<typename Type>
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::operator* ( Type  operand) const
inline

Performs component-based multiplication on two vectors.

Parameters
operandA scalar value.
Returns
The resulting vector.
template<typename Type>
CGUL::Vector2T< Type > & CGUL::Vector2T< Type >::operator*= ( Type  operand)
inline

Multiplies the individual components of another vector onto this vector's components.

Parameters
operandA scalar value.
Returns
A reference to the current object.
template<typename Type >
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::operator+ ( const Vector2T< Type > &  operand) const
inline

Performs component-based addition on two vectors.

Parameters
operandThe other vector.
Returns
The resulting vector.
template<typename Type >
CGUL::Vector2T< Type > & CGUL::Vector2T< Type >::operator+= ( const Vector2T< Type > &  operand)
inline

Adds the individual components of another vector to this vector's components.

Parameters
operandThe other operand.
Returns
A reference to the current object.
template<typename Type >
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::operator- ( ) const
inline

Gets the inverse of the vector.

The inverse is defined as (-x, -y) of the original vector.

Returns
The inverse vector.
template<typename Type >
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::operator- ( const Vector2T< Type > &  operand) const
inline

Performs component-based subtraction on two vectors.

Parameters
operandThe other vector.
Returns
The resulting vector.
template<typename Type >
CGUL::Vector2T< Type > & CGUL::Vector2T< Type >::operator-= ( const Vector2T< Type > &  operand)
inline

Subtracts the individual components of another vector from this vector's components.

Parameters
operandThe other operand.
Returns
A reference to the current object.
template<typename Type>
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::operator/ ( Type  operand) const
inline

Performs component-based division on two vectors.

Parameters
operandThe other vector.
Returns
The resulting vector.
template<typename Type>
CGUL::Vector2T< Type > & CGUL::Vector2T< Type >::operator/= ( Type  operand)
inline

Divides the individual components of another vector onto this vector's components.

Parameters
operandThe other operand.
Returns
A reference to the current object.
template<typename Type >
CGUL::Vector2T< Type > & CGUL::Vector2T< Type >::operator= ( const Vector2T< Type > &  operand)
inline

Assigns another vector into this vector.

Parameters
operandThe other vector.
Returns
A reference to the current object.
template<typename Type >
bool CGUL::Vector2T< Type >::operator== ( const Vector2T< Type > &  operand) const
inline

Checks if two vectors are exactly equal.

Due to floating point imprecision, vectors are rarely ever exactly equal. Use with care.

Parameters
operandThe other vector.
Returns
True if they are equal, false otherwise.
template<typename Type >
Type & CGUL::Vector2T< Type >::operator[] ( UInt32  index)
inline

Accesses an individual component in this vector.

Warning
This method does not bounds check, must be 0 or 1!
Parameters
indexThe component index (0 for x, 1 for y).
Returns
The value at that index.
template<typename Type >
Type CGUL::Vector2T< Type >::operator[] ( UInt32  index) const
inline

Accesses an individual (constant) component in this vector.

Unlike the other operator[], this one works on constant vectors and returns a value that cannot be modified to alter the original vector.

Warning
This method does not bounds check, must be 0 or 1!
Parameters
indexThe component index (0 for x, 1 for y).
Returns
The value at that index.
template<typename Type >
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::Rounded ( ) const
inline

Returns a vector with each component rounded.

Returns
The resulting vector.
template<typename Type>
void CGUL::Vector2T< Type >::Set ( Type  value)
inline

Sets all components to a given value.

Parameters
valueThe value to set all components to.
template<typename Type>
void CGUL::Vector2T< Type >::Set ( Type  x,
Type  y 
)
inline

Sets all components to the given values.

Parameters
xThe x component.
yThe y component.
template<typename Type>
void CGUL::Vector2T< Type >::SetAngle ( Type  angle)
inline

Sets the two dimensional angle of the vector.

Sets the vector's angle while preserving its magnitude. The new x is calculated based off the cosine function and the new y is based off the sine function. The existing magnitude is calculated via GetMagnitude and applied back to the vector after the x and y have been calculated.

Warning
This method uses sqrt() which can be slow!
Parameters
angleThe new angle (in radians) for the vector.
template<typename Type >
Type CGUL::Vector2T< Type >::SumComponents ( ) const
inline

Gets the sum of the elements.

Returns
x + y
template<typename Type >
CGUL::Vector2T< Type > CGUL::Vector2T< Type >::Swizzle ( UInt32  x,
UInt32  y 
) const
inline

Performs a two dimensional swizzle.

Swizzles the vector allowing the components to be re-arranged into a new vector.

Parameters
xThe x component (0 for x, 1 for y).
yThe y component (0 for x, 1 for y).
Returns
The resulting vector.
Todo:
Better documentation on swizzle (for now just look it up!)
template<typename Type >
CGUL::Vector3T< Type > CGUL::Vector2T< Type >::Swizzle ( UInt32  x,
UInt32  y,
UInt32  z 
) const
inline

Performs a three dimensional swizzle.

Swizzles the vector allowing the components to be re-arranged into a new vector.

Parameters
xThe x component (0 for x, 1 for y).
yThe y component (0 for x, 1 for y).
zThe z component (0 for x, 1 for y).
Returns
The resulting vector.
template<typename Type >
CGUL::Vector4T< Type > CGUL::Vector2T< Type >::Swizzle ( UInt32  x,
UInt32  y,
UInt32  z,
UInt32  w 
) const
inline

Performs a four dimensional swizzle.

Swizzles the vector allowing the components to be re-arranged into a new vector.

Parameters
xThe x component (0 for x, 1 for y).
yThe y component (0 for x, 1 for y).
zThe z component (0 for x, 1 for y).
wThe w component (0 for x, 1 for y).
Returns
The resulting vector.

The documentation for this struct was generated from the following files: