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 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 to point is , subtracting coordinates. A point says where; a vector says how to get from one place to another.
| Operation | Formula for , | Geometric meaning |
|---|---|---|
| addition | follow , then (head to tail) | |
| subtraction | the arrow from the tip of to the tip of | |
| scalar multiple | stretch by ; a negative flips the direction | |
| length (magnitude) | the Pythagorean theorem | |
| unit vector | same direction, length 1 |
Two vectors are parallel when one is a scalar multiple of the other.
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: walks from () to (). See Lerp.
The dot product: angles and perpendiculars
where is the angle between the vectors. So:
- means the vectors are perpendicular;
- means the angle is acute (they point roughly the same way); means it is obtuse.
- The projection of onto , the "shadow" of along , is .
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 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
is the signed area of the parallelogram spanned by and . Half of its absolute value is the area of the triangle they span. Its sign tells you orientation: positive if is counter-clockwise from (a left turn), negative for a right turn, zero if they are parallel. It is the same expression that appears in the shoelace formula.
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 and :
Diagonals of a parallelogram bisect each other. If is a parallelogram then , so , which says the midpoints and of the two diagonals are the same point.
In software
- Movement:
position += velocity · dtis 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 and , find , its length and its unit vector.
Answer
, length 5, unit vector .
Solution
Exercise 2: Perpendicular?
Are and perpendicular? Are and parallel?
Answer
Yes, perpendicular (dot product 0). Yes, parallel ().
Solution
Exercise 3: An exact angle
Find the angle between and .
Answer
60°
Solution
Exercise 4: Area and side
For , , , find the area of with the cross product, and say whether is left or right of the direction .
Answer
Area 9.5; is to the left.
Solution
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 , , , compute the midpoints of and of , and the vector . Compare with .
Answer
, , .
Solution
is half of and points the same way, so it is parallel and half as long, as the theorem says.
Test yourself
What is the vector from to ?
The dot product of two non-zero vectors is negative. What do you know about the angle between them?
- It is obtuse (more than 90°)
- It is acute
- It is exactly 90°
- The vectors are parallel
For and , what does tell you?
- The vectors are perpendicular
- is clockwise from
- The vectors have length 5
- is counter-clockwise from , and the parallelogram they span has area 5