Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Vectors are an extremely useful geometric object in maths and physics. Vectors express a direction and a magnitude (or length), in both 2D and 3D space. Vectors are key concept not only for producing geometry and form through code, but also for motion and interactive animation. 

(Diagram 1.  Euclidean vector)

 

 

Vectors are normally represented as The direction of a vector is simply represented with an arrow, where with the length expresses of the arrow expressing the magnitude. The information to create a 2D vector can be easily recorded with just two numbers, which can be negative or positive.

a = ( 4, 8 )  

(Diagram 2.  vector a)

 

Vectors The key thing to remember is that vectors represent direction and magnitude without a location. For this reason vectors are often combined with a coordinate. Confusingly, 2D coordinates are also generally recorded in the same way format as vectors, with just two numbers. However, coordinates are normally visually represented as pointpoints.  

 

(Diagram 3.  vector with a with coordinate, and a translated to point b)

 

There are a number of ways to manipulate and combine vectors with simple operations (vector maths).

...

If you add to vectors together, the resulting vector is the equivalent of stacking the vectors end to end. Adding vectors together can be used to simulate simple physics like the effect of wind or gravity. If a character in a game jumps while moving forward, then we will want to add the vectors of the upward momentum with the forward motion. 

a = ( 4, 8 )  

b = ( 6, -3 )

...

(Diagram 4.  vector addition)

Subtraction

Subtracting two vectors

Multiplication

Multiplication only effects the magnitude of the vector, leaving the direction unchanged... However, if the vector is multiplied by a negative, then the direction is reversed and the magnitude is unchanged.  ! Multiplying vectors could be used to control the acceleration of space ship, without changing it's direction. If an object in a game collides with wall, we may want to multiply the objects vector by -1, to reverse it's direction so that it bounces off the surface.

 

(Diagram 5.  vector multiplication)

...

(Diagram 6.  vector negative multiplication)

Subtraction

Subtracting two vectors 


Normalising

A normalised vector has had its magnitude set to one, with the direction left unchanged.

...

You can find the magnitude (the length) with this using Pythagorean formulatheorem:

(Diagram 7.  sqrt(x^2 + y^2))

...

Processing has a vector class called ‘PVector‘, which can be two or three dimensions. Basic vector maths are included in a number of methods of the PVector class.

 

Code Block
PVector vec1 = new PVector(1,2);
PVector vec2 = new PVector(3,4);
 
println(vec1.x);
println(vec1.y);
println(vec1.z);
 
println(vec1);
println(vec2);
 
vec1.set(10,20);
println(vec1);
 
println("-- Vec Add --");
PVector resVec = PVector.add(vec1,vec2);
println(resVec);
 
vec1.add(vec2);
println(vec1);
 
println("-- Vec Sub --");
resVec = PVector.sub(vec1,vec2);
println(resVec);
 
vec1.sub(vec2);
println(vec1);
 
println("-- Vec Mult --");
resVec = PVector.mult(vec1,2);
println(resVec);
 
vec1.mult(2);
println(vec1);
 
println("-- Vec Length --");
println(vec1.mag());
 
println("-- Vec Angle Between --");
println(degrees(PVector.angleBetween(vec1, vec2)));
 
println("-- Vec Normalize --");
vec1.normalize();
println(vec1);
println(vec1.mag());