Skip to content

Vectors in 2D and 3D

Pillar: SHAPE — "Vectors encode direction and magnitude; dot and cross products extract angles and areas."


What Is a Vector?

A vector is a quantity with both direction and magnitude. Contrast this with a scalar, which is just a number (like temperature or mass).

Visually, a vector is an arrow. Two arrows with the same direction and length represent the same vector, regardless of where they start.

Notation

A 2D vector can be written as:

\[\vec{v} = (v_x, v_y) = \begin{pmatrix} v_x \\ v_y \end{pmatrix}\]

The components \(v_x\) and \(v_y\) are the horizontal and vertical displacements.

Vector vs Point

A point \(P = (3, 4)\) is a location. A vector \(\vec{v} = (3, 4)\) is a displacement. They use the same notation but mean different things. The vector from point \(A\) to point \(B\) is:

\[\vec{AB} = B - A = (b_x - a_x, \, b_y - a_y)\]

Basic Operations

Addition

Add component-wise. Geometrically, place the tail of \(\vec{b}\) at the head of \(\vec{a}\):

\[\vec{a} + \vec{b} = (a_x + b_x, \, a_y + b_y)\]

Subtraction

\[\vec{a} - \vec{b} = (a_x - b_x, \, a_y - b_y)\]

\(\vec{a} - \vec{b}\) points from the head of \(\vec{b}\) to the head of \(\vec{a}\) (when both start at the origin).

Scalar Multiplication

\[c \cdot \vec{a} = (c \cdot a_x, \, c \cdot a_y)\]
  • \(c > 1\): stretches the vector
  • \(0 < c < 1\): shrinks the vector
  • \(c < 0\): reverses direction and scales

Magnitude

The magnitude (length) of \(\vec{v} = (v_x, v_y)\) is:

\[|\vec{v}| = \sqrt{v_x^2 + v_y^2}\]

This is just the distance from the origin to the point \((v_x, v_y)\).

Unit Vectors

A unit vector has magnitude \(1\). To make any non-zero vector into a unit vector:

\[\hat{v} = \frac{\vec{v}}{|\vec{v}|}\]

The standard unit vectors are \(\hat{i} = (1, 0)\) and \(\hat{j} = (0, 1)\), so:

\[\vec{v} = v_x \hat{i} + v_y \hat{j}\]

The Dot Product

The dot product (inner product) of \(\vec{a} = (a_x, a_y)\) and \(\vec{b} = (b_x, b_y)\) is:

\[\vec{a} \cdot \vec{b} = a_x b_x + a_y b_y\]

Geometric Interpretation

\[\vec{a} \cdot \vec{b} = |\vec{a}| \, |\vec{b}| \cos\theta\]

where \(\theta\) is the angle between the vectors.

What the Dot Product Tells You

Value of \(\vec{a} \cdot \vec{b}\) Angle \(\theta\) Relationship
Positive \(0° < \theta < 90°\) Vectors point roughly the same way
Zero \(\theta = 90°\) Vectors are perpendicular
Negative \(90° < \theta < 180°\) Vectors point roughly opposite

Finding the Angle

\[\cos\theta = \frac{\vec{a} \cdot \vec{b}}{|\vec{a}| \, |\vec{b}|}\]
\[\theta = \arccos\left(\frac{\vec{a} \cdot \vec{b}}{|\vec{a}| \, |\vec{b}|}\right)\]

Example

\(\vec{a} = (3, 4)\), \(\vec{b} = (-4, 3)\).

\(\vec{a} \cdot \vec{b} = 3(-4) + 4(3) = -12 + 12 = 0\)

The dot product is zero, so these vectors are perpendicular.


Projection

The projection of \(\vec{a}\) onto \(\vec{b}\) decomposes \(\vec{a}\) into a component along \(\vec{b}\) and a component perpendicular to \(\vec{b}\).

Scalar Projection

The signed length of \(\vec{a}\)'s shadow along \(\vec{b}\):

\[\text{comp}_{\vec{b}}(\vec{a}) = \frac{\vec{a} \cdot \vec{b}}{|\vec{b}|}\]

Vector Projection

The actual vector along \(\vec{b}\):

\[\text{proj}_{\vec{b}}(\vec{a}) = \frac{\vec{a} \cdot \vec{b}}{|\vec{b}|^2} \cdot \vec{b} = \frac{\vec{a} \cdot \vec{b}}{\vec{b} \cdot \vec{b}} \cdot \vec{b}\]

Example

Project \(\vec{a} = (3, 4)\) onto \(\vec{b} = (1, 0)\) (the \(x\)-axis).

\[\text{proj}_{\vec{b}}(\vec{a}) = \frac{3 \cdot 1 + 4 \cdot 0}{1} \cdot (1, 0) = 3 \cdot (1, 0) = (3, 0)\]

The projection onto the \(x\)-axis is just the \(x\)-component. Makes sense.


3D Vectors

Everything extends naturally. A 3D vector is:

\[\vec{v} = (v_x, v_y, v_z)\]
  • Addition, subtraction, scalar multiplication: still component-wise
  • Magnitude: \(|\vec{v}| = \sqrt{v_x^2 + v_y^2 + v_z^2}\)
  • Dot product: \(\vec{a} \cdot \vec{b} = a_x b_x + a_y b_y + a_z b_z\)
  • Angle formula: same as 2D

The standard unit vectors are \(\hat{i} = (1,0,0)\), \(\hat{j} = (0,1,0)\), \(\hat{k} = (0,0,1)\).


The Cross Product (3D Only)

The cross product \(\vec{a} \times \vec{b}\) is defined only in 3D. It produces a vector (not a scalar) that is perpendicular to both \(\vec{a}\) and \(\vec{b}\).

Formula

\[\vec{a} \times \vec{b} = \begin{pmatrix} 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{pmatrix}\]

A convenient way to remember this: the cross product is the determinant of:

\[\vec{a} \times \vec{b} = \det \begin{pmatrix} \hat{i} & \hat{j} & \hat{k} \\ a_x & a_y & a_z \\ b_x & b_y & b_z \end{pmatrix}\]

Magnitude

\[|\vec{a} \times \vec{b}| = |\vec{a}| \, |\vec{b}| \sin\theta\]

This equals the area of the parallelogram formed by \(\vec{a}\) and \(\vec{b}\).

Direction: Right-Hand Rule

Point your fingers along \(\vec{a}\), curl them toward \(\vec{b}\). Your thumb points in the direction of \(\vec{a} \times \vec{b}\).

Key Properties

  • Anti-commutative: \(\vec{a} \times \vec{b} = -(\vec{b} \times \vec{a})\)
  • Not associative: \(\vec{a} \times (\vec{b} \times \vec{c}) \ne (\vec{a} \times \vec{b}) \times \vec{c}\) in general
  • \(\vec{a} \times \vec{a} = \vec{0}\)
  • \(\vec{a} \times \vec{b} = \vec{0}\) if and only if \(\vec{a}\) and \(\vec{b}\) are parallel

2D Cross Product

In 2D, we define a "cross product" that returns a scalar (the \(z\)-component of the 3D cross product when \(z = 0\)):

\[\vec{a} \times \vec{b} = a_x b_y - a_y b_x\]

Geometric Meaning

  • The absolute value is the area of the parallelogram: \(|\vec{a} \times \vec{b}| = |\vec{a}||\vec{b}|\sin\theta\)
  • Positive: \(\vec{b}\) is counterclockwise from \(\vec{a}\)
  • Negative: \(\vec{b}\) is clockwise from \(\vec{a}\)
  • Zero: \(\vec{a}\) and \(\vec{b}\) are parallel (or one is zero)

Connection to Shoelace Formula

The Shoelace formula for a triangle with vertices \(A\), \(B\), \(C\) is:

\[\text{Area} = \frac{1}{2} |\vec{AB} \times \vec{AC}|\]

This is exactly the 2D cross product divided by 2. The Shoelace formula for a general polygon is a sum of these cross products.


Applications in Competitive Programming

Orientation Test

Given three points \(P\), \(Q\), \(R\), determine if the turn from \(PQ\) to \(QR\) is left (counterclockwise), right (clockwise), or straight (collinear):

\[\text{orient} = \vec{PQ} \times \vec{QR} = (Q_x - P_x)(R_y - Q_y) - (Q_y - P_y)(R_x - Q_x)\]
  • Positive → left turn (CCW)
  • Negative → right turn (CW)
  • Zero → collinear

This is the building block of convex hull algorithms, polygon inclusion tests, and segment intersection checks.

Triangle Area

\[\text{Area} = \frac{1}{2} |\vec{AB} \times \vec{AC}|\]

No need for base and height — just coordinates.


Practice Problems

Problem 1. Compute \(\vec{a} \cdot \vec{b}\) for \(\vec{a} = (2, -3)\) and \(\vec{b} = (4, 1)\).

Solution

\(\vec{a} \cdot \vec{b} = 2(4) + (-3)(1) = 8 - 3 = 5\).

Problem 2. Find the angle between \(\vec{a} = (1, 1)\) and \(\vec{b} = (1, 0)\).

Solution

\(\cos\theta = \frac{1 \cdot 1 + 1 \cdot 0}{\sqrt{2} \cdot 1} = \frac{1}{\sqrt{2}}\), so \(\theta = 45°\).

Problem 3. Compute the area of the triangle with vertices \((0,0)\), \((3,0)\), \((1,4)\).

Solution

\(\vec{AB} = (3,0)\), \(\vec{AC} = (1,4)\). Cross product: \(3 \cdot 4 - 0 \cdot 1 = 12\).

Area \(= \frac{12}{2} = 6\).

Problem 4. Are the points \((1, 2)\), \((3, 6)\), \((5, 10)\) collinear?

Solution

\(\vec{AB} = (2, 4)\), \(\vec{AC} = (4, 8)\). Cross product: \(2 \cdot 8 - 4 \cdot 4 = 0\).

Yes, collinear (cross product is zero).

Problem 5. Project \(\vec{a} = (5, 2)\) onto \(\vec{b} = (3, 4)\).

Solution

\(\vec{a} \cdot \vec{b} = 15 + 8 = 23\). \(\vec{b} \cdot \vec{b} = 9 + 16 = 25\).

\(\text{proj}_{\vec{b}}(\vec{a}) = \frac{23}{25}(3, 4) = \left(\frac{69}{25}, \frac{92}{25}\right) = (2.76, 3.68)\).

Problem 6. Compute \((1, 2, 3) \times (4, 5, 6)\).

Solution
\[\vec{a} \times \vec{b} = \begin{pmatrix} 2 \cdot 6 - 3 \cdot 5 \\ 3 \cdot 4 - 1 \cdot 6 \\ 1 \cdot 5 - 2 \cdot 4 \end{pmatrix} = \begin{pmatrix} -3 \\ 6 \\ -3 \end{pmatrix}\]