Angel 3.2
A 2D Game Prototyping Engine
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Vector2 Struct Reference

A two-dimensional floating point vector and associated math functions. More...

#include <Vector2.h>

Public Member Functions

 Vector2 (float x, float y)
 
 Vector2 (float value)
 
 Vector2 (const Vec2i &copy)
 
 Vector2 ()
 
float Length ()
 
float LengthSquared ()
 
void Normalize ()
 
bool operator== (const Vector2 &v) const
 
bool operator!= (const Vector2 &v) const
 
Vector2 operator- () const
 
Vector2 operator- (const Vector2 &v) const
 
Vector2 operator+ (const Vector2 &v) const
 
Vector2 operator/ (float divider) const
 
Vector2 operator* (float scaleFactor) const
 
Vector2operator+= (const Vector2 &v)
 
Vector2operator-= (const Vector2 &v)
 
Vector2operator*= (float f)
 
Vector2operator/= (float f)
 

Static Public Member Functions

static float Distance (const Vector2 &value1, const Vector2 &value2)
 
static float DistanceSquared (const Vector2 &value1, const Vector2 &value2)
 
static float Dot (const Vector2 &value1, const Vector2 &value2)
 
static float Cross (const Vector2 &value1, const Vector2 &value2)
 
static Vector2 Normalize (const Vector2 &value)
 
static Vector2 Reflect (const Vector2 &vector, const Vector2 &normal)
 
static Vector2 Min (const Vector2 &value1, const Vector2 &value2)
 
static Vector2 Max (const Vector2 &value1, const Vector2 &value2)
 
static Vector2 Clamp (const Vector2 &value, const Vector2 &min, const Vector2 &max)
 
static Vector2 Lerp (const Vector2 &value1, const Vector2 &value2, float amount)
 
static Vector2 Negate (const Vector2 &value)
 
static Vector2 Rotate (const Vector2 &value, const float radians)
 

Public Attributes

float X
 
float Y
 

Static Public Attributes

static Vector2 Zero
 
static Vector2 One
 
static Vector2 UnitX
 
static Vector2 UnitY
 

Detailed Description

A floating point two-dimensional vector. Can be used either as a traditional vector (indicating a direction) or a position (treating X and Y as coordinates).

Definition at line 41 of file Vector2.h.

Constructor & Destructor Documentation

Vector2::Vector2 ( float  x,
float  y 
)

Constructor to initialize the vector to set dimensions

Parameters
xThe X dimension
yThe Y dimension

Definition at line 42 of file Vector2.cpp.

Vector2::Vector2 ( float  value)

Constructor to initialize the vector to uniform dimensions

Parameters
valueThe value to use for both the X and Y dimension

Definition at line 47 of file Vector2.cpp.

Vector2::Vector2 ( const Vec2i copy)

Constructor to initalize from a Vec2i struct.

Parameters
copyThe Vec2i to be converted into a Vector2.

Definition at line 57 of file Vector2.cpp.

Vector2::Vector2 ( )

Constructor to initialize a zero-length vector (0, 0)

Definition at line 52 of file Vector2.cpp.

Member Function Documentation

float Vector2::Length ( )

Get the absolute magnitude of the vector. Uses a square-root, so be careful calling this too much within a loop.

Returns
The length (magnitude) of the vector

Definition at line 62 of file Vector2.cpp.

float Vector2::LengthSquared ( )

Get the squared magnitude of the vector – if all you care about is comparison, it's a lot faster to consider the squared lengths of the vectors.

Returns
The length (magnitude) of the vector squared

Definition at line 67 of file Vector2.cpp.

float Vector2::Distance ( const Vector2 value1,
const Vector2 value2 
)
static

Get the absolute distance between two points (most useful if the Vector2 represents a position). Uses a square-root, so be careful calling this too much within a loop.

Parameters
value1The first point
value2The second point
Returns
The distance between the two points

Definition at line 72 of file Vector2.cpp.

float Vector2::DistanceSquared ( const Vector2 value1,
const Vector2 value2 
)
static

Get the absolute distance between two points – if all you care about is comparison, it's a lot faster to consider the squared lengths of the vectors.

Parameters
value1The first point
value2The second point
Returns
The distance between the two points squared

Definition at line 77 of file Vector2.cpp.

float Vector2::Dot ( const Vector2 value1,
const Vector2 value2 
)
static

Get the dot product of two vectors.

Parameters
value1The first vector
value2The second vector
Returns
The dot product

Definition at line 82 of file Vector2.cpp.

float Vector2::Cross ( const Vector2 value1,
const Vector2 value2 
)
static

Get the cross product of two vectors. Note that the mathematical definition of a cross product results in another vector perpendicular to the two inputs, but since both of our vectors are 2D, the returned vector will always have X and Y components of 0. Thus this function returns what would be the Z component of that vector.

Parameters
value1The first vector
value2The second vector
Returns
The Z component of the cross product

Definition at line 87 of file Vector2.cpp.

void Vector2::Normalize ( )

Normalizes a vector in place – retains its direction, but ensures that its magnitude is equal to 1.0.

Definition at line 92 of file Vector2.cpp.

Vector2 Vector2::Normalize ( const Vector2 value)
static

Get the normalized value for a Vector2 without affecting the original.

Parameters
valueThe Vector2 to normalize
Returns
A normalized version of the passed-in Vector2

Definition at line 110 of file Vector2.cpp.

Vector2 Vector2::Reflect ( const Vector2 vector,
const Vector2 normal 
)
static

Reflect one Vector around another

Parameters
vectorThe vector to reflect
normalThe normal to reflect it around
Returns
The new vector resulting from the reflection

Definition at line 117 of file Vector2.cpp.

Vector2 Vector2::Min ( const Vector2 value1,
const Vector2 value2 
)
static

Get a new vector from the minimum X and minimum Y of the two

Returns
The vector composed from minimums on both axes

Definition at line 122 of file Vector2.cpp.

Vector2 Vector2::Max ( const Vector2 value1,
const Vector2 value2 
)
static

Get a new vector from the maximum X and maximum Y of the two

Returns
The vector composed from the maximums on both axes

Definition at line 127 of file Vector2.cpp.

Vector2 Vector2::Clamp ( const Vector2 value,
const Vector2 min,
const Vector2 max 
)
static

Clamp a vector to a given minimum and maximum

Parameters
valueThe vector to be clamped
minThe vector representing the X and Y minimums for the clamping
maxThe vector representing the X and Y maximums for the clamping
Returns
The clamped vector

Definition at line 132 of file Vector2.cpp.

Vector2 Vector2::Lerp ( const Vector2 value1,
const Vector2 value2,
float  amount 
)
static

Perform a linear interpolation between two vectors

Parameters
value1The starting point vector
value2The ending point vector
amountThe amount (from 0.0 to 1.0) to interpolate between them
Returns
The interpolated vector

Definition at line 137 of file Vector2.cpp.

Vector2 Vector2::Negate ( const Vector2 value)
static

Get a negated vector – if you add the result and the original, you should get a zero-length vector (0, 0)

Parameters
valueThe vector to negate
Returns
The negated vector

Definition at line 142 of file Vector2.cpp.

Vector2 Vector2::Rotate ( const Vector2 value,
const float  radians 
)
static

Rotate a vector

Parameters
valueThe original vector to rotate
radiansThe rotation angle (in radians)
Returns
The rotated vector

Definition at line 147 of file Vector2.cpp.

Member Data Documentation

float Vector2::X

The X dimension, publicly available because it's so often gotten or set and there's no good reason to encapsulate it.

Definition at line 47 of file Vector2.h.

float Vector2::Y

The Y dimension, also publicly available.

Definition at line 52 of file Vector2.h.

Vector2 Vector2::Zero
static

A reference to a zero-length vector (0, 0)

NB: We can't make a static member variable constant, so it is possible to change this value. That is a horrible idea and will make the engine act "odd" at best.

Definition at line 61 of file Vector2.h.

Vector2 Vector2::One
static

A reference to a (1, 1) vector

NB: We can't make a static member variable constant, so it is possible to change this value. That is a horrible idea and will make the engine act "odd" at best.

Definition at line 70 of file Vector2.h.

Vector2 Vector2::UnitX
static

A reference to a (1, 0) vector

NB: We can't make a static member variable constant, so it is possible to change this value. That is a horrible idea and will make the engine act "odd" at best.

Definition at line 79 of file Vector2.h.

Vector2 Vector2::UnitY
static

A reference to a (0, 1) vector

NB: We can't make a static member variable constant, so it is possible to change this value. That is a horrible idea and will make the engine act "odd" at best.

Definition at line 88 of file Vector2.h.


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