Vector cross product
- Algorithms course
- Expert
- Lesson 19/41
- 4 min
Determine orientation with the cross product.
Vector cross product
The cross product determines the orientation of three points: left turn (counterclockwise), right turn (clockwise) or colinear. It is the foundation of geometric algorithms.
For points A, B, C the cross product is (B-A) × (C-A). In two dimensions this yields a scalar via (bx-ax)·(cy-ay) - (by-ay)·(cx-ax).
A positive result means left turn, negative means right turn, zero means colinear.
Orientation formula
To compute the orientation of three points we derive two edge vectors: AB = (bx-ax, by-ay) and AC = (cx-ax, cy-ay).
The cross product AB × AC is the determinant of these two vectors. The result is the signed area of the parallelogram.
This computation is O(1) and is used constantly in more complex algorithms.
Practice cross_product function
Write cross_product(ax, ay, bx, by, cx, cy). Return the cross product of three points. Print 12 with print(cross_product(0, 0, 4, 0, 2, 3)).
Starter code
def cross_product(ax, ay, bx, by, cx, cy):
# Cross product of three pointsSample solution
def cross_product(ax, ay, bx, by, cx, cy):
# Cross product of three points
return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax)
print(cross_product(0, 0, 4, 0, 2, 3))Expected output
12
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does a negative cross product mean?
cp = (bx-ax)*(cy-ay) - (by-ay)*(cx-ax)- Left turn (counterclockwise)
- Colinear
- Zero
- Right turn (clockwise) Correct answer
Explanation: A negative cross product means right turn (clockwise).
2 cross product computation
cp = (bx - ax) * (cy - ay) - (____ - ay) * (cx - ax)
cp = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax)
Explanation: (by - ay) is the right term.
3 Orientation concepts
| Term | Meaning |
|---|---|
positive |
Left turn |
negative |
Right turn |
zero |
Colinear |
determinant |
Signed area |