Vector subtraction

Like vector addition, vector subtraction also measures the combined displacement of two vectors. Also similar to vector addition, vector subtraction is a component wise operation. To subtract \(\vec{B}\) from \(\vec{A}\), subtract each component of \(\vec{B}\) from each component of \(\vec{A}\)

$$ \vec{A} - \vec{B} = (A_0 - B_0, A_1 - B_1 ... A_n - B_n) $$

When subtracting vectors \(\vec{A} - \vec{B}\), the resulting vector points towards the vector being subtracted from, \(\vec{A}\). This may look unintuitive at first, it makes more sense when thought about as \(\vec{A} + (-\vec{B})\) instead.

To subtract vector \(\vec{A}\) from \(\vec{B}\) visually, draw a new vector starting at the tip of \(\vec{B}\) pointing to the tip of \(\vec{A}\).

Canvas support required

In the demo above, there are two vectors, the blue vector \(\vec{A}\) and the green vector \(\vec{B}\). The result of the subtraction is the orange vector. There is a dashed organge vector, which is also the result of the subtraction just drawn at a different point. When thinking of subtraction, usually we expect this dashed result.

Implementing vector subtraction in code is trivial:

vec2 Sub(vec2 a, vec2 b) {
    return vec2(a.x - b.x, a.y - b.y);
}

vec3 Sub(vec3 a, vec3 b) {
    return vec3(a.x - b.x, a.y - b.y, a.z - b.z);
}

vec4 Sub(vec4 a, vec4 b) {
    return vec4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
}