CUGL
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
cugl::Vec2 Class Reference

#include <CUVec2.h>

Public Member Functions

 Vec2 ()
 
 Vec2 (float x, float y)
 
 Vec2 (const float *array)
 
 Vec2 (const Vec2 &p1, const Vec2 &p2)
 
 Vec2 (const Vec2 &copy)
 
 ~Vec2 ()
 
Vec2operator= (const Vec2 &v)
 
Vec2operator= (const float *array)
 
Vec2set (float x, float y)
 
Vec2set (const float *array)
 
Vec2set (const Vec2 &v)
 
Vec2set (const Vec2 &p1, const Vec2 &p2)
 
Vec2setZero ()
 
Vec2clamp (const Vec2 &min, const Vec2 &max)
 
Vec2 getClamp (const Vec2 &min, const Vec2 &max) const
 
Vec2add (const Vec2 &v)
 
Vec2add (float x, float y)
 
Vec2subtract (const Vec2 &v)
 
Vec2subtract (float x, float y)
 
Vec2scale (float s)
 
Vec2scale (float sx, float sy)
 
Vec2scale (const Vec2 &v)
 
Vec2divide (float s)
 
Vec2divide (float sx, float sy)
 
Vec2divide (const Vec2 &v)
 
Vec2negate ()
 
Vec2reciprocate ()
 
Vec2 getNegation () const
 
Vec2 getReciprocal () const
 
Vec2map (std::function< float(float)> func)
 
Vec2 getMap (std::function< float(float)> func) const
 
Vec2operator+= (const Vec2 &v)
 
Vec2operator-= (const Vec2 &v)
 
Vec2operator*= (float s)
 
Vec2operator*= (const Vec2 &v)
 
Vec2operator/= (float s)
 
Vec2operator/= (const Vec2 &v)
 
const Vec2 operator+ (const Vec2 &v) const
 
const Vec2 operator- (const Vec2 &v) const
 
const Vec2 operator- () const
 
const Vec2 operator* (float s) const
 
const Vec2 operator* (const Vec2 &v) const
 
const Vec2 operator/ (float s) const
 
const Vec2 operator/ (const Vec2 &v) const
 
bool operator< (const Vec2 &v) const
 
bool operator<= (const Vec2 &v) const
 
bool operator> (const Vec2 &v) const
 
bool operator>= (const Vec2 &v) const
 
bool operator== (const Vec2 &v) const
 
bool operator!= (const Vec2 &v) const
 
bool under (const Vec2 &v) const
 
bool over (const Vec2 &v) const
 
bool equals (const Vec2 &v, float variance=CU_MATH_EPSILON) const
 
float getAngle () const
 
float getAngle (const Vec2 &other) const
 
bool isZero () const
 
bool isNearZero (float variance=CU_MATH_EPSILON) const
 
bool isOne () const
 
bool isInvertible () const
 
bool isUnit (float variance=CU_MATH_EPSILON) const
 
float distance (const Vec2 &v) const
 
float distanceSquared (const Vec2 &v) const
 
float length () const
 
float lengthSquared () const
 
float dot (const Vec2 &v) const
 
float cross (const Vec2 &other) const
 
Vec2normalize ()
 
Vec2 getNormalization () const
 
Vec2rotate (float angle)
 
Vec2rotate (float angle, const Vec2 &point)
 
Vec2rotate (const Vec2 &other)
 
Vec2unrotate (const Vec2 &other)
 
Vec2 getRotation (float angle)
 
Vec2 getRotation (float angle, const Vec2 &point)
 
Vec2 getRotation (const Vec2 &other)
 
Vec2 getUnrotation (const Vec2 &other)
 
Vec2perp ()
 
Vec2rperp ()
 
Vec2 getPerp () const
 
Vec2 getRPerp () const
 
Vec2 getMidpoint (const Vec2 &other) const
 
Vec2project (const Vec2 &other)
 
Vec2 getProjection (const Vec2 &other) const
 
Vec2lerp (const Vec2 &other, float alpha)
 
Vec2 getLerp (const Vec2 &other, float alpha)
 
std::string toString (bool verbose=false) const
 
 operator std::string () const
 
 operator Size () const
 
 Vec2 (const Size &size)
 
Vec2operator= (const Size &size)
 
Vec2operator+= (const Size &right)
 
Vec2operator-= (const Size &right)
 
const Vec2 operator+ (const Size &right) const
 
const Vec2 operator- (const Size &right) const
 
 operator Vec3 () const
 
 Vec2 (const Vec3 &v)
 
Vec2operator= (const Vec3 &size)
 
 operator Vec4 () const
 
 Vec2 (const Vec4 &v)
 
Vec2operator= (const Vec4 &size)
 

Static Public Member Functions

static Vec2 forAngle (const float a)
 
static Vec2clamp (const Vec2 &v, const Vec2 &min, const Vec2 &max, Vec2 *dst)
 
static float angle (const Vec2 &v1, const Vec2 &v2)
 
static Vec2add (const Vec2 &v1, const Vec2 &v2, Vec2 *dst)
 
static Vec2subtract (const Vec2 &v1, const Vec2 &v2, Vec2 *dst)
 
static Vec2scale (const Vec2 &v, float s, Vec2 *dst)
 
static Vec2scale (const Vec2 &v1, const Vec2 &v2, Vec2 *dst)
 
static Vec2divide (const Vec2 &v, float s, Vec2 *dst)
 
static Vec2divide (const Vec2 &v1, const Vec2 &v2, Vec2 *dst)
 
static Vec2reciprocate (const Vec2 &v, Vec2 *dst)
 
static Vec2negate (const Vec2 &v, Vec2 *dst)
 
static float dot (const Vec2 &v1, const Vec2 &v2)
 
static float cross (const Vec2 &v1, const Vec2 &v2)
 
static Vec2normalize (const Vec2 &v, Vec2 *dst)
 
static Vec2midpoint (const Vec2 &v1, const Vec2 &v2, Vec2 *dst)
 
static Vec2project (const Vec2 &v1, const Vec2 &v2, Vec2 *dst)
 
static Vec2lerp (const Vec2 &v1, const Vec2 &v2, float alpha, Vec2 *dst)
 
static bool doesLineIntersect (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D, float *S=nullptr, float *T=nullptr)
 
static bool doesLineOverlap (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D)
 
static bool isLineParallel (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D)
 
static bool doesSegmentIntersect (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D)
 
static bool doesSegmentOverlap (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D, Vec2 *S=nullptr, Vec2 *E=nullptr)
 
static Vec2 getIntersection (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D)
 

Public Attributes

float x
 
float y
 

Static Public Attributes

static const Vec2 ZERO
 
static const Vec2 ONE
 
static const Vec2 UNIT_X
 
static const Vec2 UNIT_Y
 
static const Vec2 ANCHOR_MIDDLE
 
static const Vec2 ANCHOR_BOTTOM_LEFT
 
static const Vec2 ANCHOR_TOP_LEFT
 
static const Vec2 ANCHOR_BOTTOM_RIGHT
 
static const Vec2 ANCHOR_TOP_RIGHT
 
static const Vec2 ANCHOR_MIDDLE_RIGHT
 
static const Vec2 ANCHOR_MIDDLE_LEFT
 
static const Vec2 ANCHOR_MIDDLE_TOP
 
static const Vec2 ANCHOR_MIDDLE_BOTTOM
 

Detailed Description

This class defines a 2-element floating point vector.

This class may be used to represent either a normal, a direction or a point interchangeably without casting. In addition, instances of this class may be freely cast to Size and vectors of other sizes.

This class is in standard layout with fields of uniform type. This means that it is safe to reinterpret_cast objects to float arrays.

Constructor & Destructor Documentation

cugl::Vec2::Vec2 ( )
inline

Constructs a new vector initialized to all zeros.

cugl::Vec2::Vec2 ( float  x,
float  y 
)
inline

Constructs a new vector initialized to the specified values.

Parameters
xThe x coordinate.
yThe y coordinate.
cugl::Vec2::Vec2 ( const float *  array)
inline

Constructs a new vector from the values in the specified array.

Parameters
arrayAn array containing the elements of the vector in the order x, y.
cugl::Vec2::Vec2 ( const Vec2 p1,
const Vec2 p2 
)
inline

Constructs a vector that describes the direction between the specified points.

Parameters
p1The first point.
p2The second point.
cugl::Vec2::Vec2 ( const Vec2 copy)
inline

Constructs a new vector that is a copy of the specified vector.

Parameters
copyThe vector to copy.
cugl::Vec2::~Vec2 ( )
inline

Destroys this vector, releasing all resources

cugl::Vec2::Vec2 ( const Size size)
explicit

Creates a vector from the given size.

The width is converted to the x coordinate and height to y.

Parameters
sizeThe size to convert
cugl::Vec2::Vec2 ( const Vec3 v)
explicit

Creates a 2d vector from the given 3d one.

The z-value is dropped.

Parameters
vThe vector to convert
cugl::Vec2::Vec2 ( const Vec4 v)
explicit

Creates a 2d vector from the given 4d one.

All coordinates are divided by the w-coordinate (assuming it is not zero) before this conversion. Afterwards, both z and w are dropped.

Parameters
vThe vector to convert

Member Function Documentation

static Vec2* cugl::Vec2::add ( const Vec2 v1,
const Vec2 v2,
Vec2 dst 
)
static

Adds the specified vectors and stores the result in dst.

Parameters
v1The first vector.
v2The second vector.
dstA vector to store the result in
Returns
A reference to dst for chaining
Vec2& cugl::Vec2::add ( const Vec2 v)
inline

Adds the given vector to this one in place.

Parameters
vThe vector to add
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::add ( float  x,
float  y 
)
inline

Adds the given values to this vector.

Parameters
xThe x coordinate to add.
yThe y coordinate to add.
Returns
A reference to this (modified) Vec2 for chaining.
static float cugl::Vec2::angle ( const Vec2 v1,
const Vec2 v2 
)
static

Returns the angle (in radians) between the specified vectors.

Parameters
v1The first vector.
v2The second vector.
Returns
The angle between the two vectors (in radians).
static Vec2* cugl::Vec2::clamp ( const Vec2 v,
const Vec2 min,
const Vec2 max,
Vec2 dst 
)
static

Clamps the specified vector within the given range and returns it in dst.

Parameters
vThe vector to clamp.
minThe minimum value.
maxThe maximum value.
dstA vector to store the result in.
Returns
A reference to dst for chaining
Vec2& cugl::Vec2::clamp ( const Vec2 min,
const Vec2 max 
)
inline

Clamps this vector within the given range.

Parameters
minThe minimum value.
maxThe maximum value.
Returns
A reference to this (modified) Vec2 for chaining.
float cugl::Vec2::cross ( const Vec2 other) const
inline

Returns the cross product of this vector with another

The cross-product of any two vectors in the plane is perpendicular to the plane. This method returns the magnitude of that z-vector.

Parameters
otherThe vector to cross with
Returns
the cross product of this vector with another
static float cugl::Vec2::cross ( const Vec2 v1,
const Vec2 v2 
)
static

Returns the cross product of the specified vectors.

The cross-product of any two vectors in the plane is perpendicular to the plane. This method returns the magnitude of that z-vector.

Parameters
v1The first vector.
v2The second vector.
Returns
the cross product between the vectors.
float cugl::Vec2::distance ( const Vec2 v) const
inline

Returns the distance between this vector and v.

Parameters
vThe other vector.
Returns
The distance between this vector and v.

{

See also
distanceSquared}
float cugl::Vec2::distanceSquared ( const Vec2 v) const
inline

Returns the squared distance between this vector and v.

This method is faster than distance because it does not need to compute a square root. Hence it is best to us this method when it is not necessary to get the exact distance between two vectors (e.g. when simply comparing the distance between different vectors).

Parameters
vThe other vector.
Returns
The squared distance between this vector and v.

{

See also
distance}
static Vec2* cugl::Vec2::divide ( const Vec2 v,
float  s,
Vec2 dst 
)
static

Divides the specified vector and stores the result in dst.

The division is uniform by the constant s.

Parameters
vThe vector to divide.
sThe uniform division factor.
dstThe destination vector.
Returns
A reference to dst for chaining
static Vec2* cugl::Vec2::divide ( const Vec2 v1,
const Vec2 v2,
Vec2 dst 
)
static

Divides the specified vector and stores the result in dst.

The division is nonuniform by the attributes in v2.

Parameters
v1The vector to divide.
v2The nonuniform division factor.
dstThe destination vector.
Returns
A reference to dst for chaining
Vec2& cugl::Vec2::divide ( float  s)

Divides this vector in place by the given factor.

Parameters
sThe scalar to divide by
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::divide ( float  sx,
float  sy 
)

Divides this vector nonuniformly by the given factors.

Parameters
sxThe scalar to divide the x-axis
syThe scalar to divide the y-axis
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::divide ( const Vec2 v)

Divides this vector in place by the given vector.

This method is provided to support non-uniform scaling.

Parameters
vThe vector to divide by
Returns
A reference to this (modified) Vec2 for chaining.
static bool cugl::Vec2::doesLineIntersect ( const Vec2 A,
const Vec2 B,
const Vec2 C,
const Vec2 D,
float *  S = nullptr,
float *  T = nullptr 
)
static

Returns true if lines AB and CD intersect

The line segment parameters of the hit-points are stored in S and T. These values are left unchanged if there is no intersection.

The hit point is given by C + T * (D - C) [ or alternatively by A + S * (B - A)]

The return value for this function only tests for lines. To test intersection for segments, you must verify that both S & T lie in [0..1] . For rays, we have to make sure S & T > 0.

This method returns false if the lines overlap.

Parameters
Athe startpoint for the first line L1 = (A - B)
Bthe endpoint for the first line L1 = (A - B)
Cthe startpoint for the second line L2 = (C - D)
Dthe endpoint for the second line L2 = (C - D)
Sthe range for a hitpoint in L1 (p = A + S*(B - A))
Tthe range for a hitpoint in L2 (p = C + T*(D - C))
Returns
true if lines AB and CD intersect
static bool cugl::Vec2::doesLineOverlap ( const Vec2 A,
const Vec2 B,
const Vec2 C,
const Vec2 D 
)
static

Returns true if line AB overlaps segment CD.

This result means that AB and CD are both parallel and are on top of each other. AB and CD are treated as lines for this function.

Parameters
Athe startpoint for the first line L1 = (A - B)
Bthe endpoint for the first line L1 = (A - B)
Cthe startpoint for the second line L2 = (C - D)
Dthe endpoint for the second line L2 = (C - D)
Returns
true if line AB overlaps segment CD.
static bool cugl::Vec2::doesSegmentIntersect ( const Vec2 A,
const Vec2 B,
const Vec2 C,
const Vec2 D 
)
static

Returns true if segment AB intersects with segment CD

This method returns false if the segments overlap.

Parameters
Athe startpoint for the first line L1 = (A - B)
Bthe endpoint for the first line L1 = (A - B)
Cthe startpoint for the second line L2 = (C - D)
Dthe endpoint for the second line L2 = (C - D)
Returns
true if segment AB intersects with segment CD
static bool cugl::Vec2::doesSegmentOverlap ( const Vec2 A,
const Vec2 B,
const Vec2 C,
const Vec2 D,
Vec2 S = nullptr,
Vec2 E = nullptr 
)
static

Returns true if Segment AB overlaps with segment CD

This result means that AB and CD are both parallel and are on top of each other. AB and CD are treated as segments for this function.

The bounds of the overlap are stored in S and E. If there is no overlap, these values may or may not be modified.

Parameters
Athe startpoint for the first line L1 = (A - B)
Bthe endpoint for the first line L1 = (A - B)
Cthe startpoint for the second line L2 = (C - D)
Dthe endpoint for the second line L2 = (C - D)
Sthe initial overlap position
Ethe terminal overlap position
float cugl::Vec2::dot ( const Vec2 v) const
inline

Returns the dot product of this vector and the specified vector.

Parameters
vThe vector to compute the dot product with.
Returns
The dot product.
static float cugl::Vec2::dot ( const Vec2 v1,
const Vec2 v2 
)
static

Returns the dot product between the specified vectors.

Parameters
v1The first vector.
v2The second vector.
Returns
The dot product between the vectors.
bool cugl::Vec2::equals ( const Vec2 v,
float  variance = CU_MATH_EPSILON 
) const
inline

Returns true if the vectors are within tolerance of each other.

The tolerance bounds the traditional Euclidean difference between the two vectors (treated as points)

Parameters
vThe vector to compare against.
varianceThe comparison tolerance.
Returns
true if the vectors are within tolerance of each other.
static Vec2 cugl::Vec2::forAngle ( const float  a)
inlinestatic

Returns the unit vector for the given angle (in radians)

Parameters
aThe defining angle in radians.
Returns
the unit vector for the given angle (in radians)
float cugl::Vec2::getAngle ( ) const
inline

Returns the angle in radians between this vector and the x axis.

If the vector is zero, the result is undefined.

Returns
the angle in radians between this vector and the x axis.
float cugl::Vec2::getAngle ( const Vec2 other) const

Returns the angle between this vector and other.

The angle is measured starting at this one. If either vector is zero, the result is undefined.

Parameters
otherThe vector to sweep towards.
Returns
the angle between this vector and other.
Vec2 cugl::Vec2::getClamp ( const Vec2 min,
const Vec2 max 
) const
inline

Returns a copy of this vector clamped within the given range.

Note: this does not modify this vector.

Parameters
minThe minimum value.
maxThe maximum value.
Returns
A copy of this vector clamped within the given range.
static Vec2 cugl::Vec2::getIntersection ( const Vec2 A,
const Vec2 B,
const Vec2 C,
const Vec2 D 
)
static

Returns the intersection point of lines AB and CD.

This function treats AB and CD as lines, not segments. To get finer control over the intersection point, use doesLineIntersect.

Parameters
Athe startpoint for the first line L1 = (A - B)
Bthe endpoint for the first line L1 = (A - B)
Cthe startpoint for the second line L2 = (C - D)
Dthe endpoint for the second line L2 = (C - D)
Returns
the intersection point of lines AB and CD.
Vec2 cugl::Vec2::getLerp ( const Vec2 other,
float  alpha 
)
inline

Returns the linear interpolation of this vector with other.

If alpha is 0, the vector is unchanged. If alpha is 1, the vector is other. Otherwise it is a value on the line ab. This method supports alpha outside of the range 0..1.

Parameters
otherThe other end of the line segment.
alphaThe interpolation value
Returns
The linear interpolation of this vector with other.
Vec2 cugl::Vec2::getMap ( std::function< float(float)>  func) const
inline

Returns a copy of this vector with func applied to each component.

This method supports any function that has the signature float func(float); This includes many of the functions in math.h.

Parameters
funcThe function to map on the coordinates.
Returns
A copy of this vector with func applied to each component.
Vec2 cugl::Vec2::getMidpoint ( const Vec2 other) const
inline

Returns the midpoint between this point and another.

Note: this does not modify this vector.

Parameters
otherThe other end of the line segment.
Returns
the midpoint between this point and another.
Vec2 cugl::Vec2::getNegation ( ) const
inline

Returns a negated copy of this vector.

Note: This method does not modify the vector

Returns
a negated copy of this vector.
Vec2 cugl::Vec2::getNormalization ( ) const
inline

Returns a normalized copy of this vector.

This method creates a copy of this vector that is of unit length (i.e. the length of the vector after calling this method will be 1.0f). If the vector already has unit length or if the length of the vector is zero, this method does nothing. Note: this does not modify this vector.

Returns
A normalized copy of this vector.
Vec2 cugl::Vec2::getPerp ( ) const
inline

Returns a perpendicular of v, rotated 90 degrees counter-clockwise.

The result guarantees cross(v, perp(v)) >= 0

Note: this does not modify this vector.

Returns
a perpendicular of v, rotated 90 degrees counter-clockwise.
Vec2 cugl::Vec2::getProjection ( const Vec2 other) const
inline

Returns the projection of this vector on to the other one.

Note: this does not modify this vector.

Parameters
otherThe vector to project on.
Returns
the projection of this vector on to the other one.
Vec2 cugl::Vec2::getReciprocal ( ) const
inline

Returns a reciprocated copy of this vector.

The reciprocal is computed for each element invidiually. This method does not check that all elements are non-zero. If any element is zero, the result will be system-dependent.

Note: This method does not modify the vector

Returns
a reciprocated copy of this vector.
Vec2 cugl::Vec2::getRotation ( float  angle)

Returns a copy of this vector rotated by the angle around the origin.

The angle is measured in radians. Note: this does not modify this vector.

Parameters
angleThe angle to rotate by (in radians).
Returns
a copy of this vector rotated by the angle around the origin.
Vec2 cugl::Vec2::getRotation ( float  angle,
const Vec2 point 
)

Returns a copy of this vector rotated by the angle around the given point.

The angle is measured in radians. Note: this does not modify this vector.

Parameters
pointThe point to rotate around.
angleThe angle to rotate by (in radians).
Returns
a copy of this vector rotated by the angle around the given point.
Vec2 cugl::Vec2::getRotation ( const Vec2 other)
inline

Returns a rotated copy of this vector using the angle of the other.

This rotation uses complex multiplication to add the angles of the two vectors together. This method is faster than rotating by an angle.

Note: this does not modify this vector.

Parameters
otherThe vector representing the angle to rotate by
Returns
a rotated copy of this vector using the angle of the other.
Vec2 cugl::Vec2::getRPerp ( ) const
inline

Returns a perpendicular of v, rotated 90 degrees clockwise.

The result guarantees cross(v, perp(v)) <= 0

Note: this does not modify this vector.

Returns
a perpendicular of v, rotated 90 degrees clockwise.
Vec2 cugl::Vec2::getUnrotation ( const Vec2 other)
inline

Returns an unrotated copy of this vector using the angle of the other.

This rotation uses complex multiplication to add the angles of the two vectors together. This method is faster than rotating by an angle.

Note: this does not modify this vector.

Parameters
otherThe vector representing the angle to unrotate by
Returns
an unrotated copy of this vector using the angle of the other.
bool cugl::Vec2::isInvertible ( ) const
inline

Returns true if this vector contains no zeroes.

Returns
true if this vector contains no zeroes, false otherwise.
static bool cugl::Vec2::isLineParallel ( const Vec2 A,
const Vec2 B,
const Vec2 C,
const Vec2 D 
)
static

Returns true if line AB is non-trivially parallel with segment CD.

This result means that AB and CD are parallel and are NOT overlappping. AB and CD are treated as lines for this function.

Parameters
Athe startpoint for the first line L1 = (A - B)
Bthe endpoint for the first line L1 = (A - B)
Cthe startpoint for the second line L2 = (C - D)
Dthe endpoint for the second line L2 = (C - D)
Returns
true if line AB is non-trivially parallel with segment CD.
bool cugl::Vec2::isNearZero ( float  variance = CU_MATH_EPSILON) const
inline

Returns true if this vector is with tolerance of the origin.

Parameters
varianceThe comparison tolerance
Returns
true if this vector is with tolerance of the origin.
bool cugl::Vec2::isOne ( ) const
inline

Returns true if this vector contains all ones.

Returns
true if this vector contains all ones, false otherwise.
bool cugl::Vec2::isUnit ( float  variance = CU_MATH_EPSILON) const
inline

Returns true if this vector is a unit vector.

Parameters
varianceThe comparison tolerance
Returns
true if this vector is a unit vector.
bool cugl::Vec2::isZero ( ) const
inline

Returns true this vector contains all zeros.

Returns
true if this vector contains all zeros, false otherwise.
float cugl::Vec2::length ( ) const
inline

Returns the length of this vector.

Returns
The length of the vector.

{

See also
lengthSquared}
float cugl::Vec2::lengthSquared ( ) const
inline

Returns the squared length of this vector.

This method is faster than length because it does not need to compute a square root. Hence it is best to us this method when it is not necessary to get the exact length of a vectors (e.g. when simply comparing the length to a threshold value).

Returns
The squared length of the vector.

{

See also
length}
Vec2& cugl::Vec2::lerp ( const Vec2 other,
float  alpha 
)
inline

Modifies this vector to be the linear interpolation with other.

If alpha is 0, the vector is unchanged. If alpha is 1, the vector is other. Otherwise it is a value on the line ab. This method supports alpha outside of the range 0..1.

Parameters
otherThe other end of the line segment.
alphaThe interpolation value
Returns
This vector, after the interpolation.
static Vec2* cugl::Vec2::lerp ( const Vec2 v1,
const Vec2 v2,
float  alpha,
Vec2 dst 
)
static

Computes the linear interpolation of two vectors and stores it in dst.

If alpha is 0, the vector is a copy of v1. If alpha is 1, the vector is a copy of v2. Otherwise it is a value on the line v1v2. This method supports alpha outside of the range 0..1.

Parameters
v1The first point.
v2The second point.
alphaThe interpolation value.
dstThe destination vector.
Returns
The linear interpolation of this vector with other.
Vec2& cugl::Vec2::map ( std::function< float(float)>  func)
inline

Maps the given function to the vector coordinates in place.

This method supports any function that has the signature float func(float); This includes many of the functions in math.h.

Parameters
funcThe function to map on the coordinates.
Returns
A reference to this (modified) Vec2 for chaining.
static Vec2* cugl::Vec2::midpoint ( const Vec2 v1,
const Vec2 v2,
Vec2 dst 
)
static

Computes the midpoint between two points and stores it in dst.

Parameters
v1The first point.
v2The second point.
dstThe destination vector.
Returns
A reference to dst for chaining
static Vec2* cugl::Vec2::negate ( const Vec2 v,
Vec2 dst 
)
static

Negates the specified vector and stores the result in dst.

Parameters
vThe vector to negate.
dstThe destination vector.
Returns
A reference to dst for chaining
Vec2& cugl::Vec2::negate ( )
inline

Negates this vector.

Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::normalize ( )

Normalizes this vector.

This method normalizes the vector so that it is of unit length (i.e. the length of the vector after calling this method will be 1.0f). If the vector already has unit length or if the length of the vector is zero, this method does nothing.

Returns
This vector, after the normalization occurs.
static Vec2* cugl::Vec2::normalize ( const Vec2 v,
Vec2 dst 
)
static

Normalizes the specified vector and stores the result in dst.

If the quaternion already has unit length or if the length of the quaternion is zero, this method copies v into dst.

Parameters
vThe vector to normalize.
dstThe destination vector.
Returns
A reference to dst for chaining
cugl::Vec2::operator Size ( ) const

Cast from Vec2 to Size.

cugl::Vec2::operator std::string ( ) const
inline

Cast from Vec2 to a string.

cugl::Vec2::operator Vec3 ( ) const

Casts from Vec2 to Vec3.

The z-value is set to 0.

cugl::Vec2::operator Vec4 ( ) const

Casts from Vec2 to Vec4.

The z-value is set to 0, but the w-value is set to 1. That is because the standard usage of Vec4 objects is homogenous coords.

bool cugl::Vec2::operator!= ( const Vec2 v) const
inline

Returns true if this vector is not equal to the given vector.

Comparison is exact, which may be unreliable given that the attributes are floats.

Parameters
vThe vector to compare against.
Returns
True if this vector is not equal to the given vector.
const Vec2 cugl::Vec2::operator* ( float  s) const
inline

Returns the scalar product of this vector with the given value.

Note: this does not modify this vector.

Parameters
sThe value to scale by.
Returns
The scalar product of this vector with the given value.
const Vec2 cugl::Vec2::operator* ( const Vec2 v) const
inline

Returns the scalar product of this vector with the given vector.

This method is provided to support non-uniform scaling. Note: this does not modify this vector.

Parameters
vThe vector to scale by.
Returns
The scalar product of this vector with the given vector.
Vec2& cugl::Vec2::operator*= ( float  s)
inline

Scales this vector in place by the given factor.

Parameters
sThe value to scale by
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::operator*= ( const Vec2 v)
inline

Scales this vector nonuniformly by the given vector.

Parameters
vThe vector to scale by
Returns
A reference to this (modified) Vec2 for chaining.
const Vec2 cugl::Vec2::operator+ ( const Vec2 v) const
inline

Returns the sum of this vector with the given vector.

Note: this does not modify this vector.

Parameters
vThe vector to add.
Returns
The sum of this vector with the given vector.
const Vec2 cugl::Vec2::operator+ ( const Size right) const
inline

Returns the sum of this vector with the given size.

Note: this does not modify this vector.

Parameters
rightThe suze to add.
Returns
The sum of this vector with the given size.
Vec2& cugl::Vec2::operator+= ( const Vec2 v)
inline

Adds the given vector to this one in place.

Parameters
vThe vector to add
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::operator+= ( const Size right)

Adds the given size to this vector in place.

Parameters
rightThe Size to add
Returns
A reference to this (modified) Vec2 for chaining.
const Vec2 cugl::Vec2::operator- ( const Vec2 v) const
inline

Returns the difference of this vector with the given vector.

Note: this does not modify this vector.

Parameters
vThe vector to subtract.
Returns
The difference of this vector with the given vector.
const Vec2 cugl::Vec2::operator- ( ) const
inline

Returns the negation of this vector.

Note: this does not modify this vector.

Returns
The negation of this vector.
const Vec2 cugl::Vec2::operator- ( const Size right) const
inline

Returns the difference of this vector with the given size.

Note: this does not modify this vector.

Parameters
rightThe size to subtract.
Returns
The difference of this vector with the given size.
Vec2& cugl::Vec2::operator-= ( const Vec2 v)
inline

Subtracts the given vector from this one in place.

Parameters
vThe vector to subtract
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::operator-= ( const Size right)

Subtracts the given size from this vector in place.

Parameters
rightThe Size to subtract
Returns
A reference to this (modified) Vec2 for chaining.
const Vec2 cugl::Vec2::operator/ ( float  s) const
inline

Returns a copy of this vector divided by the given constant

Note: this does not modify this vector.

Parameters
sthe constant to divide this vector with
Returns
A copy of this vector divided by the given constant
const Vec2 cugl::Vec2::operator/ ( const Vec2 v) const
inline

Returns a copy of this vector divided by the given vector.

This method is provided to support non-uniform scaling. Note: this does not modify this vector.

Parameters
vthe vector to divide this vector with
Returns
A copy of this vector divided by the given vector
Vec2& cugl::Vec2::operator/= ( float  s)
inline

Divides this vector in place by the given factor.

Parameters
sThe scalar to divide by
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::operator/= ( const Vec2 v)
inline

Divides this vector in place by the given vector.

This method is provided to support non-uniform scaling.

Parameters
vThe vector to divide by
Returns
A reference to this (modified) Vec2 for chaining.
bool cugl::Vec2::operator< ( const Vec2 v) const
inline

Returns true if this vector is less than the given vector.

This comparison uses the lexicographical order. To test if all components in this vector are less than those of v, use the method under().

Parameters
vThe vector to compare against.
Returns
True if this vector is less than the given vector.
bool cugl::Vec2::operator<= ( const Vec2 v) const
inline

Returns true if this vector is less than or equal the given vector.

This comparison uses the lexicographical order. To test if all components in this vector are less than those of v, use the method under().

Parameters
vThe vector to compare against.
Returns
True if this vector is less than or equal the given vector.
Vec2& cugl::Vec2::operator= ( const Vec2 v)
inline

Sets the elements of this vector to those in the specified vector.

Parameters
vThe vector to copy.
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::operator= ( const float *  array)
inline

Sets the elements of this vector from the values in the specified array.

Parameters
arrayAn array containing the elements of the vector in the order x, y.
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::operator= ( const Size size)

Sets the coordinates of this vector to those of the given size.

The width is converted to the x coordinate and height to y.

Parameters
sizeThe size to convert.
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::operator= ( const Vec3 size)

Sets the coordinates of this vector to those of the given 3d vector.

The z-value is dropped.

Parameters
sizeThe vector to convert
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::operator= ( const Vec4 size)

Sets the coordinates of this vector to those of the given 4d vector.

All coordinates are divided by the w-coordinate (assuming it is not zero) before this conversion. Afterwards, both z and w are dropped.

Parameters
sizeThe vector to convert
Returns
A reference to this (modified) Vec2 for chaining.
bool cugl::Vec2::operator== ( const Vec2 v) const
inline

Returns true if this vector is equal to the given vector.

Comparison is exact, which may be unreliable given that the attributes are floats.

Parameters
vThe vector to compare against.
Returns
True if this vector is equal to the given vector.
bool cugl::Vec2::operator> ( const Vec2 v) const
inline

Returns true if this vector is greater than the given vector.

This comparison uses the lexicographical order. To test if all components in this vector are greater than those of v, use the method over().

Parameters
vThe vector to compare against.
Returns
True if this vector is greater than the given vector.
bool cugl::Vec2::operator>= ( const Vec2 v) const
inline

Returns true if this vector is greater than or equal the given vector.

This comparison uses the lexicographical order. To test if all components in this vector are greater than those of v, use the method over().

Parameters
vThe vector to compare against.
Returns
True if this vector is greater than or equal the given vector.
bool cugl::Vec2::over ( const Vec2 v) const
inline

Returns true if this vector dominates the given vector.

Domination means that all components of this vector are greater than or equal to the components of the given vector.

Parameters
vThe vector to compare against.
Returns
True if this vector is dominated by the given vector.
Vec2& cugl::Vec2::perp ( )
inline

Converts this vector to its perpendicular, rotated 90 degrees counter-clockwise.

The result guarantees cross(original, v) >= 0

Returns
This vector, after the transformation.
Vec2& cugl::Vec2::project ( const Vec2 other)
inline

Modifies this vector to be its projection on to the other one.

Parameters
otherThe vector to project on.
Returns
This vector, after the projection.
static Vec2* cugl::Vec2::project ( const Vec2 v1,
const Vec2 v2,
Vec2 dst 
)
static

Computes the projection of one vector on to another and stores it in dst.

Parameters
v1The original vector.
v2The vector to project on.
dstThe destination vector.
Returns
A reference to dst for chaining
static Vec2* cugl::Vec2::reciprocate ( const Vec2 v,
Vec2 dst 
)
static

Reciprocates the specified quaternion and stores the result in dst.

Reciprocation is applied to each element invidiually. This method does not check that all elements are non-zero. If any element is zero, the result will be system-dependent.

Parameters
vThe vector to reciprocate.
dstThe destination vector.
Returns
A reference to dst for chaining
Vec2& cugl::Vec2::reciprocate ( )
inline

Reciprovates this vector in place.

The reciprocal is computed for each element invidiually. This method does not check that all elements are non-zero. If any element is zero, the result will be system-dependent.

Returns
A reference to this (modified) Vec4 for chaining.
Vec2& cugl::Vec2::rotate ( float  angle)

Rotates this vector by the angle (in radians) around the origin.

Parameters
angleThe angle to rotate by (in radians).
Returns
This vector, after the rotation occurs.
Vec2& cugl::Vec2::rotate ( float  angle,
const Vec2 point 
)

Rotates this vector by the angle (in radians) around the given point.

Parameters
pointThe point to rotate around.
angleThe angle to rotate by (in radians).
Returns
This vector, after the rotation occurs.
Vec2& cugl::Vec2::rotate ( const Vec2 other)
inline

Rotates this vector so that its angle is increased by that of the other.

This rotation uses complex multiplication to add the angles of the two vectors together. This method is faster than rotating by an angle.

Parameters
otherThe vector representing the angle to rotate by
Returns
This vector, after the rotation occurs.
Vec2& cugl::Vec2::rperp ( )
inline

Converts this vector to its perpendicular, rotated 90 degrees clockwise.

The result guarantees cross(original, v) <= 0

Returns
This vector, after the transformation.
static Vec2* cugl::Vec2::scale ( const Vec2 v,
float  s,
Vec2 dst 
)
static

Scales the specified vector and stores the result in dst.

The scale is uniform by the constant s.

Parameters
vThe vector to scale.
sThe uniform scaling factor.
dstThe destination vector.
Returns
A reference to dst for chaining
static Vec2* cugl::Vec2::scale ( const Vec2 v1,
const Vec2 v2,
Vec2 dst 
)
static

Scales the specified vector and stores the result in dst.

The scale is nonuniform by the attributes in v2.

Parameters
v1The vector to scale.
v2The nonuniform scaling factor.
dstThe destination vector.
Returns
A reference to dst for chaining
Vec2& cugl::Vec2::scale ( float  s)
inline

Scales this vector in place by the given factor.

Parameters
sThe scalar to multiply by
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::scale ( float  sx,
float  sy 
)
inline

Scales this vector nonuniformly by the given factors.

Parameters
sxThe scalar to multiply the x-axis
syThe scalar to multiply the y-axis
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::scale ( const Vec2 v)
inline

Scales this vector nonuniformly by the given vector.

Parameters
vThe vector to scale by
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::set ( float  x,
float  y 
)
inline

Sets the elements of this vector to the specified values.

Parameters
xThe new x coordinate.
yThe new y coordinate.
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::set ( const float *  array)
inline

Sets the elements of this vector from the values in the specified array.

Parameters
arrayAn array containing the elements of the vector in the order x, y.
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::set ( const Vec2 v)
inline

Sets the elements of this vector to those in the specified vector.

Parameters
vThe vector to copy.
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::set ( const Vec2 p1,
const Vec2 p2 
)
inline

Sets this vector to the directional vector between the specified points.

Parameters
p1The initial point of the vector.
p2The terminal point of the vector.
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::setZero ( )
inline

Sets the elements of this vector to zero.

Returns
A reference to this (modified) Vec2 for chaining.
static Vec2* cugl::Vec2::subtract ( const Vec2 v1,
const Vec2 v2,
Vec2 dst 
)
static

Subtracts the specified vectors and stores the result in dst. The resulting vector is computed as (v1 - v2).

Parameters
v1The first vector.
v2The second vector.
dstThe destination vector.
Returns
A reference to dst for chaining
Vec2& cugl::Vec2::subtract ( const Vec2 v)
inline

Subtracts the given vector from this one in place.

Parameters
vThe vector to subtract
Returns
A reference to this (modified) Vec2 for chaining.
Vec2& cugl::Vec2::subtract ( float  x,
float  y 
)
inline

Subtracts the given values from this vector.

Parameters
xThe x coordinate to subtract.
yThe y coordinate to subtract.
Returns
A reference to this (modified) Vec2 for chaining.
std::string cugl::Vec2::toString ( bool  verbose = false) const

Returns a string representation of this vector for debugging purposes.

If verbose is true, the string will include class information. This allows us to unambiguously identify the class.

Parameters
verboseWhether to include class information
Returns
a string representation of this vector for debuggging purposes.
bool cugl::Vec2::under ( const Vec2 v) const
inline

Returns true if this vector is dominated by the given vector.

Domination means that all components of the given vector are greater than or equal to the components of this one.

Parameters
vThe vector to compare against.
Returns
True if this vector is dominated by the given vector.
Vec2& cugl::Vec2::unrotate ( const Vec2 other)
inline

Rotates this vector so that its angle is decreased by that of the other.

This rotation uses complex multiplication to add the angles of the two vectors together. This method is faster than rotating by an angle.

Parameters
otherThe vector representing the angle to unrotate by
Returns
This vector, after the rotation occurs.

Member Data Documentation

const Vec2 cugl::Vec2::ANCHOR_BOTTOM_LEFT
static

The relative anchor Vec2(0, 0) in the unit square

const Vec2 cugl::Vec2::ANCHOR_BOTTOM_RIGHT
static

The relative anchor Vec2(1, 0) in the unit square

const Vec2 cugl::Vec2::ANCHOR_MIDDLE
static

The relative anchor Vec2(0.5, 0.5) in the unit square

const Vec2 cugl::Vec2::ANCHOR_MIDDLE_BOTTOM
static

The relative anchor Vec2(0.5, 0) in the unit square

const Vec2 cugl::Vec2::ANCHOR_MIDDLE_LEFT
static

The relative anchor Vec2(0, 0.5) in the unit square

const Vec2 cugl::Vec2::ANCHOR_MIDDLE_RIGHT
static

The relative anchor Vec2(1, 0.5) in the unit square

const Vec2 cugl::Vec2::ANCHOR_MIDDLE_TOP
static

The relative anchor Vec2(0.5, 1) in the unit square

const Vec2 cugl::Vec2::ANCHOR_TOP_LEFT
static

The relative anchor Vec2(0, 1) in the unit square

const Vec2 cugl::Vec2::ANCHOR_TOP_RIGHT
static

The relative anchor Vec2(1, 1) in the unit square

const Vec2 cugl::Vec2::ONE
static

The unit vector Vec2(1,1)

const Vec2 cugl::Vec2::UNIT_X
static

The x-axis Vec2(1,0)

const Vec2 cugl::Vec2::UNIT_Y
static

The y-axis Vec2(0,1)

float cugl::Vec2::x

The x coordinate.

float cugl::Vec2::y

The x coordinate.

const Vec2 cugl::Vec2::ZERO
static

The zero vector Vec2(0,0)


The documentation for this class was generated from the following file: