

The dot product is in contrast to the cross product, which returns a If they point in opposite directions, their dot product will be Magnitudes, if they are perpendicular, their dot product will be 0, and If they point in exactly the sameĭirection, their dot product will simply be the product of their One interpretation of the dot product is as a measure of how closely two The product of the magnitude of the two vectors and the cosine of the The dot product of two vectors, also known as the scalar product, is Returns the dot product of this vector with 'vec'.
#Rotate a vector 2d code
This also works for subtraction and division.Īs 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!

OfVec2f result = v1 * 3 // result is 15 steps forward and 6 steps right You can scale an ofVec2f by multiplying it with a float: ofVec2f v1(5, 2) // walk 5 steps forward and 2 steps right OfVec2f result = v1 + v2 // result is 6 steps forward then 3 steps sideways what happens if you do v1 followed by v2? just add v1 and v2 together: V2.set(1, 1) // v2 represents walking 1 step forward then 1 step sideways 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: ofVec2f v1(5, 2) // v1 represents walking 5 steps forward then 2 steps sideways Using ofVec2f greatly simplifies arithmetic operations in two dimensions. 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. 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'. You will also see the term vector used to describe an array of objects in C++ (such as text strings). 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. A vector whose magnitude is 1 (ie a vector that is normalized) is called a unit vector. Vectors in mathematics in general are entities with magnitude (also called length) and direction.

Vectors are at the heart of animations, particle systems, and 2D and 3D graphics. 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. Moving through space requires knowledge of where things are and where they are going. OfVec2f is a class for storing a two dimensional vector.
