Cocos2d-x  v3.17
Vec2 Class Reference

Defines a 2-element floating point vector. More...

## Public Member Functions

Vec2 ()
Constructs a new vector initialized to all zeros.

Vec2 ()
Constructs a new vector initialized to all zeros.

Vec2 ()
Constructs a new vector initialized to all zeros.

Vec2 (float xx, float yy)
Constructs a new vector initialized to the specified values. More...

Vec2 (const float *array)
Constructs a new vector from the values in the specified array. More...

Vec2 ( var array)
Constructs a new vector from the values in the specified array. More...

Vec2 ( local array)
Constructs a new vector from the values in the specified array. More...

Vec2 (const Vec2 &p1, const Vec2 &p2)
Constructs a vector that describes the direction between the specified points. More...

Vec2 (const Vec2 &copy)
Constructs a new vector that is a copy of the specified vector. More...

~Vec2 ()
Destructor.

bool isZero () const
Indicates whether this vector contains all zeros. More...

bool isOne () const
Indicates whether this vector contains all ones. More...

void add (const Vec2 &v)
Adds the elements of the specified vector to this one. More...

var add ( var v)
Adds the elements of the specified vector to this one. More...

local add ( local v)
Adds the elements of the specified vector to this one. More...

void clamp (const Vec2 &min, const Vec2 &max)
Clamps this vector within the specified range. More...

var clamp ( var min, var max)
Clamps this vector within the specified range. More...

local clamp ( local min, local max)
Clamps this vector within the specified range. More...

float distance (const Vec2 &v) const
Returns the distance between this vector and v. More...

float distanceSquared (const Vec2 &v) const
Returns the squared distance between this vector and v. More...

float dot (const Vec2 &v) const
Returns the dot product of this vector and the specified vector. More...

float length () const
Computes the length of this vector. More...

float lengthSquared () const
Returns the squared length of this vector. More...

var lengthSquared ()
Returns the squared length of this vector. More...

local lengthSquared ()
Returns the squared length of this vector. More...

void negate ()
Negates this vector.

var negate ()
Negates this vector.

local negate ()
Negates this vector.

void normalize ()
Normalizes this vector. More...

var normalize ()
Normalizes this vector. More...

local normalize ()
Normalizes this vector. More...

Vec2 getNormalized () const
Get the normalized vector.

var getNormalized ()
Get the normalized vector.

local getNormalized ()
Get the normalized vector.

void scale (float scalar)
Scales all elements of this vector by the specified value. More...

var scale ( var scalar)
Scales all elements of this vector by the specified value. More...

local scale ( local scalar)
Scales all elements of this vector by the specified value. More...

void scale (const Vec2 &scale)
Scales each element of this vector by the matching component of scale. More...

void rotate (const Vec2 &point, float angle)
Rotates this vector by angle (specified in radians) around the given point. More...

var rotate ( var point, var angle)
Rotates this vector by angle (specified in radians) around the given point. More...

local rotate ( local point, local angle)
Rotates this vector by angle (specified in radians) around the given point. More...

void set (float xx, float yy)
Sets the elements of this vector to the specified values. More...

var set ( var xx, var yy)
Sets the elements of this vector to the specified values. More...

local set ( local xx, local yy)
Sets the elements of this vector to the specified values. More...

void set (const float *array)
Sets the elements of this vector from the values in the specified array. More...

void set (const Vec2 &v)
Sets the elements of this vector to those in the specified vector. More...

void set (const Vec2 &p1, const Vec2 &p2)
Sets this vector to the directional vector between the specified points. More...

void setZero ()
Sets the elements of this vector to zero.

void subtract (const Vec2 &v)
Subtracts this vector and the specified vector as (this - v) and stores the result in this vector. More...

void smooth (const Vec2 &target, float elapsedTime, float responseTime)
Updates this vector towards the given target using a smoothing function. More...

Vec2 operator+ (const Vec2 &v) const
Calculates the sum of this vector with the given vector. More...

Vec2operator+= (const Vec2 &v)
Adds the given vector to this vector. More...

Vec2 operator- (const Vec2 &v) const
Calculates the sum of this vector with the given vector. More...

Vec2operator-= (const Vec2 &v)
Subtracts the given vector from this vector. More...

Vec2 operator- () const
Calculates the negation of this vector. More...

Vec2 operator* (float s) const
Calculates the scalar product of this vector with the given value. More...

var operator* ( var s)
Calculates the scalar product of this vector with the given value. More...

local operator* ( local s)
Calculates the scalar product of this vector with the given value. More...

Vec2operator*= (float s)
Scales this vector by the given value. More...

Vec2 operator/ (float s) const
Returns the components of this vector divided by the given constant. More...

bool operator< (const Vec2 &v) const
Determines if this vector is less than the given vector. More...

bool operator> (const Vec2 &v) const
Determines if this vector is greater than the given vector. More...

bool operator== (const Vec2 &v) const
Determines if this vector is equal to the given vector. More...

bool operator!= (const Vec2 &v) const
Determines if this vector is not equal to the given vector. More...

bool fuzzyEquals (const Vec2 &target, float variance) const
float getLength () const
Calculates distance between point an origin. More...

float getLengthSq () const
Calculates the square length of a Vec2 (not calling sqrt() ) More...

float getDistanceSq (const Vec2 &other) const
Calculates the square distance between two points (not calling sqrt() ) More...

float getDistance (const Vec2 &other) const
Calculates the distance between two points. More...

float getAngle () const
float getAngle (const Vec2 &other) const
float cross (const Vec2 &other) const
Calculates cross product of two points. More...

Vec2 getPerp () const
Calculates perpendicular of v, rotated 90 degrees counter-clockwise – cross(v, perp(v)) >= 0. More...

Vec2 getMidpoint (const Vec2 &other) const
Calculates midpoint between two points. More...

Vec2 getClampPoint (const Vec2 &min_inclusive, const Vec2 &max_inclusive) const
Clamp a point between from and to. More...

Vec2 compOp (std::function< float(float)> function) const
Run a math operation function on each point component absf, floorf, ceilf, roundf any function that has the signature: float func(float); For example: let's try to take the floor of x,y p.compOp(floorf);. More...

Vec2 getRPerp () const
Calculates perpendicular of v, rotated 90 degrees clockwise – cross(v, rperp(v)) <= 0. More...

Vec2 project (const Vec2 &other) const
Calculates the projection of this over other. More...

Vec2 rotate (const Vec2 &other) const
Complex multiplication of two points ("rotates" two points). More...

Vec2 unrotate (const Vec2 &other) const
Unrotates two points. More...

Vec2 lerp (const Vec2 &other, float alpha) const
Linear Interpolation between two points a and b. More...

Vec2 rotateByAngle (const Vec2 &pivot, float angle) const
Rotates a point counter clockwise by the angle around a pivot. More...

## Static Public Member Functions

static float angle (const Vec2 &v1, const Vec2 &v2)
Returns the angle (in radians) between the specified vectors. More...

static void add (const Vec2 &v1, const Vec2 &v2, Vec2 *dst)
Adds the specified vectors and stores the result in dst. More...

static void clamp (const Vec2 &v, const Vec2 &min, const Vec2 &max, Vec2 *dst)
Clamps the specified vector within the specified range and returns it in dst. More...

local clamp ( local v, local min, local max, local dst)
Clamps the specified vector within the specified range and returns it in dst. More...

static float dot (const Vec2 &v1, const Vec2 &v2)
Returns the dot product between the specified vectors. More...

local dot ( local v1, local v2)
Returns the dot product between the specified vectors. More...

static void subtract (const Vec2 &v1, const Vec2 &v2, Vec2 *dst)
Subtracts the specified vectors and stores the result in dst. More...

local subtract ( local v1, local v2, local dst)
Subtracts the specified vectors and stores the result in dst. More...

static bool isLineIntersect (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D, float *S=nullptr, float *T=nullptr)
A general line-line intersection test. More...

static bool isLineOverlap (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D)
returns true if Line A-B overlap with segment C-D More...

static bool isLineParallel (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D)
returns true if Line A-B parallel with segment C-D More...

static bool isSegmentOverlap (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D, Vec2 *S=nullptr, Vec2 *E=nullptr)
returns true if Segment A-B overlap with segment C-D More...

static bool isSegmentIntersect (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D)
returns true if Segment A-B intersects with segment C-D More...

static Vec2 getIntersectPoint (const Vec2 &A, const Vec2 &B, const Vec2 &C, const Vec2 &D)
returns the intersection point of line A-B, C-D More...

## Public Attributes

float x
The x coordinate.

var x
The x coordinate.

local x
The x coordinate.

float y
The y coordinate.

var y
The y coordinate.

local y
The y coordinate.

## Static Public Attributes

static const Vec2 ZERO
equals to Vec2(0,0)

var ZERO
equals to Vec2(0,0)

static const Vec2 ONE
equals to Vec2(1,1)

static const Vec2 UNIT_X
equals to Vec2(1,0)

var UNIT_X
equals to Vec2(1,0)

local UNIT_X
equals to Vec2(1,0)

static const Vec2 UNIT_Y
equals to Vec2(0,1)

var UNIT_Y
equals to Vec2(0,1)

local UNIT_Y
equals to Vec2(0,1)

static const Vec2 ANCHOR_MIDDLE
equals to Vec2(0.5, 0.5)

var ANCHOR_MIDDLE
equals to Vec2(0.5, 0.5)

local ANCHOR_MIDDLE
equals to Vec2(0.5, 0.5)

static const Vec2 ANCHOR_BOTTOM_LEFT
equals to Vec2(0, 0)

var ANCHOR_BOTTOM_LEFT
equals to Vec2(0, 0)

local ANCHOR_BOTTOM_LEFT
equals to Vec2(0, 0)

static const Vec2 ANCHOR_TOP_LEFT
equals to Vec2(0, 1)

var ANCHOR_TOP_LEFT
equals to Vec2(0, 1)

local ANCHOR_TOP_LEFT
equals to Vec2(0, 1)

static const Vec2 ANCHOR_BOTTOM_RIGHT
equals to Vec2(1, 0)

var ANCHOR_BOTTOM_RIGHT
equals to Vec2(1, 0)

local ANCHOR_BOTTOM_RIGHT
equals to Vec2(1, 0)

static const Vec2 ANCHOR_TOP_RIGHT
equals to Vec2(1, 1)

var ANCHOR_TOP_RIGHT
equals to Vec2(1, 1)

local ANCHOR_TOP_RIGHT
equals to Vec2(1, 1)

static const Vec2 ANCHOR_MIDDLE_RIGHT
equals to Vec2(1, 0.5)

var ANCHOR_MIDDLE_RIGHT
equals to Vec2(1, 0.5)

local ANCHOR_MIDDLE_RIGHT
equals to Vec2(1, 0.5)

static const Vec2 ANCHOR_MIDDLE_LEFT
equals to Vec2(0, 0.5)

var ANCHOR_MIDDLE_LEFT
equals to Vec2(0, 0.5)

local ANCHOR_MIDDLE_LEFT
equals to Vec2(0, 0.5)

static const Vec2 ANCHOR_MIDDLE_TOP
equals to Vec2(0.5, 1)

var ANCHOR_MIDDLE_TOP
equals to Vec2(0.5, 1)

local ANCHOR_MIDDLE_TOP
equals to Vec2(0.5, 1)

static const Vec2 ANCHOR_MIDDLE_BOTTOM
equals to Vec2(0.5, 0)

var ANCHOR_MIDDLE_BOTTOM
equals to Vec2(0.5, 0)

local ANCHOR_MIDDLE_BOTTOM
equals to Vec2(0.5, 0)

## Detailed Description

Defines a 2-element floating point vector.

## Constructor & Destructor Documentation

 Vec2 ( float xx, float yy )

Constructs a new vector initialized to the specified values.

Parameters
 xx The x coordinate. yy The y coordinate.
 var Vec2 ( var xx, var yy )

Constructs a new vector initialized to the specified values.

Parameters
 xx The x coordinate. yy The y coordinate.
 local Vec2 ( local xx, local yy )

Constructs a new vector initialized to the specified values.

Parameters
 xx The x coordinate. yy The y coordinate.
 Vec2 ( const float * array )

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

Parameters
 array An array containing the elements of the vector in the order x, y.
 var Vec2 ( var array )

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

Parameters
 array An array containing the elements of the vector in the order x, y.
 local Vec2 ( local array )

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

Parameters
 array An array containing the elements of the vector in the order x, y.
 Vec2 ( const Vec2 & p1, const Vec2 & p2 )

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

Parameters
 p1 The first point. p2 The second point.
 var Vec2 ( var p1, var p2 )

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

Parameters
 p1 The first point. p2 The second point.
 local Vec2 ( local p1, local p2 )

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

Parameters
 p1 The first point. p2 The second point.
 Vec2 ( const Vec2 & copy )

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

Parameters
 copy The vector to copy.
 var Vec2 ( var copy )

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

Parameters
 copy The vector to copy.
 local Vec2 ( local copy )

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

Parameters
 copy The vector to copy.

## Member Function Documentation

 bool isZero ( ) const
inline

Indicates whether this vector contains all zeros.

Returns
true if this vector contains all zeros, false otherwise.
 var isZero ( )
inline

Indicates whether this vector contains all zeros.

Returns
true if this vector contains all zeros, false otherwise.
 local isZero ( )
inline

Indicates whether this vector contains all zeros.

Returns
true if this vector contains all zeros, false otherwise.
 bool isOne ( ) const
inline

Indicates whether this vector contains all ones.

Returns
true if this vector contains all ones, false otherwise.
 var isOne ( )
inline

Indicates whether this vector contains all ones.

Returns
true if this vector contains all ones, false otherwise.
 local isOne ( )
inline

Indicates whether this vector contains all ones.

Returns
true if this vector contains all ones, false otherwise.
 static float angle ( const Vec2 & v1, const Vec2 & v2 )
static

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

Parameters
 v1 The first vector. v2 The second vector.
Returns
The angle between the two vectors (in radians).
 var angle ( var v1, var v2 )
static

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

Parameters
 v1 The first vector. v2 The second vector.
Returns
The angle between the two vectors (in radians).
 local angle ( local v1, local v2 )
static

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

Parameters
 v1 The first vector. v2 The second vector.
Returns
The angle between the two vectors (in radians).
 void add ( const Vec2 & v )
inline

Adds the elements of the specified vector to this one.

Parameters
 v The vector to add.
 var add ( var v )
inline

Adds the elements of the specified vector to this one.

Parameters
 v The vector to add.
 local add ( local v )
inline

Adds the elements of the specified vector to this one.

Parameters
 v The vector to add.
 static void add ( const Vec2 & v1, const Vec2 & v2, Vec2 * dst )
static

Adds the specified vectors and stores the result in dst.

Parameters
 v1 The first vector. v2 The second vector. dst A vector to store the result in.
 var add ( var v1, var v2, var dst )
static

Adds the specified vectors and stores the result in dst.

Parameters
 v1 The first vector. v2 The second vector. dst A vector to store the result in.
 local add ( local v1, local v2, local dst )
static

Adds the specified vectors and stores the result in dst.

Parameters
 v1 The first vector. v2 The second vector. dst A vector to store the result in.
 void clamp ( const Vec2 & min, const Vec2 & max )

Clamps this vector within the specified range.

Parameters
 min The minimum value. max The maximum value.
 var clamp ( var min, var max )

Clamps this vector within the specified range.

Parameters
 min The minimum value. max The maximum value.
 local clamp ( local min, local max )

Clamps this vector within the specified range.

Parameters
 min The minimum value. max The maximum value.
 static void clamp ( const Vec2 & v, const Vec2 & min, const Vec2 & max, Vec2 * dst )
static

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

Parameters
 v The vector to clamp. min The minimum value. max The maximum value. dst A vector to store the result in.
 var clamp ( var v, var min, var max, var dst )
static

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

Parameters
 v The vector to clamp. min The minimum value. max The maximum value. dst A vector to store the result in.
 local clamp ( local v, local min, local max, local dst )
static

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

Parameters
 v The vector to clamp. min The minimum value. max The maximum value. dst A vector to store the result in.
 float distance ( const Vec2 & v ) const

Returns the distance between this vector and v.

Parameters
 v The other vector.
Returns
The distance between this vector and v.
distanceSquared
 var distance ( var v )

Returns the distance between this vector and v.

Parameters
 v The other vector.
Returns
The distance between this vector and v.
distanceSquared
 local distance ( local v )

Returns the distance between this vector and v.

Parameters
 v The other vector.
Returns
The distance between this vector and v.
distanceSquared
 float distanceSquared ( const Vec2 & v ) const
inline

Returns the squared distance between this vector and v.

When it is not necessary to get the exact distance between two vectors (for example, when simply comparing the distance between different vectors), it is advised to use this method instead of distance.

Parameters
 v The other vector.
Returns
The squared distance between this vector and v.
distance
 var distanceSquared ( var v )
inline

Returns the squared distance between this vector and v.

When it is not necessary to get the exact distance between two vectors (for example, when simply comparing the distance between different vectors), it is advised to use this method instead of distance.

Parameters
 v The other vector.
Returns
The squared distance between this vector and v.
distance
 local distanceSquared ( local v )
inline

Returns the squared distance between this vector and v.

When it is not necessary to get the exact distance between two vectors (for example, when simply comparing the distance between different vectors), it is advised to use this method instead of distance.

Parameters
 v The other vector.
Returns
The squared distance between this vector and v.
distance
 float dot ( const Vec2 & v ) const
inline

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

Parameters
 v The vector to compute the dot product with.
Returns
The dot product.
 var dot ( var v )
inline

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

Parameters
 v The vector to compute the dot product with.
Returns
The dot product.
 local dot ( local v )
inline

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

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

Returns the dot product between the specified vectors.

Parameters
 v1 The first vector. v2 The second vector.
Returns
The dot product between the vectors.
 var dot ( var v1, var v2 )
static

Returns the dot product between the specified vectors.

Parameters
 v1 The first vector. v2 The second vector.
Returns
The dot product between the vectors.
 local dot ( local v1, local v2 )
static

Returns the dot product between the specified vectors.

Parameters
 v1 The first vector. v2 The second vector.
Returns
The dot product between the vectors.
 float length ( ) const

Computes the length of this vector.

Returns
The length of the vector.
lengthSquared
 var length ( )

Computes the length of this vector.

Returns
The length of the vector.
lengthSquared
 local length ( )

Computes the length of this vector.

Returns
The length of the vector.
lengthSquared
 float lengthSquared ( ) const
inline

Returns the squared length of this vector.

When it is not necessary to get the exact length of a vector (for example, when simply comparing the lengths of different vectors), it is advised to use this method instead of length.

Returns
The squared length of the vector.
length
 var lengthSquared ( )
inline

Returns the squared length of this vector.

When it is not necessary to get the exact length of a vector (for example, when simply comparing the lengths of different vectors), it is advised to use this method instead of length.

Returns
The squared length of the vector.
length
 local lengthSquared ( )
inline

Returns the squared length of this vector.

When it is not necessary to get the exact length of a vector (for example, when simply comparing the lengths of different vectors), it is advised to use this method instead of length.

Returns
The squared length of the vector.
length
 void normalize ( )

Normalizes this vector.

This method normalizes this Vec2 so that it is of unit length (in other words, 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.
 var normalize ( )

Normalizes this vector.

This method normalizes this Vec2 so that it is of unit length (in other words, 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.
 local normalize ( )

Normalizes this vector.

This method normalizes this Vec2 so that it is of unit length (in other words, 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.
 void scale ( float scalar )
inline

Scales all elements of this vector by the specified value.

Parameters
 scalar The scalar value.
 var scale ( var scalar )
inline

Scales all elements of this vector by the specified value.

Parameters
 scalar The scalar value.
 local scale ( local scalar )
inline

Scales all elements of this vector by the specified value.

Parameters
 scalar The scalar value.
 void scale ( const Vec2 & scale )
inline

Scales each element of this vector by the matching component of scale.

Parameters
 scale The vector to scale by.
 var scale ( var scale )
inline

Scales each element of this vector by the matching component of scale.

Parameters
 scale The vector to scale by.
 local scale ( local scale )
inline

Scales each element of this vector by the matching component of scale.

Parameters
 scale The vector to scale by.
 void rotate ( const Vec2 & point, float angle )

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

Parameters
 point The point to rotate around. angle The angle to rotate by (in radians).
 var rotate ( var point, var angle )

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

Parameters
 point The point to rotate around. angle The angle to rotate by (in radians).
 local rotate ( local point, local angle )

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

Parameters
 point The point to rotate around. angle The angle to rotate by (in radians).
 void set ( float xx, float yy )
inline

Sets the elements of this vector to the specified values.

Parameters
 xx The new x coordinate. yy The new y coordinate.
 var set ( var xx, var yy )
inline

Sets the elements of this vector to the specified values.

Parameters
 xx The new x coordinate. yy The new y coordinate.
 local set ( local xx, local yy )
inline

Sets the elements of this vector to the specified values.

Parameters
 xx The new x coordinate. yy The new y coordinate.
 void set ( const float * array )

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

Parameters
 array An array containing the elements of the vector in the order x, y.
 var set ( var array )

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

Parameters
 array An array containing the elements of the vector in the order x, y.
 local set ( local array )

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

Parameters
 array An array containing the elements of the vector in the order x, y.
 void set ( const Vec2 & v )
inline

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

Parameters
 v The vector to copy.
 var set ( var v )
inline

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

Parameters
 v The vector to copy.
 local set ( local v )
inline

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

Parameters
 v The vector to copy.
 void set ( const Vec2 & p1, const Vec2 & p2 )
inline

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

Parameters
 p1 The first point. p2 The second point.
 var set ( var p1, var p2 )
inline

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

Parameters
 p1 The first point. p2 The second point.
 local set ( local p1, local p2 )
inline

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

Parameters
 p1 The first point. p2 The second point.
 void subtract ( const Vec2 & v )
inline

Subtracts this vector and the specified vector as (this - v) and stores the result in this vector.

Parameters
 v The vector to subtract.
 var subtract ( var v )
inline

Subtracts this vector and the specified vector as (this - v) and stores the result in this vector.

Parameters
 v The vector to subtract.
 local subtract ( local v )
inline

Subtracts this vector and the specified vector as (this - v) and stores the result in this vector.

Parameters
 v The vector to subtract.
 static void 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
 v1 The first vector. v2 The second vector. dst The destination vector.
 var subtract ( var v1, var v2, var dst )
static

Subtracts the specified vectors and stores the result in dst.

The resulting vector is computed as (v1 - v2).

Parameters
 v1 The first vector. v2 The second vector. dst The destination vector.
 local subtract ( local v1, local v2, local dst )
static

Subtracts the specified vectors and stores the result in dst.

The resulting vector is computed as (v1 - v2).

Parameters
 v1 The first vector. v2 The second vector. dst The destination vector.
 void smooth ( const Vec2 & target, float elapsedTime, float responseTime )
inline

Updates this vector towards the given target using a smoothing function.

The given response time determines the amount of smoothing (lag). A longer response time yields a smoother result and more lag. To force this vector to follow the target closely, provide a response time that is very small relative to the given elapsed time.

Parameters
 target target value. elapsedTime elapsed time between calls. responseTime response time (in the same units as elapsedTime).
 var smooth ( var target, var elapsedTime, var responseTime )
inline

Updates this vector towards the given target using a smoothing function.

The given response time determines the amount of smoothing (lag). A longer response time yields a smoother result and more lag. To force this vector to follow the target closely, provide a response time that is very small relative to the given elapsed time.

Parameters
 target target value. elapsedTime elapsed time between calls. responseTime response time (in the same units as elapsedTime).
 local smooth ( local target, local elapsedTime, local responseTime )
inline

Updates this vector towards the given target using a smoothing function.

The given response time determines the amount of smoothing (lag). A longer response time yields a smoother result and more lag. To force this vector to follow the target closely, provide a response time that is very small relative to the given elapsed time.

Parameters
 target target value. elapsedTime elapsed time between calls. responseTime response time (in the same units as elapsedTime).
 Vec2 operator+ ( const Vec2 & v ) const
inline

Calculates the sum of this vector with the given vector.

Note: this does not modify this vector.

Parameters
 v The vector to add.
Returns
The vector sum.
 Vec2 operator+ ( var v )
inline

Calculates the sum of this vector with the given vector.

Note: this does not modify this vector.

Parameters
 v The vector to add.
Returns
The vector sum.
 Vec2 operator+ ( local v )
inline

Calculates the sum of this vector with the given vector.

Note: this does not modify this vector.

Parameters
 v The vector to add.
Returns
The vector sum.
 Vec2& operator+= ( const Vec2 & v )
inline

Adds the given vector to this vector.

Parameters
 v The vector to add.
Returns
This vector, after the addition occurs.
 Vec2& operator+= ( var v )
inline

Adds the given vector to this vector.

Parameters
 v The vector to add.
Returns
This vector, after the addition occurs.
 Vec2& operator+= ( local v )
inline

Adds the given vector to this vector.

Parameters
 v The vector to add.
Returns
This vector, after the addition occurs.
 Vec2 operator- ( const Vec2 & v ) const
inline

Calculates the sum of this vector with the given vector.

Note: this does not modify this vector.

Parameters
 v The vector to add.
Returns
The vector sum.
 Vec2 operator- ( var v )
inline

Calculates the sum of this vector with the given vector.

Note: this does not modify this vector.

Parameters
 v The vector to add.
Returns
The vector sum.
 Vec2 operator- ( local v )
inline

Calculates the sum of this vector with the given vector.

Note: this does not modify this vector.

Parameters
 v The vector to add.
Returns
The vector sum.
 Vec2& operator-= ( const Vec2 & v )
inline

Subtracts the given vector from this vector.

Parameters
 v The vector to subtract.
Returns
This vector, after the subtraction occurs.
 Vec2& operator-= ( var v )
inline

Subtracts the given vector from this vector.

Parameters
 v The vector to subtract.
Returns
This vector, after the subtraction occurs.
 Vec2& operator-= ( local v )
inline

Subtracts the given vector from this vector.

Parameters
 v The vector to subtract.
Returns
This vector, after the subtraction occurs.
 Vec2 operator- ( ) const
inline

Calculates the negation of this vector.

Note: this does not modify this vector.

Returns
The negation of this vector.
 Vec2 operator- ( )
inline

Calculates the negation of this vector.

Note: this does not modify this vector.

Returns
The negation of this vector.
 Vec2 operator- ( )
inline

Calculates the negation of this vector.

Note: this does not modify this vector.

Returns
The negation of this vector.
 Vec2 operator* ( float s ) const
inline

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

Note: this does not modify this vector.

Parameters
 s The value to scale by.
Returns
The scaled vector.
 Vec2 operator* ( var s )
inline

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

Note: this does not modify this vector.

Parameters
 s The value to scale by.
Returns
The scaled vector.
 Vec2 operator* ( local s )
inline

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

Note: this does not modify this vector.

Parameters
 s The value to scale by.
Returns
The scaled vector.
 Vec2& operator*= ( float s )
inline

Scales this vector by the given value.

Parameters
 s The value to scale by.
Returns
This vector, after the scale occurs.
 Vec2& operator*= ( var s )
inline

Scales this vector by the given value.

Parameters
 s The value to scale by.
Returns
This vector, after the scale occurs.
 Vec2& operator*= ( local s )
inline

Scales this vector by the given value.

Parameters
 s The value to scale by.
Returns
This vector, after the scale occurs.
 Vec2 operator/ ( float s ) const
inline

Returns the components of this vector divided by the given constant.

Note: this does not modify this vector.

Parameters
 s the constant to divide this vector with
Returns
a smaller vector
 Vec2 operator/ ( var s )
inline

Returns the components of this vector divided by the given constant.

Note: this does not modify this vector.

Parameters
 s the constant to divide this vector with
Returns
a smaller vector
 Vec2 operator/ ( local s )
inline

Returns the components of this vector divided by the given constant.

Note: this does not modify this vector.

Parameters
 s the constant to divide this vector with
Returns
a smaller vector
 bool operator< ( const Vec2 & v ) const
inline

Determines if this vector is less than the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is less than the given vector, false otherwise.
 bool operator< ( var v )
inline

Determines if this vector is less than the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is less than the given vector, false otherwise.
 bool operator< ( local v )
inline

Determines if this vector is less than the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is less than the given vector, false otherwise.
 bool operator> ( const Vec2 & v ) const
inline

Determines if this vector is greater than the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is greater than the given vector, false otherwise.
 bool operator> ( var v )
inline

Determines if this vector is greater than the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is greater than the given vector, false otherwise.
 bool operator> ( local v )
inline

Determines if this vector is greater than the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is greater than the given vector, false otherwise.
 bool operator== ( const Vec2 & v ) const
inline

Determines if this vector is equal to the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is equal to the given vector, false otherwise.
 bool operator== ( var v )
inline

Determines if this vector is equal to the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is equal to the given vector, false otherwise.
 bool operator== ( local v )
inline

Determines if this vector is equal to the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is equal to the given vector, false otherwise.
 bool operator!= ( const Vec2 & v ) const
inline

Determines if this vector is not equal to the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is not equal to the given vector, false otherwise.
 bool operator!= ( var v )
inline

Determines if this vector is not equal to the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is not equal to the given vector, false otherwise.
 bool operator!= ( local v )
inline

Determines if this vector is not equal to the given vector.

Parameters
 v The vector to compare against.
Returns
True if this vector is not equal to the given vector, false otherwise.
 bool fuzzyEquals ( const Vec2 & target, float variance ) const
Returns
if points have fuzzy equality which means equal with some degree of variance.
Since
v2.1.4
 float getLength ( ) const
inline

Calculates distance between point an origin.

Returns
float
Since
v2.1.4
 float getLengthSq ( ) const
inline

Calculates the square length of a Vec2 (not calling sqrt() )

Returns
float
Since
v2.1.4
 float getDistanceSq ( const Vec2 & other ) const
inline

Calculates the square distance between two points (not calling sqrt() )

Returns
float
Since
v2.1.4
 float getDistance ( const Vec2 & other ) const
inline

Calculates the distance between two points.

Returns
float
Since
v2.1.4
 float getAngle ( ) const
inline
Returns
the angle in radians between this vector and the x axis
Since
v2.1.4
 float getAngle ( const Vec2 & other ) const
Returns
the angle in radians between two vector directions
Since
v2.1.4
 float cross ( const Vec2 & other ) const
inline

Calculates cross product of two points.

Returns
float
Since
v2.1.4
 Vec2 getPerp ( ) const
inline

Calculates perpendicular of v, rotated 90 degrees counter-clockwise – cross(v, perp(v)) >= 0.

Returns
Vec2
Since
v2.1.4
 Vec2 getMidpoint ( const Vec2 & other ) const
inline

Calculates midpoint between two points.

Returns
Vec2
Since
v3.0
 Vec2 getClampPoint ( const Vec2 & min_inclusive, const Vec2 & max_inclusive ) const
inline

Clamp a point between from and to.

Since
v3.0
 Vec2 compOp ( std::function< float(float)> function ) const
inline

Run a math operation function on each point component absf, floorf, ceilf, roundf any function that has the signature: float func(float); For example: let's try to take the floor of x,y p.compOp(floorf);.

Since
v3.0
 Vec2 getRPerp ( ) const
inline

Calculates perpendicular of v, rotated 90 degrees clockwise – cross(v, rperp(v)) <= 0.

Returns
Vec2
Since
v2.1.4
 Vec2 project ( const Vec2 & other ) const
inline

Calculates the projection of this over other.

Returns
Vec2
Since
v2.1.4
 Vec2 rotate ( const Vec2 & other ) const
inline

Complex multiplication of two points ("rotates" two points).

Returns
Vec2 vector with an angle of this.getAngle() + other.getAngle(), and a length of this.getLength() * other.getLength().
Since
v2.1.4
 Vec2 unrotate ( const Vec2 & other ) const
inline

Unrotates two points.

Returns
Vec2 vector with an angle of this.getAngle() - other.getAngle(), and a length of this.getLength() * other.getLength().
Since
v2.1.4
 Vec2 lerp ( const Vec2 & other, float alpha ) const
inline

Linear Interpolation between two points a and b.

Returns
alpha == 0 ? a alpha == 1 ? b otherwise a value between a..b
Since
v2.1.4
 Vec2 rotateByAngle ( const Vec2 & pivot, float angle ) const

Rotates a point counter clockwise by the angle around a pivot.

Parameters
 pivot is the pivot, naturally angle is the angle of rotation ccw in radians
Returns
the rotated point
Since
v2.1.4
 static bool isLineIntersect ( const Vec2 & A, const Vec2 & B, const Vec2 & C, const Vec2 & D, float * S = nullptr, float * T = nullptr )
static

A general line-line intersection test.

Parameters
 A the startpoint for the first line L1 = (A - B) B the endpoint for the first line L1 = (A - B) C the startpoint for the second line L2 = (C - D) D the endpoint for the second line L2 = (C - D) S the range for a hitpoint in L1 (p = A + S*(B - A)) T the range for a hitpoint in L2 (p = C + T*(D - C))
Returns
whether these two lines intersects.

Note that to truly test intersection for segments we have to make sure that S & T lie within [0..1] and for rays, make sure S & T > 0 the hit point is C + T * (D - C); the hit point also is A + S * (B - A);

Since
3.0
 static bool isLineOverlap ( const Vec2 & A, const Vec2 & B, const Vec2 & C, const Vec2 & D )
static

returns true if Line A-B overlap with segment C-D

Since
v3.0
 static bool isLineParallel ( const Vec2 & A, const Vec2 & B, const Vec2 & C, const Vec2 & D )
static

returns true if Line A-B parallel with segment C-D

Since
v3.0
 static bool isSegmentOverlap ( const Vec2 & A, const Vec2 & B, const Vec2 & C, const Vec2 & D, Vec2 * S = nullptr, Vec2 * E = nullptr )
static

returns true if Segment A-B overlap with segment C-D

Since
v3.0
 static bool isSegmentIntersect ( const Vec2 & A, const Vec2 & B, const Vec2 & C, const Vec2 & D )
static

returns true if Segment A-B intersects with segment C-D

Since
v3.0
 static Vec2 getIntersectPoint ( const Vec2 & A, const Vec2 & B, const Vec2 & C, const Vec2 & D )
static

returns the intersection point of line A-B, C-D

Since
v3.0

The documentation for this class was generated from the following file:
• Vec2.h