[02 Nov 2012]

# Vectors in Computer Graphics

Vectors

Vectors have direction and magnitude. Number of components in a vector corresponds to number of dimensions. Scalar can be treated as speed or distance, and vector as velocity or offset with direction. Depending on requirements, vector can be written as row-vector or as column-vector (for example, for proper matrix multiplication):

$\vec{a}=\begin{vmatrix}x & y & z \end{vmatrix}$              $\vec{a }^{T} = \begin{vmatrix}x\\ y \\ z\end{vmatrix}$

Length (magnitude) of the vector:

$\left \| \vec{a} \right \| = \sqrt{ \sum_{i=1}^{n}a_{i}^{ 2} }$

Normalized vector (vector with lenght equal to 1, unit vector):

$\hat{a}=\frac{\vec{a}}{\left \| a \right \|}$

Magnitude of difference between vectors:

$d=\left \| \vec{a}-\vec{b} \right \|$

Dot product of vectors

Dot (inner) product of vectors is one of the most usefull and used operations in computer graphics. It is equal to sum of products of corresponding components of the vectors. Result is scalar value:

$\vec{a}\cdot \vec{b}=\sum_{i=1}^{n}a_{i}b_{i}=a_{1}b_{1}+...+a_{n}b_{n}$

Dot product can be interpreted as cosine of angle between two vectors, or as projection of one vector on another. These interpretations are very related. Lets look on vectors a and b:

Cosine of angle between vectors and angle are calculated as:

$cos(\theta )=\frac{\vec{a}\cdot \vec{b}}{\left \| \vec{a} \right \|\left \| \vec{b} \right \|}$
$cos(\theta )=\hat{a}\cdot \hat{b}$
$\theta&space;=acos(\hat{a}\cdot \hat{b})$

Length of projection of vector a on vector b and projected vector e are equal to:

$\left \| e \right \|=\vec{a} \cdot \vec{b}$
$\vec{e}=(\vec{a}\cdot \vec{b})\hat{b}$

Properties of dot product:

• dot product has higher priority than addition and substraction
• $a \cdot b = 0 \Rightarrow a\perp b$
• $a \cdot b = 1 \Rightarrow a \parallel b$
• $a \cdot b = -1 \Rightarrow a \parallel b$
• $a \cdot b > 0 \Rightarrow nearly \; same\;direction$
• $a \cdot b < 0 \Rightarrow nearly \; opposite\;direction$
• $a\cdot a=\left \| a \right \|^{2}$
• $a\cdot b=b\cdot a$
• $k(a\cdot b)=a\cdot (kb)$
• $a\cdot (b+c)=a\cdot b+a\cdot c$

Cross product of vectors

Cross product is another usefull operation in computer graphics. It's only defined for 3-dimensional space. Result of cross product of two vectors is vector that is perpendicular to them. This property is used for calculation of normals, orthogonalization of vectors, etc. Lets look at vectors a and b:

Vector c is equal to:

$\vec{c}=\vec{a}\times \vec{b}=\begin{vmatrix}a_{x} \\ a_{y} \\ a_{z}\end{vmatrix}\times \begin{vmatrix}b_{x} \\ b_{y} \\ b_{z}\end{vmatrix}=\begin{vmatrix} a_{y}b_{z}-a_{z}b_{y} \\ a_{z}b_{x}-a_{x}b_{z} \\ a_{x}b_{y}-a_{y}b_{ x} \end{vmatrix}$

Vectors a and b don't have to be perpendicular. Magnitude of vector c is equal to area of parallelogram S that is formed by vectors a and b. If vectors a and b are unit vectors, then magnitude of c will be equal to sine of angle between vectors a and b:

$S = \left \| \vec{a} \times \vec{b} \right \|$
$sin(\theta ) = \frac{\left \| \vec{a} \times \vec{b} \right \|}{\left \| \vec{a} \right \| \left \| \vec{b} \right \|}$

Direction of vector c depends on order of vectors in cross product. For example, in right-handed coordinate system, direction of vector c can be determined by right-hand rule. If index finger points in direction of vector a and middle finger in direction of vector b, then thumb will determine direction of vector c. For left-handed coordinate system you can use your left-hand rule. If the order of vectors in cross product is changed, then vector c will point in opposite direction.

Cross product has higher priority than addition, substraction and dot product.

Other properties of cross product:

• $a \times b=- (b \times a)$
• $a \times b=(-a) \times (-b)$
• $a \times (b+c) = a \times b + a \times c$
• $(a \times b)\times c \neq a \times (b\times c)$
• $a \times a = 0$
• $a \parallel b \Rightarrow a \times b = 0$

Scalar triple product of vectors

Triple product of vectors is equal to volume of parallelepiped that is formed by vectors a, b and c:

$V = \vec{c}\cdot (\vec{a}\times \vec{b})$

Also, triple product is equal to determinant of a matrix that is formed form vectors a, b and c:

$\vec{c} \cdot (\vec{a} \times \vec{b})=det \begin{bmatrix}a_{x} & a_{y} & a_{z} \\ b_{x} & b_{y} & b_{z} \\ c_{x} & c_{y} & c_{z} \end{bmatrix}$

Properties of triple product:

• If two vectors (of a, b or c) are parallel, then result is equal to 0.
• If vectors a, b and c lie on same plane, then result is equal to 0.

Sun and Black Cat- Igor Dykhta () © 2007-2014