Skip to the workspace
Pebellslearn by building
Computational Geometry  ›  Lesson

Orientation & the Cross Product

Orientation & the Cross Product 28 minThe sign of a 2D cross product tells you whether three points turn left, turn right, or lie on one line — the single test every other lesson in this section is built from

dsa / Orientation & the Cross Product

Explore each scene at your own pace.

01 · Which way does the van turn?

Which way does the van turn?

Orientation & the Cross Product

Definition
Orientation is the turn direction of three points taken in order, found from the sign of the cross product of the vectors (b - a) and (c - a): positive is a left turn, negative a right turn, and zero means the points lie on one straight line.
In plain words
Stand at the first point, look towards the second, and ask whether the third is on your left, on your right, or straight ahead. One small calculation answers that without any angles.

In short: given three points a, b, c in order, the path a -> b -> c either bends left, bends right, or runs dead straight. The cross product of vector (b - a) and vector (c - a) answers this with one number: its sign is the turn direction, and its size is twice the area of the triangle the three points span. Positive means a left (counter-clockwise) turn, negative means a right (clockwise) turn, and exactly zero means the three points are collinear — they lie on one straight line. Every lesson in this section reduces to asking this one question about three points.

A triangle a, b, c on a coordinate plane with vector b-a drawn from a and vector c-a drawn from a, the parallelogram they span shaded green with a plus sign because c sits to the left of the line through a and b, and a second faint triangle below the line shaded red with a minus sign to show the mirrored case

Which way does the van turn?

Meridian's route planner reads three consecutive stops at a time and asks whether the van bends left or right between them:

step through it
1def orientation(a, b, c):
2 cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
3 if cross > 0:
4 return 1
5 if cross < 0:
6 return -1
7 return 0
8 
9depot, warehouse, mall = (0, 0), (4, 0), (4, 3)
10print(orientation(depot, warehouse, mall))

depot -> warehouse runs straight east; mall sits north of warehouse, so the van bends left there — orientation returns 1. Swap mall for a stop south of the line, (2, -3), and the sign flips to -1: the van bends right. Nothing about the distance between the points matters here, only which side of the line a -> b the point c lands on.

Scene 1 of 5
Project context and further resources
You're building a piece ofMeridian — service areas & coverage
This piece — orientation(): Classifies whether three points bend left, right, or lie on a straight line — the one test everything else here is built from.
Scenario Meridian's route planner needs one cheap test, run millions of times: given three stops in order, did the van bend left, bend right, or just carry straight on?
orientation.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
orientation(a, b, c) → int1 for a left turn, -1 for a right turn, 0 if the three points are collinear.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.