This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.
ofVec2f is a class for storing a two dimensional vector. More...
#include <ofVec2f.h>
Public Member Functions | |
ofVec2f (const glm::vec2 &v) | |
ofVec2f (const glm::vec3 &v) | |
ofVec2f (const glm::vec4 &v) | |
operator glm::vec2 () const | |
Construct a 2D vector | |
ofVec2f () | |
Construct a 2D vector. | |
ofVec2f (float scalar) | |
Construct a 2D vector with x and y set to scalar | |
ofVec2f (float x, float y) | |
Construct a 2D vector with specific x and `y components. | |
ofVec2f (const ofVec3f &vec) | |
Create a 2D vector (ofVec2f) from a 3D vector (ofVec3f) by \throwing away the z component of the 3D vector. | |
ofVec2f (const ofVec4f &vec) | |
Create a 2D vector (ofVec2f) from a 4D vector (ofVec4f) by throwing away the z and w components of the 4D vector. | |
Access components | |
float * | getPtr () |
Returns a pointer to the memory position of the first element of the vector (x); the second element (y) immediately follows it in memory. | |
const float * | getPtr () const |
float & | operator[] (int n) |
Allows to access the x and y components of an ofVec2f as though it is an array. | |
float | operator[] (int n) const |
void | set (float x, float y) |
Set x and y components of this vector with just one function call. | |
void | set (const ofVec2f &vec) |
Set the x and y components of this vector by copying the corresponding values from vec. | |
void | set (float scalar) |
Comparison | |
bool | operator== (const ofVec2f &vec) const |
Check for equality between two ofVec2f. | |
bool | operator!= (const ofVec2f &vec) const |
Check for inequality between two ofVec2f. | |
bool | match (const ofVec2f &vec, float tolerance=0.0001f) const |
Returns true if each component is close enough to its corresponding component in vec, where what is close enough is determined by the value of tolerance: | |
bool | isAligned (const ofVec2f &vec, float tolerance=0.0001f) const |
Determine if two vectors are aligned. | |
bool | isAlignedRad (const ofVec2f &vec, float tolerance=0.0001f) const |
Determine if two vectors are aligned with tolerance in radians. | |
bool | align (const ofVec2f &vec, float tolerance=0.0001f) const |
Determine if two vectors are aligned. | |
bool | alignRad (const ofVec2f &vec, float tolerance=0.0001f) const |
Determine if two vectors are aligned with tolerance in radians. | |
Operators | |
ofVec2f | operator+ (const ofVec2f &vec) const |
Super easy vector addition. Returns a new vector (x+vec.x,y+vec.y). | |
ofVec2f | operator+ (const float f) const |
Returns a new vector with a float value f added to both x and y members. | |
ofVec2f & | operator+= (const ofVec2f &vec) |
Super easy addition assignment. Adds vec.x to x, and adds vec.y to y. | |
ofVec2f & | operator+= (const float f) |
Adds a float value f to both x and y members. | |
ofVec2f | operator- (const ofVec2f &vec) const |
Super easy vector subtraction. Returns a new vector (x-vec.x,y-vec.y). | |
ofVec2f | operator- (const float f) const |
Returns a new vector with a float value f subtracted from both x and y members. | |
ofVec2f | operator- () const |
Returns a new ofVec2f that is the inverted version (mirrored in X and Y) of this vector. | |
ofVec2f & | operator-= (const ofVec2f &vec) |
Super easy subtraction assignment. Subtracts vec.x from x, and subtracts vec.y from y. | |
ofVec2f & | operator-= (const float f) |
Subtract a float value f from both x and y members. | |
ofVec2f | operator* (const ofVec2f &vec) const |
Returns a new vector (x*vec.x , y*vec.y). | |
ofVec2f | operator* (const float f) const |
Return a new ofVec2f that is this vector scaled by multiplying both x and y members by the float. | |
ofVec2f & | operator*= (const ofVec2f &vec) |
Multiplies x by vec.x, and multiplies y by vec.y. | |
ofVec2f & | operator*= (const float f) |
Scale this vector by multiplying both x and y members by f. | |
ofVec2f | operator/ (const ofVec2f &vec) const |
Returns a new vector (x/vec.x,y/vec.y). | |
ofVec2f | operator/ (const float f) const |
Return a new ofVec2f that is this vector scaled by dividing both x and y members by f. | |
ofVec2f & | operator/= (const ofVec2f &vec) |
Divides x by vec.x, and divides y by vec.y. | |
ofVec2f & | operator/= (const float f) |
Scale this vector by dividing both x and y members by f. | |
Simple manipulations | |
ofVec2f | getScaled (const float length) const |
ofVec2f & | scale (const float length) |
Scales this vector up or down so that it has the requested length. | |
ofVec2f | getRotated (float angle) const |
Returns a new vector that is the result of rotating this vector by 'angle' degrees about the origin. | |
ofVec2f | getRotated (float angle, const ofVec2f &pivot) const |
Returns a new vector that is the result of rotating this vector by 'angle' degrees about the point 'pivot'. | |
ofVec2f | getRotatedRad (float angle) const |
Returns a new vector that is the result of rotating this vector by 'angle' radians about the origin. | |
ofVec2f | getRotatedRad (float angle, const ofVec2f &pivot) const |
Returns a new vector that is the result of rotating this vector by 'angle' radians about the origin. | |
ofVec2f & | rotate (float angle) |
Rotates this vector by 'angle' degrees about the origin. | |
ofVec2f & | rotate (float angle, const ofVec2f &pivot) |
Rotates this vector by 'angle' degrees about the point 'pivot'. | |
ofVec2f & | rotateRad (float angle) |
Rotates this vector by 'angle' radians about the origin. | |
ofVec2f & | rotateRad (float angle, const ofVec2f &pivot) |
Rotates this vector by 'angle' radians about the point 'pivot'. | |
ofVec2f | getMapped (const ofVec2f &origin, const ofVec2f &vx, const ofVec2f &vy) const |
Get vector mapped to new coordinate system. | |
ofVec2f & | map (const ofVec2f &origin, const ofVec2f &vx, const ofVec2f &vy) |
Maps this vector from its default coordinate system – origin (0,0), X direction (1,0), Y direction (0,1) – to a new coordinate system defined with origin at origin, X direction vx, and Y direction vy. | |
Distance | |
float | distance (const ofVec2f &pnt) const |
Distance between two points. | |
float | squareDistance (const ofVec2f &pnt) const |
Distance between two points squared. | |
Interpolation | |
ofVec2f | getInterpolated (const ofVec2f &pnt, float p) const |
Linear interpolation. | |
ofVec2f & | interpolate (const ofVec2f &pnt, float p) |
Linear interpolation. | |
ofVec2f | getMiddle (const ofVec2f &pnt) const |
Calculate and return the midpoint between this vector and pnt. | |
ofVec2f & | middle (const ofVec2f &pnt) |
Set this vector to the midpoint between itself and pnt. | |
ofVec2f & | average (const ofVec2f *points, std::size_t num) |
Average vector over an array of points. | |
Limit | |
ofVec2f | getNormalized () const |
Returns a normalized copy of this vector. | |
ofVec2f & | normalize () |
Normalize the vector. | |
ofVec2f | getLimited (float max) const |
Get vector limited by length. | |
ofVec2f & | limit (float max) |
Restrict the length (magnitude) of this vector to a maximum of max units by scaling down if necessary. | |
Measurement | |
float | length () const |
Return the length (magnitude) of this vector. | |
float | lengthSquared () const |
Return the squared length (squared magnitude) of this vector. | |
float | angle (const ofVec2f &vec) const |
Calculate the angle to another vector in degrees. | |
float | angleRad (const ofVec2f &vec) const |
Calculate the angle to another vector in radians. | |
Perpendicular | |
ofVec2f | getPerpendicular () const |
Return the normalized ofVec2f that is perpendicular to this vector (ie rotated 90 degrees and normalized). | |
ofVec2f & | perpendicular () |
Set this vector to its own normalized perpendicular (by rotating 90 degrees and normalizing). | |
float | dot (const ofVec2f &vec) const |
Returns the dot product of this vector with 'vec'. | |
Public Attributes | |
float | x |
Stores the x component of the vector. | |
float | y |
Stores the y component of the vector. | |
Detailed Description
ofVec2f is a class for storing a two dimensional vector.
Moving through space requires knowledge of where things are and where they are going. Vector Maths is the class of mathematics that gives us control over these things in space, allowing for elegant and intuitive descriptions of complex structures and movement. Vectors are at the heart of animations, particle systems, and 2D and 3D graphics.
Vectors in mathematics in general are entities with magnitude (also called length) and direction. A vector whose magnitude is 1 (ie a vector that is normalized) is called a unit vector. Unit vectors are very handy for storing directions as they can be easily scaled up (or down) to represent motion in a particular direction with a particular length.
You will also see the term vector used to describe an array of objects in C++ (such as text strings). Don't let this confuse you, they are quite different: one of them is a mathematical term for a fixed-length list of numbers that you can do mathematical operations on, the other is a C++-specific term that means 'dynamically sizeable array'.
ofVec2f has two member variables, x and y, which allow to conveniently store 2D properties of an object such as its position, velocity, or acceleration.
Using ofVec2f greatly simplifies arithmetic operations in two dimensions. For example if you have two vectors v1 and v2, both of which represent a 2D change in position, you can find the total change of position of both of them just by doing an addition v1 + v2:
You can scale an ofVec2f by multiplying it with a float:
This also works for subtraction and division.
As you can see this really makes dealing with vectors as easy as dealing with single floats or ints, and can reduce the number of lines of code you have to write by half, at the same time making your code much easier to read and understand!
Constructor & Destructor Documentation
◆ ofVec2f() [1/8]
ofVec2f::ofVec2f | ( | ) |
Construct a 2D vector.
◆ ofVec2f() [2/8]
|
explicit |
Construct a 2D vector with x
and y
set to scalar
◆ ofVec2f() [3/8]
ofVec2f::ofVec2f | ( | float | x, |
float | y | ||
) |
◆ ofVec2f() [4/8]
ofVec2f::ofVec2f | ( | const ofVec3f & | vec | ) |
◆ ofVec2f() [5/8]
ofVec2f::ofVec2f | ( | const ofVec4f & | vec | ) |
◆ ofVec2f() [6/8]
ofVec2f::ofVec2f | ( | const glm::vec2 & | v | ) |
◆ ofVec2f() [7/8]
ofVec2f::ofVec2f | ( | const glm::vec3 & | v | ) |
◆ ofVec2f() [8/8]
ofVec2f::ofVec2f | ( | const glm::vec4 & | v | ) |
Member Function Documentation
◆ align()
bool ofVec2f::align | ( | const ofVec2f & | vec, |
float | tolerance = 0.0001f |
||
) | const |
Determine if two vectors are aligned.
- Parameters
-
vec The vector to compare alignment with tolerance an angle tolerance/threshold (specified in degrees) for deciding if the vectors are sufficiently aligned.
- Returns
- true if both vectors are aligned (pointing in the same direction).
◆ alignRad()
bool ofVec2f::alignRad | ( | const ofVec2f & | vec, |
float | tolerance = 0.0001f |
||
) | const |
Determine if two vectors are aligned with tolerance in radians.
- Parameters
-
vec The vector to compare alignment with tolerance an angle tolerance/threshold (specified in radians) for deciding if the vectors are sufficiently aligned.
- See also
- align()
◆ angle()
float ofVec2f::angle | ( | const ofVec2f & | vec | ) | const |
◆ angleRad()
float ofVec2f::angleRad | ( | const ofVec2f & | vec | ) | const |
◆ average()
Average vector over an array of points.
Sets this vector to be the average (centre of gravity or centroid) of a given array of ofVec2f.
- Parameters
-
points The array of ofVec2f to avarage over num specifies the number of ofVec2f in the array.
- Returns
- Vector that is the avarage of the points in the array
◆ distance()
float ofVec2f::distance | ( | const ofVec2f & | pnt | ) | const |
Distance between two points.
Treats both this vector and pnt as points in 2D space, and calculates and returns the distance between them.
Distance involves a square root calculation, which is one of the slowest things you can do in programming. If you don't need an exact number but rather just a rough idea of distance (for example when finding the shortest distance of a bunch of points to a reference point, where it doesn't matter exactly what the distances are, you just want the shortest), you can use squareDistance() instead.
- Parameters
-
pnt The point to calculate the distance to
- Returns
- The distance as float
- See also
- squareDistance()
◆ dot()
float ofVec2f::dot | ( | const ofVec2f & | vec | ) | const |
Returns the dot product of this vector with 'vec'.
The dot product of two vectors, also known as the scalar product, is the product of the magnitude of the two vectors and the cosine of the angle between them.
One interpretation of the dot product is as a measure of how closely two vectors align with each other. If they point in exactly the same direction, their dot product will simply be the product of their magnitudes, if they are perpendicular, their dot product will be 0, and if they point in opposite directions, their dot product will be negative.
The dot product is in contrast to the cross product, which returns a vector rather than a scalar.
◆ getInterpolated()
Linear interpolation.
Perform a linear interpolation of this vector's position towards pnt and return the interpolated position without altering the original vector.
p
is normally between 0 and 1 and where 0 means stay the original position and 1 means move all the way to pnt, but you can also have p greater than 1 overshoot pnt, or less than 0 to move backwards away from pnt.
- Parameters
-
pnt The point to move towards p The amount to move towards pnt
- See also
- interpolate()
◆ getLimited()
ofVec2f ofVec2f::getLimited | ( | float | max | ) | const |
Get vector limited by length.
- See also
- limit()
- Parameters
-
max The maximum length of the vector to return
- Returns
- A copy of this vector with its length (magnitude) restricted to a maximum of max units by scaling down if necessary.
◆ getMapped()
Get vector mapped to new coordinate system.
In most cases you want vx
and vy
to be perpendicular and of unit length; if they are not perpendicular you will have shearing as part of the mapping, and if they are not of unit length you will have scaling as part of the mapping.
- Returns
- A new ofVec2f calculated by copying this vector and then mapping from its default coordinate system – origin (0,0), X direction (1,0), Y direction (0,1) – to a new coordinate system defined with origin at origin, X direction vx, and Y direction vy.
◆ getMiddle()
Calculate and return the midpoint between this vector and pnt.
- Parameters
-
pnt The vector to find the middle to
- Returns
- The middle between this vector and
pnt
- See also
- middle()
◆ getNormalized()
ofVec2f ofVec2f::getNormalized | ( | ) | const |
Returns a normalized copy of this vector.
Normalization means to scale the vector so that its length (magnitude) is exactly 1, at which stage all that is left is the direction. A normalized vector is usually called a unit vector, and can be used to represent a pure direction (heading).
◆ getPerpendicular()
ofVec2f ofVec2f::getPerpendicular | ( | ) | const |
Return the normalized ofVec2f that is perpendicular to this vector (ie rotated 90 degrees and normalized).
Image courtesy of Wikipedia
- See also
- perpendicular()
◆ getPtr() [1/2]
|
inline |
Returns a pointer to the memory position of the first element of the vector (x); the second element (y) immediately follows it in memory.
This is very useful when using arrays of ofVec2fs to store geometry information, as it allows the vector to be treated as a simple C array of floats that can be passed verbatim to OpenGL.
◆ getPtr() [2/2]
|
inline |
◆ getRotated() [1/2]
ofVec2f ofVec2f::getRotated | ( | float | angle | ) | const |
Returns a new vector that is the result of rotating this vector by 'angle' degrees about the origin.
- See also
- getRotatedRad()
- rotate()
◆ getRotated() [2/2]
Returns a new vector that is the result of rotating this vector by 'angle' degrees about the point 'pivot'.
◆ getRotatedRad() [1/2]
ofVec2f ofVec2f::getRotatedRad | ( | float | angle | ) | const |
Returns a new vector that is the result of rotating this vector by 'angle' radians about the origin.
◆ getRotatedRad() [2/2]
Returns a new vector that is the result of rotating this vector by 'angle' radians about the origin.
◆ getScaled()
ofVec2f ofVec2f::getScaled | ( | const float | length | ) | const |
◆ interpolate()
Linear interpolation.
Perform a linear interpolation of this vector's position towards pnt. p controls the amount to move towards pnt. p is normally between 0 and 1 and where 0 means stay the original position and 1 means move all the way to pnt, but you can also have p greater than 1 overshoot pnt, or less than 0 to move backwards away from pnt.
- See also
- getInterpolated()
◆ isAligned()
bool ofVec2f::isAligned | ( | const ofVec2f & | vec, |
float | tolerance = 0.0001f |
||
) | const |
Determine if two vectors are aligned.
- Parameters
-
vec The vector to compare alignment with tolerance an angle tolerance/threshold (specified in degrees) for deciding if the vectors are sufficiently aligned.
- Returns
- true if both vectors are aligned (pointing in the same direction).
◆ isAlignedRad()
bool ofVec2f::isAlignedRad | ( | const ofVec2f & | vec, |
float | tolerance = 0.0001f |
||
) | const |
Determine if two vectors are aligned with tolerance in radians.
- Parameters
-
vec The vector to compare alignment with tolerance an angle tolerance/threshold (specified in radians) for deciding if the vectors are sufficiently aligned.
- See also
- isAligned()
◆ length()
float ofVec2f::length | ( | ) | const |
Return the length (magnitude) of this vector.
length() involves a square root calculation, which is one of the slowest things you can do in programming. If you don't need an exact number but rather just a rough idea of a length (for example when finding the shortest distance of a bunch of points to a reference point, where it doesn't matter exactly what the lengths are, you just want the shortest), you can use lengthSquared() instead.
- See also
- lengthSquared()
◆ lengthSquared()
float ofVec2f::lengthSquared | ( | ) | const |
Return the squared length (squared magnitude) of this vector.
Use as a much faster alternative to length() if you don't need to know an accurate length but rather just a rough idea of a length (for example when finding the shortest distance of a bunch of points to a reference point, where it doesn't matter exactly what the lengths are, you just want the shortest). It avoids the square root calculation that is ordinarily required to calculate a length.
- See also
- length()
◆ limit()
ofVec2f & ofVec2f::limit | ( | float | max | ) |
Restrict the length (magnitude) of this vector to a maximum of max units by scaling down if necessary.
- See also
- limit()
◆ map()
Maps this vector from its default coordinate system – origin (0,0), X direction (1,0), Y direction (0,1) – to a new coordinate system defined with origin at origin, X direction vx, and Y direction vy.
In most case you want vx and vy to be perpendicular and of unit length; if they are not perpendicular you will have shearing as part of the mapping, and if they are not of unit length you will have scaling as part of the mapping.
- See also
- perpendicular()
◆ match()
bool ofVec2f::match | ( | const ofVec2f & | vec, |
float | tolerance = 0.0001f |
||
) | const |
Returns true if each component is close enough to its corresponding component in vec, where what is close enough is determined by the value of tolerance:
This is handy if, for example, you want to find out when a point becomes close enough to another point to trigger an event.
◆ middle()
Set this vector to the midpoint between itself and pnt.
- See also
- getMiddle()
◆ normalize()
ofVec2f & ofVec2f::normalize | ( | ) |
Normalize the vector.
Normalizing means to scale the vector so that its length (magnitude) is exactly 1, at which stage all that is left is the direction. A normalized vector is usually called a unit vector, and can be used to represent a pure direction (heading).
- See also
- getNormalized()
◆ operator glm::vec2()
ofVec2f::operator glm::vec2 | ( | ) | const |
◆ operator!=()
bool ofVec2f::operator!= | ( | const ofVec2f & | vec | ) | const |
◆ operator*() [1/2]
ofVec2f ofVec2f::operator* | ( | const float | f | ) | const |
◆ operator*() [2/2]
◆ operator*=() [1/2]
ofVec2f & ofVec2f::operator*= | ( | const float | f | ) |
Scale this vector by multiplying both x and y members by f.
◆ operator*=() [2/2]
◆ operator+() [1/2]
ofVec2f ofVec2f::operator+ | ( | const float | f | ) | const |
◆ operator+() [2/2]
◆ operator+=() [1/2]
ofVec2f & ofVec2f::operator+= | ( | const float | f | ) |
Adds a float value f to both x and y members.
◆ operator+=() [2/2]
◆ operator-() [1/3]
ofVec2f ofVec2f::operator- | ( | ) | const |
◆ operator-() [2/3]
ofVec2f ofVec2f::operator- | ( | const float | f | ) | const |
◆ operator-() [3/3]
◆ operator-=() [1/2]
ofVec2f & ofVec2f::operator-= | ( | const float | f | ) |
Subtract a float value f from both x and y members.
◆ operator-=() [2/2]
◆ operator/() [1/2]
ofVec2f ofVec2f::operator/ | ( | const float | f | ) | const |
◆ operator/() [2/2]
◆ operator/=() [1/2]
ofVec2f & ofVec2f::operator/= | ( | const float | f | ) |
Scale this vector by dividing both x and y members by f.
◆ operator/=() [2/2]
◆ operator==()
bool ofVec2f::operator== | ( | const ofVec2f & | vec | ) | const |
◆ operator[]() [1/2]
|
inline |
Allows to access the x and y components of an ofVec2f as though it is an array.
This function can be handy if you want to do the same operation to both x and y components, as it means you can just make a for loop that repeats twice.
◆ operator[]() [2/2]
|
inline |
◆ perpendicular()
ofVec2f & ofVec2f::perpendicular | ( | ) |
Set this vector to its own normalized perpendicular (by rotating 90 degrees and normalizing).
Image courtesy of Wikipedia
- See also
- getPerpendicular()
◆ rotate() [1/2]
ofVec2f & ofVec2f::rotate | ( | float | angle | ) |
Rotates this vector by 'angle' degrees about the origin.
- See also
- getRotated()
◆ rotate() [2/2]
Rotates this vector by 'angle' degrees about the point 'pivot'.
◆ rotateRad() [1/2]
ofVec2f & ofVec2f::rotateRad | ( | float | angle | ) |
Rotates this vector by 'angle' radians about the origin.
- See also
- getRotatedRad()
◆ rotateRad() [2/2]
Rotates this vector by 'angle' radians about the point 'pivot'.
◆ scale()
ofVec2f & ofVec2f::scale | ( | const float | length | ) |
Scales this vector up or down so that it has the requested length.
- See also
- getScaled()
◆ set() [1/3]
void ofVec2f::set | ( | const ofVec2f & | vec | ) |
◆ set() [2/3]
void ofVec2f::set | ( | float | scalar | ) |
◆ set() [3/3]
void ofVec2f::set | ( | float | x, |
float | y | ||
) |
Set x and y components of this vector with just one function call.
~~~~{.cpp} ofVec2f v1;//#include "ofConstants.h" */ //#include "glm/fwd.hpp" /** v1.set(40, 20); ~~~~
◆ squareDistance()
float ofVec2f::squareDistance | ( | const ofVec2f & | pnt | ) | const |
Distance between two points squared.
Treats both this vector and pnt as points in 2D space, and calculates and returns the squared distance between them.
Use as a much faster alternative to distance if you don't need to know an exact number but rather just a rough idea of distance (for example when finding the shortest distance of a bunch of points to a reference point, where it doesn't matter exactly what the distances are, you just want the shortest). It avoids the square root calculation that is ordinarily required to calculate a length.
- Returns
- The distance squared as float
- See also
- distance()
Member Data Documentation
◆ x
float ofVec2f::x |
Stores the x
component of the vector.
◆ y
float ofVec2f::y |
Stores the y
component of the vector.
The documentation for this class was generated from the following files:
- /Users/icq4ever/Desktop/oF0120/libs/openFrameworks/math/ofVec2f.h
- /Users/icq4ever/Desktop/oF0120/libs/openFrameworks/math/ofVec2f.cpp
- /Users/icq4ever/Desktop/oF0120/libs/openFrameworks/math/ofVec4f.cpp