TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › Vectors in Geometry

Vectors in Geometry

A vector is an arrow with a length and a direction, but no fixed starting point. In coordinates it is a pair of numbers such as (4,3)(4, 3): "4 right, 3 up". Vectors turn geometry into arithmetic, and they are the everyday currency of game and graphics code: positions, velocities, directions and normals are all vectors.

Vectors from points

The vector from point AA to point BB is AB→=B−A\overrightarrow{AB} = B - A, subtracting coordinates. A point says where; a vector says how to get from one place to another.

Operation Formula for u=(u1,u2)\mathbf{u} = (u_1, u_2), v=(v1,v2)\mathbf{v} = (v_1, v_2) Geometric meaning
addition u+v=(u1+v1,u2+v2)\mathbf{u} + \mathbf{v} = (u_1 + v_1, u_2 + v_2) follow u\mathbf{u}, then v\mathbf{v} (head to tail)
subtraction u−v\mathbf{u} - \mathbf{v} the arrow from the tip of v\mathbf{v} to the tip of u\mathbf{u}
scalar multiple ku=(ku1,ku2)k\mathbf{u} = (ku_1, ku_2) stretch by kk; a negative kk flips the direction
length (magnitude) ∥u∥=u12+u22\lVert \mathbf{u} \rVert = \sqrt{u_1^2 + u_2^2} the Pythagorean theorem
unit vector u/∥u∥\mathbf{u} / \lVert \mathbf{u} \rVert same direction, length 1

Two vectors are parallel when one is a scalar multiple of the other.

python · try it yourself
import math

A, B = (1, 2), (5, 5)
AB = (B[0] - A[0], B[1] - A[1])
length = math.hypot(*AB)
unit = (AB[0] / length, AB[1] / length)
print("AB =", AB, " |AB| =", length, " unit =", unit)

u, v = (2, 1), (-1, 3)
print("u + v =", (u[0] + v[0], u[1] + v[1]))
print("3u    =", (3 * u[0], 3 * u[1]))
print("A + 0.5·AB =", (A[0] + 0.5 * AB[0], A[1] + 0.5 * AB[1]), "(the midpoint of AB)")
AB = (4, 3)  |AB| = 5.0  unit = (0.8, 0.6)
u + v = (1, 4)
3u    = (6, 3)
A + 0.5·AB = (3.0, 3.5) (the midpoint of AB)

The last line is the start of linear interpolation: A+t AB→A + t\,\overrightarrow{AB} walks from AA (t=0t = 0) to BB (t=1t = 1). See Lerp.

The dot product: angles and perpendiculars

u⋅v=u1v1+u2v2=∥u∥⋅∥v∥⋅cos⁡θ\mathbf{u} \cdot \mathbf{v} = u_1 v_1 + u_2 v_2 = \lVert \mathbf{u} \rVert \cdot \lVert \mathbf{v} \rVert \cdot \cos\theta

where θ\theta is the angle between the vectors. So:

  • u⋅v=0\mathbf{u} \cdot \mathbf{v} = 0 means the vectors are perpendicular;
  • u⋅v>0\mathbf{u} \cdot \mathbf{v} > 0 means the angle is acute (they point roughly the same way); u⋅v<0\mathbf{u} \cdot \mathbf{v} < 0 means it is obtuse.
  • The projection of u\mathbf{u} onto v\mathbf{v}, the "shadow" of u\mathbf{u} along v\mathbf{v}, is (u⋅vv⋅v)v\left(\frac{\mathbf{u} \cdot \mathbf{v}}{\mathbf{v} \cdot \mathbf{v}}\right) \mathbf{v}.
python · try it yourself
import math

def dot(u, v):
    return u[0] * v[0] + u[1] * v[1]

a, b = (2, 1), (1, 3)
cos_t = dot(a, b) / (math.hypot(*a) * math.hypot(*b))
print("a·b =", dot(a, b), "  angle =", round(math.degrees(math.acos(cos_t)), 6), "°")

k = dot(a, b) / dot(b, b)
proj = (k * b[0], k * b[1])
print("projection of a onto b:", proj)
rest = (a[0] - proj[0], a[1] - proj[1])
print("a − proj =", rest, "  perpendicular to b?", dot(rest, b) == 0)
a·b = 5   angle = 45.0 °
projection of a onto b: (0.5, 1.5)
a − proj = (1.5, -0.5)   perpendicular to b? True

Splitting a vector into a part along b\mathbf{b} and a part perpendicular to it is how games make a character slide along a wall: keep the part parallel to the wall, drop the part into it. See Projection and Reflection.

The 2D cross product: area and orientation

In 2D, the number

u×v=u1v2−u2v1\mathbf{u} \times \mathbf{v} = u_1 v_2 - u_2 v_1

is the signed area of the parallelogram spanned by u\mathbf{u} and v\mathbf{v}. Half of its absolute value is the area of the triangle they span. Its sign tells you orientation: positive if v\mathbf{v} is counter-clockwise from u\mathbf{u} (a left turn), negative for a right turn, zero if they are parallel. It is the same expression that appears in the shoelace formula.

python · try it yourself
def cross(u, v):
    return u[0] * v[1] - u[1] * v[0]

def side(A, B, P):
    """Is P to the left of, right of, or on the line from A to B?"""
    c = cross((B[0] - A[0], B[1] - A[1]), (P[0] - A[0], P[1] - A[1]))
    return "left" if c > 0 else "right" if c < 0 else "on the line"

A, B = (0, 0), (4, 2)
for P in [(1, 3), (3, 0), (6, 3)]:
    print(P, side(A, B, P))

C = (1, 3)
area = abs(cross((B[0] - A[0], B[1] - A[1]), (C[0] - A[0], C[1] - A[1]))) / 2
print("area of triangle ABC:", area)
(1, 3) left
(3, 0) right
(6, 3) on the line
area of triangle ABC: 5.0

This one test is the building block of convex hulls, segment-intersection tests and "which side of the road is the car on" logic (Convex Hulls).

Proving with vectors

Vectors make some classic theorems a two-line calculation.

Midsegment theorem. The segment joining the midpoints of two sides of a triangle is parallel to the third side and half as long. With midpoints M=(A+C)/2M = (A + C)/2 and N=(B+C)/2N = (B + C)/2:

N−M=(B+C)/2−(A+C)/2=(B−A)/2=12⋅AB→N - M = (B + C)/2 - (A + C)/2 = (B - A)/2 = \tfrac{1}{2} \cdot \overrightarrow{AB}

Diagonals of a parallelogram bisect each other. If ABCDABCD is a parallelogram then D−A=C−BD - A = C - B, so A+C=B+DA + C = B + D, which says the midpoints (A+C)/2(A + C)/2 and (B+D)/2(B + D)/2 of the two diagonals are the same point.

In software

  • Movement: position += velocity · dt is vector addition and scaling.
  • Lighting: brightness is the dot product of the surface normal with the light direction.
  • Field of view: an enemy sees the player if dot(facing, to_player) > |to_player| · cos(half_angle).
  • More in Vectors, Dot Product, Linear Algebra: Dot Product and Calculus: Vectors.

Exercises

Exercise 1: Length and direction

For A=(−1,4)A = (-1, 4) and B=(3,1)B = (3, 1), find AB→\overrightarrow{AB}, its length and its unit vector.

Answer

AB→=(4,−3)\overrightarrow{AB} = (4, -3), length 5, unit vector (0.8,−0.6)(0.8, -0.6).

Solution
AB→=(3−(−1),1−4)=(4,−3)∥AB→∥=16+9=5unit=(4/5,−3/5)=(0.8,−0.6)\begin{aligned} \overrightarrow{AB} &= (3 - (-1), 1 - 4) = (4, -3) \\ \lVert \overrightarrow{AB} \rVert &= \sqrt{16 + 9} = 5 \\ \text{unit} &= (4/5, -3/5) = (0.8, -0.6) \end{aligned}

Exercise 2: Perpendicular?

Are (6,−4)(6, -4) and (2,3)(2, 3) perpendicular? Are (6,−4)(6, -4) and (−3,2)(-3, 2) parallel?

Answer

Yes, perpendicular (dot product 0). Yes, parallel ((6,−4)=−2⋅(−3,2)(6, -4) = -2 \cdot (-3, 2)).

Solution
(6,−4)⋅(2,3)=12−12=0→perpendicularcross: 6⋅2−(−4)(−3)=12−12=0→parallel (opposite directions)\begin{aligned} &(6, -4) \cdot (2, 3) = 12 - 12 = 0 \to \text{perpendicular} \\ &\text{cross: } 6 \cdot 2 - (-4)(-3) = 12 - 12 = 0 \to \text{parallel (opposite directions)} \end{aligned}

Exercise 3: An exact angle

Find the angle between (1,0)(1, 0) and (1,3)(1, \sqrt{3}).

Answer

60°

Solution
dot=1,∥u∥=1,∥v∥=1+3=2cos⁡θ=1/2⇒θ=60∘\begin{aligned} &\text{dot} = 1, \quad \lVert \mathbf{u} \rVert = 1, \quad \lVert \mathbf{v} \rVert = \sqrt{1 + 3} = 2 \\ &\cos\theta = 1 / 2 \Rightarrow \theta = 60^\circ \end{aligned}

Exercise 4: Area and side

For A=(1,1)A = (1, 1), B=(5,2)B = (5, 2), C=(2,6)C = (2, 6), find the area of △ABC\triangle ABC with the cross product, and say whether CC is left or right of the direction A→BA \to B.

Answer

Area 9.5; CC is to the left.

Solution
AB→=(4,1),AC→=(1,5)AB→×AC→=4⋅5−1⋅1=19>0→C is to the leftarea=19/2=9.5\begin{aligned} &\overrightarrow{AB} = (4, 1), \quad \overrightarrow{AC} = (1, 5) \\ &\overrightarrow{AB} \times \overrightarrow{AC} = 4 \cdot 5 - 1 \cdot 1 = 19 > 0 \to C \text{ is to the left} \\ &\text{area} = 19 / 2 = 9.5 \end{aligned}
python · try it yourself
AB, AC = (4, 1), (1, 5)
c = AB[0] * AC[1] - AB[1] * AC[0]
print(c, c / 2)
19 9.5

Exercise 5: Check the midsegment theorem

For the triangle A=(0,0)A = (0, 0), B=(6,0)B = (6, 0), C=(2,4)C = (2, 4), compute the midpoints MM of AC‾\overline{AC} and NN of BC‾\overline{BC}, and the vector MN→\overrightarrow{MN}. Compare with AB→\overrightarrow{AB}.

Answer

M=(1,2)M = (1, 2), N=(4,2)N = (4, 2), MN→=(3,0)=12⋅(6,0)=12⋅AB→\overrightarrow{MN} = (3, 0) = \frac{1}{2} \cdot (6, 0) = \frac{1}{2} \cdot \overrightarrow{AB}.

Solution
M=((0+2)/2,(0+4)/2)=(1,2)N=((6+2)/2,(0+4)/2)=(4,2)MN→=(4−1,2−2)=(3,0),AB→=(6,0)\begin{aligned} M &= ((0 + 2)/2, (0 + 4)/2) = (1, 2) \\ N &= ((6 + 2)/2, (0 + 4)/2) = (4, 2) \\ \overrightarrow{MN} &= (4 - 1, 2 - 2) = (3, 0), \qquad \overrightarrow{AB} = (6, 0) \end{aligned}

MN→\overrightarrow{MN} is half of AB→\overrightarrow{AB} and points the same way, so it is parallel and half as long, as the theorem says.

Test yourself

What is the vector from A=(2,7)A = (2, 7) to B=(5,3)B = (5, 3)?

  1. (7,10)(7, 10)
  2. (−3,4)(-3, 4)
  3. (3,−4)(3, -4)
  4. (3,4)(3, 4)

The dot product of two non-zero vectors is negative. What do you know about the angle between them?

  1. It is obtuse (more than 90°)
  2. It is acute
  3. It is exactly 90°
  4. The vectors are parallel

For u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2), what does u1v2−u2v1=5u_1 v_2 - u_2 v_1 = 5 tell you?

  1. The vectors are perpendicular
  2. v\mathbf{v} is clockwise from u\mathbf{u}
  3. The vectors have length 5
  4. v\mathbf{v} is counter-clockwise from u\mathbf{u}, and the parallelogram they span has area 5