Vectors are 1D lists of numbers, grouped to enable higher level operations. They represent magnitude and direction in a vector space. Each number, or component, is a distance along a basis vector in the vector space. Vectors can also describe points, as the direction and magnitude from the origin.
3D vectors, which have three components, are common in 3D computer graphics. With a homogeneous coordinate system, an additional element may be used to define the vector as being directional or positional. A direction vector $(x, y, z, 0)$ will ignore translation during 4×4 homogeneous matrix multiply. A use case is transforming tangent vectors by a model matrix. A position vector $(x, y, z, 1)$ will scale the homogeneous matrix multiply translation by one.
Operations
- Addition: $(a_1, a_2, a_3) + (b_1, b_2, b_3) \equiv (a_1 + b_1, a_2 + b_2, a_3 + b_3)$
- Subtraction: $(a_1, a_2, a_3) - (b_1, b_2, b_3) \equiv (a_1 - b_1, a_2 - b_2, a_3 - b_3)$
- Scaling: $s (a_1, a_2, a_3) \equiv (s a_1, s a_2, s a_3)$
- Although not used in mathematics, multiplying two vectors is a common convenience function provided by a number of programming libraries:
vec3(a1, a2, a3) * vec3(b1, b2, b3) == vec3(a1*b1, a2*b2, a3*b3)
. Although vector multiplication normally refers to the dot product, denoted with a $\cdot$.
The magnitude, or length, of any dimensional vector is the square root of the sum of the elements squared.
Normalizing a vector, to make it unit length and keep just its direction is done by scaling the components to divide by the length (the bold $\mathbf{a}$ represents a vector):
Dot Product
The dot product, also called the scalar or inner product, gives a scalar. It is not easily relatable geometrically but introduces the cosine of the angle between vectors which has some very useful properties.
This is less interesting until the magnitudes $|\mathbf{a}|$ and $|\mathbf{b}|$ are removed. An immediate relation to the angle $\theta$ can be seen. Due to the expense of trig functions, this is not always that useful. In many cases, explicit angles can be avoided entirely.
$\cos(\theta)$ is quite useful to determine if two vectors are pointing in the same direction, being 1 for the same direction, 0 for right angles and -1 for opposite. It is also used directly as a diffuse lighting coefficient.
A far more common use of the dot product is scalar projection. $\cos(\theta)$ gives a ratio between the opposite and hypotenuse sides of a right angle triangle. Scaling by $|\mathbf{a}|$ gives the adjacent side length. This gives the distance in the direction of $\mathbf{b}$, an arbitrary vector, that $\mathbf{a}$ travels, unlike the components of $\mathbf{a}$ which give the distance along the basis vectors $x, y, z$.
Scalar projection is used in the introduction of transformation matrices.
Cross Product
The cross product $\times$ of two vectors is another vector, unlike the dot product, perpendicular to the both of the others. Its length is equal to $|\mathbf{a}| |\mathbf{b}| \sin(\theta)$. The order of $\mathbf{a}$ and $\mathbf{b}$ and the space handedness determines which way the result faces.
A common use is to calculate normals for polygonal meshes as the cross product of two tangent vectors gives a normal. $|\mathbf{a} \times \mathbf{b}|$ also gives the area of a parallelogram formed by $\mathbf{a}$ and $\mathbf{b}$, and half that is the area of the triangle. Due to the change in sign, the cross product can be used to find which side of a plane a point lies. Although the cross product is not defined in 2D, the determinant
A combination of the dot and cross product to find $\cos(\theta)$ and $\sin(\theta)$ with the correct sign is useful in combination with the function atan2
to find $\theta$ over a full circle.
Vector Space
A vector space is a space in which vectors exist. A linear vector space can also be defined by basis vectors. For example, a vector $(1, 2, 3)$ defines magnitudes along the axes $x, y, z$, which form the standard basis in $\mathbb{R}^3$. In other words, the vector $(1, 2, 3)$ represents $1x + 2y + 3z$ where $x, y, z$ are $(1, 0, 0), (0, 1, 0), (0, 0, 1)$.
The same elements forming a vector (1, 2, 3) in another space may be calculated in the standard basis given a mapping between the spaces. This discussion is continued in matrices.
Handedness
The handedness, or orientation, of a 3D space is determined by the ordering of the basis vectors. Think of a 2D Cartesian coordinate system drawn on a piece of paper. $+x$ normally points right and $+y$ up. When introducing $z$ is it natural to think of it as pointing up, coming out of the paper towards you. This is a right handed coordinate system. With your right hand open, the thumb points towards $+x$, fingers $+y$ and when closing your hand the fingers point towards $+z$. This is a more common coordinate system used by many modelling applications and OpenGL, whereas DirectX is left handed. The choice is somewhat arbitrary, but keeping consistency can avoid having to convert between different systems all the time.
The choice of which axis is considered “up” is also arbitrary. In the paper example above, $+z$ is an obvious choice. However our computer screens are normally in front of us. With $+x$ still pointing right and $+y$ being up. With a right handed coordinate system, $+z$ now points towards us, coming out of the screen.