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:
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:
Basic Operations
Addition
Add component-wise. Geometrically, place the tail of \(\vec{b}\) at the head of \(\vec{a}\):
Subtraction
\(\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 > 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:
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:
The standard unit vectors are \(\hat{i} = (1, 0)\) and \(\hat{j} = (0, 1)\), so:
The Dot Product
The dot product (inner product) of \(\vec{a} = (a_x, a_y)\) and \(\vec{b} = (b_x, b_y)\) is:
Geometric Interpretation
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
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}\):
Vector Projection
The actual vector along \(\vec{b}\):
Example
Project \(\vec{a} = (3, 4)\) onto \(\vec{b} = (1, 0)\) (the \(x\)-axis).
The projection onto the \(x\)-axis is just the \(x\)-component. Makes sense.
3D Vectors
Everything extends naturally. A 3D vector is:
- 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
A convenient way to remember this: the cross product is the determinant of:
Magnitude
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\)):
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:
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):
- 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
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)\).