Using a Determinant to Find Orientation: Why Subtract Two Products?
Given three points in the plane, how can we use only their coordinates to decide whether traveling from the first to the second and then toward the third makes a left turn or a right turn?
The answer needs only subtraction, multiplication, and comparison. The unfamiliar-looking name determinant is, here, simply notation for a short calculation. This article explains from the beginning why it works; no knowledge of vectors, trigonometry, or matrices is required.
First clarify: whose left side?
We use the rectangular coordinate system from mathematics class: the horizontal axis points right and the vertical axis points up.
Imagine standing at point and facing point . We ask whether point is to the left or right of the direction you face. We compare the two sides of the line ; need not be in front of you.
For example, when you face directly right, above is your left and below is your right. Turn to face directly left, and above becomes your right instead.
So we must first establish the direction from toward ; looking only at up, down, left, and right in the diagram is not enough. We call this the "directed line ."
For three noncollinear points, being to the left of directed line also means that traveling along makes a left turn. Being to the right means a right turn.
For now, assume that and are distinct; otherwise "facing " gives no definite direction.
Move the starting point to the origin
Let the three points have coordinates
Here is the horizontal coordinate of and its vertical coordinate; the other points use the same notation.
Translate the entire figure so that lands at the origin. Moving all points together does not change which side of the line they are on. The translated coordinates are
where
We can read as follows: going from to moves horizontally by and vertically by . Negative numbers mean movement in the opposite direction.
For example, , , and become
We have only changed our observation position, not the shape formed by the three points.
A first attempt: compare the height of the point with the line
First consider . Traveling toward it from the origin moves up by for every to the right.
Thus the height on this line at horizontal coordinate should be
But has height , above the line's height . We face upward and to the right, so above the line is our left side. Therefore is on the left.
Replacing by puts it below the line, on the right; replacing it by puts it exactly on the line.
This suggests a method: at the same horizontal coordinate, compare the point's height with the line's height.
Derive the formula from the height difference
Now replace the particular point by the general point .
Heading right: when
The line through the origin and has the following height at horizontal coordinate :
This is the proportional relationship along a line: multiplying the horizontal displacement by some factor multiplies the vertical displacement by the same factor. It also applies to the part of the line extended in the opposite direction, where coordinates can be negative.
The height difference between and the line is
When heading right, above is left and below is right. Thus means left, means right, and means on the line.
Since , multiplying by does not change the sign:
We therefore do not need to perform division. We only need to calculate
Heading left: when the horizontal displacement is negative
The height difference is still , but now we face left: below the line is our left side.
Thus the left side corresponds to . Since as well, the product of these two negative numbers is positive, so we still have .
The right side corresponds to ; multiplying by negative gives .
In other words, when heading left, the correspondence between above/below and left/right reverses, and so does the sign of the multiplier . These two changes allow the same decision rule to keep working.
Heading vertically: when
We cannot divide by in this case, but can still be calculated:
Since and are distinct, here .
| Facing direction | Where is the left side? | Sign on the left side |
|---|---|---|
| Upward, | The point is west of the vertical axis, so | |
| Downward, | The point is east of the vertical axis, so |
On the right side the sign is the opposite. If , the point lies on this vertical line and .
Together, these three cases cover every possible heading:
| Value of | Point position | Turn while walking |
|---|---|---|
| is left of directed line | Left turn | |
| is right of directed line | Right turn | |
| The three points are collinear | This alone cannot distinguish continuing straight, turning back, or coincident points |
This is why the formula works: the sign of one number combines the point's height difference from the line with the direction we face.
What exactly is a determinant?
Arrange four numbers in two rows and enclose them between two vertical lines:
This is called a determinant of order two. "Order two" means that it has two rows and two columns.
To calculate it, multiply the upper-left and lower-right entries, then subtract the product of the upper-right and lower-left entries. The vertical lines here denote a determinant, not an absolute value.
Substitute the coordinate differences used in the translation to obtain a formula directly in terms of the original three points:
A determinant adds no new calculation steps; it simply gives this useful expression a name and notation. In computational geometry, this expression is also often called the "two-dimensional cross product."
Why is it also related to area?
Start with a configuration that is easy to draw: , , and , with and , and with above .
Draw a rectangle whose lower-left corner is the origin and whose upper-right corner is . Its area is . Outside triangle , three right triangles remain:
| Position | Two perpendicular sides | Area |
|---|---|---|
| Upper left | and | |
| Upper right | and | |
| Lower right | and |
Subtracting these three pieces from the rectangle's area gives
The last step uses only expansion and collection of like terms. You can draw and to check: the rectangle has area , and the three pieces have areas , , and . The remaining area is , exactly .
The decomposition above illustrates one positional arrangement. In general, we also have
Here does mean absolute value. To understand why this works in other arrangements, when choose the point on line with the same horizontal coordinate as . The vertical segment has length . Using as a common base, the distances from and to this vertical line are and .
- If lies between and , then lies on segment . Add the two triangular areas; the sum of their heights is .
- If lies outside that range, then lies on the extension of the segment. Subtract the two triangular areas; the absolute difference of their heights is .
Both cases give . If , use the vertical segment directly as the base. Its length is , the height is , and the area is .
Thus stores two kinds of information: its absolute value is twice the triangle's area, and its sign indicates orientation. This also explains why three collinear points give zero: the triangle is flattened into a line and has zero area.
Calculate three examples yourself
Fix and . The coordinate differences from to are .
| Third point | Coordinate differences from to | Conclusion | |
|---|---|---|---|
| Left side, left turn | |||
| Right side, right turn | |||
| Collinear |
In the third example, is exactly twice . Continuing from in the direction toward therefore reaches .
How does a convex hull algorithm use this result?
When constructing the boundary of a convex hull, Andrew's monotone chain algorithm considers the last two points in the current chain and the new point to be added, then calculates .
- : the path keeps turning left, so retain the current last point .
- : a right turn occurs, so pop and check the preceding points again.
- : the three points are collinear. This project retains only the endpoints of convex hull edges, so it also pops the intermediate point .
This removal affects only the chain currently being constructed. A point not in the lower chain may still belong to the upper chain. Sorting, forward and reverse scans, and orientation tests must work together to produce the complete convex hull.
Four things to remember when using the formula
Do not swap the point order arbitrarily. Swapping and changes the expression to , reversing the orientation.
Keep axis directions consistent. This article assumes that the vertical axis points up. Web canvases often make it point down; applying the formula directly to such screen coordinates reverses the visual left/right turns associated with the signs.
Zero does not necessarily mean continuing straight ahead. Three collinear points may describe continuing straight or turning back. If , the formula also gives zero, but there is no definite initial heading.
Close to zero is not zero. A mathematically small positive number still means the left side. Computer floating-point arithmetic can introduce rounding error; this project uses exact rational arithmetic to avoid mistaking nearly collinear points for truly collinear ones.
A short exercise
Let and : face straight upward from the origin.
- Is on the left or right?
- What about ?
- What about ? Does zero mean continuing forward in the original direction?
Show answers
Here and , so .
- , so : on the left.
- , so : on the right.
- , so : the three points are collinear. However, after walking from to , reaching requires turning back, not continuing forward.