If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

# More vector math

Addition was really just the first step. There are many mathematical operations that are commonly used with vectors. Below is a comprehensive list of the operations available as functions in the PVector object from ProcessingJS. We’ll go through a few of the key ones now. As our examples get more sophisticated in later sections, we’ll continue to reveal the details of more functions.

### Vector Subtraction

w, with, vector, on top, equals, u, with, vector, on top, minus, v, with, vector, on top
can be written as:
start text, w, end text, start subscript, x, end subscript, equals, start text, u, end text, start subscript, x, end subscript, minus, start text, v, end text, start subscript, x, end subscript
start text, w, end text, start subscript, y, end subscript, equals, start text, u, end text, start subscript, y, end subscript, minus, start text, v, end text, start subscript, y, end subscript
and so the function inside PVector looks like:
PVector.prototype.sub = function(vector2) {
this.x = this.x - vector2.x;
this.y = this.y - vector2.y;
};
The following example demonstrates vector subtraction by taking the difference between two points (the mouse location and the center of the window).
Basic Number Properties with Vectors
When we're doing math with real numbers, they obey these basic rules:
The commutative rule: 3, plus, 2, equals, 2, plus, 3
The associative rule: left parenthesis, 3, plus, 2, right parenthesis, plus, 1, equals, 3, plus, left parenthesis, 2, plus, 1, right parenthesis
Those same rules are true of math with vectors:
The commutative rule: u, with, vector, on top, plus, v, with, vector, on top, equals, v, with, vector, on top, plus, u, with, vector, on top
The associative rule: u, with, vector, on top, plus, left parenthesis, v, with, vector, on top, plus, w, with, vector, on top, right parenthesis, equals, left parenthesis, u, with, vector, on top, plus, v, with, vector, on top, right parenthesis, plus, w, with, vector, on top

### Vector Multiplication

Moving on to multiplication, we have to think a little bit differently. When we talk about multiplying a vector, what we typically mean is scaling a vector. If we wanted to scale a vector to twice its size or one-third of its size (leaving its direction the same), we would say: “Multiply the vector by 2” or “Multiply the vector by 1/3.” Note that we are multiplying a vector by a scalar, a single number, not another vector.
To scale a vector, we multiply each component (x and y) by a scalar.
w, with, vector, on top, equals, u, with, vector, on top, times, n
can be written as:
$w_x = u_x * n \\ w_y = u_y * n$
Scaling a vector
Let’s look at an example with vector notation.
$\vec{u} = (-3, 7) \\ n = 3 \\ \vec{w} = \vec{u} * n \\ w_x = -3 * 3 \\ w_y = 7 * 3 \\ \vec{w} = (-9, 21)$
Therefore, the function inside the PVector object is written as:
PVector.prototype.mult = function(n) {
this.x = this.x * n;
this.y = this.y * n;
}
And using mult in code is as simple as:
var u = new PVector(-3,7);
// This PVector is now three times the size and is equal to (-9,21).
u.mult(3);
Here's the example from earlier, but we multiply the vector by 0.5 each time, so that it's scaled by half:
Instead of multiplying by 0.5 above, we could have also divided by 2. Division works just like multiplication—we simply replace the multiplication sign (asterisk) with the division sign (forward slash).
This is how the div method is implemented internally:
PVector.prototype.div = function(n) {
this.x = this.x / n;
this.y = this.y / n;
}
And this is how we can use it in code:
var u = new PVector(8, -4);
u.div(2);

### More Number Properties with Vectors

As with addition, basic algebraic rules of multiplication apply to vectors.
The associative rule: left parenthesis, n, times, m, right parenthesis, times, v, with, vector, on top, equals, n, times, left parenthesis, m, times, v, with, vector, on top, right parenthesis
The distributive rule with 2 scalars, 1 vector: left parenthesis, n, plus, m, right parenthesis, times, v, with, vector, on top, equals, n, times, v, with, vector, on top, plus, m, times, v, with, vector, on top
The distributive rule with 2 vectors, 1 scalar: left parenthesis, u, with, vector, on top, plus, v, with, vector, on top, right parenthesis, times, n, equals, u, with, vector, on top, times, n, plus, v, with, vector, on top, times, n