Skip to main content

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 AA and facing point BB. We ask whether point CC is to the left or right of the direction you face. We compare the two sides of the line ABAB; CC 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 AA toward BB; looking only at up, down, left, and right in the diagram is not enough. We call this the "directed line ABAB."

For three noncollinear points, CC being to the left of directed line ABAB also means that traveling along A→B→CA\to B\to C makes a left turn. Being to the right means a right turn.

For now, assume that AA and BB are distinct; otherwise "facing BB" gives no definite direction.

Move the starting point to the origin​

Let the three points have coordinates

A=(Ax,Ay),B=(Bx,By),C=(Cx,Cy).\teachBlue{A}=(\teachBlue{A_x,A_y}),\quad \teachOrange{B}=(\teachOrange{B_x,B_y}),\quad \teachViolet{C}=(\teachViolet{C_x,C_y}).

Here AxA_x is the horizontal coordinate of AA and AyA_y its vertical coordinate; the other points use the same notation.

Translate the entire figure so that AA lands at the origin. Moving all points together does not change which side of the line they are on. The translated coordinates are

A′=(0,0),B′=(a,b),C′=(c,d),\teachBlue{A'}=(0,0),\quad \teachOrange{B'}=(\teachOrange{a,b}),\quad \teachViolet{C'}=(\teachViolet{c,d}),

where

a=Bx−Ax,b=By−Ay,\teachOrange{a}=\teachOrange{B_x}-\teachBlue{A_x},\quad \teachOrange{b}=\teachOrange{B_y}-\teachBlue{A_y}, c=Cx−Ax,d=Cy−Ay.\teachViolet{c}=\teachViolet{C_x}-\teachBlue{A_x},\quad \teachViolet{d}=\teachViolet{C_y}-\teachBlue{A_y}.

We can read (a,b)(a,b) as follows: going from AA to BB moves horizontally by aa and vertically by bb. Negative numbers mean movement in the opposite direction.

For example, A=(2,1)A=(2,1), B=(6,3)B=(6,3), and C=(3,4)C=(3,4) become

A′=(0,0),B′=(4,2),C′=(1,3).A'=(0,0),\quad B'=(4,2),\quad C'=(1,3).

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 B′=(4,2)B'=(4,2). Traveling toward it from the origin moves up by 22 for every 44 to the right.

Thus the height on this line at horizontal coordinate 11 should be

2×14=12.2\times\frac14=\frac12.

But C′=(1,3)C'=(1,3) has height 33, above the line's height 12\frac12. We face upward and to the right, so above the line is our left side. Therefore C′C' is on the left.

Replacing C′C' by (1,0)(1,0) puts it below the line, on the right; replacing it by (1,12)(1,\frac12) 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 B′=(4,2)B'=(4,2) by the general point B′=(a,b)B'=(a,b).

Heading right: when a>0a>0​

The line through the origin and (a,b)(a,b) has the following height at horizontal coordinate cc:

yline=bac.y_{\text{line}}=\frac ba c.

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 C′=(c,d)C'=(c,d) and the line is

h=d−bac.h=d-\frac ba c.

When heading right, above is left and below is right. Thus h>0h>0 means left, h<0h\lt 0 means right, and h=0h=0 means on the line.

Since a>0a>0, multiplying by aa does not change the sign:

ah=a(d−bac)=ad−bc.ah=a\left(d-\frac ba c\right)=ad-bc.

We therefore do not need to perform division. We only need to calculate

D=ad−bc.\boxed{D=ad-bc}.

Heading left: when the horizontal displacement is negative​

The height difference is still h=d−bach=d-\frac ba c, but now we face left: below the line is our left side.

Thus the left side corresponds to h<0h\lt 0. Since a<0a\lt 0 as well, the product of these two negative numbers is positive, so we still have D=ah>0D=ah>0.

The right side corresponds to h>0h>0; multiplying by negative aa gives D<0D\lt 0.

In other words, when heading left, the correspondence between above/below and left/right reverses, and so does the sign of the multiplier aa. These two changes allow the same decision rule to keep working.

Heading vertically: when a=0a=0​

We cannot divide by aa in this case, but D=ad−bcD=ad-bc can still be calculated:

D=−bc.D=-bc.

Since AA and BB are distinct, here b≠0b\ne0.

Facing directionWhere is the left side?Sign on the left side
Upward, b>0b>0The point is west of the vertical axis, so c<0c\lt 0D=−bc>0D=-bc>0
Downward, b<0b\lt 0The point is east of the vertical axis, so c>0c>0D=−bc>0D=-bc>0

On the right side the sign is the opposite. If c=0c=0, the point lies on this vertical line and D=0D=0.

Together, these three cases cover every possible heading:

Value of DDPoint positionTurn while walking
D>0D>0CC is left of directed line ABABLeft turn
D<0D\lt 0CC is right of directed line ABABRight turn
D=0D=0The three points are collinearThis 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:

∣abcd∣=ad−bc.\begin{vmatrix} a & b\\ c & d \end{vmatrix} =ad-bc.

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:

D=(Bx−Ax)(Cy−Ay)−(By−Ay)(Cx−Ax).\boxed{ D=(B_x-A_x)(C_y-A_y) -(B_y-A_y)(C_x-A_x) }.

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: A′=(0,0)A'=(0,0), B′=(a,b)B'=(a,b), and C′=(c,d)C'=(c,d), with 0<c<a0\lt c\lt a and 0<b<d0\lt b\lt d, and with C′C' above A′B′A'B'.

Draw a rectangle whose lower-left corner is the origin and whose upper-right corner is (a,d)(a,d). Its area is adad. Outside triangle A′B′C′A'B'C', three right triangles remain:

PositionTwo perpendicular sidesArea
Upper leftcc and ddcd/2cd/2
Upper righta−ca-c and d−bd-b(a−c)(d−b)/2(a-c)(d-b)/2
Lower rightaa and bbab/2ab/2

Subtracting these three pieces from the rectangle's area gives

SA′B′C′=ad−cd2−(a−c)(d−b)2−ab2=ad−bc2.\begin{aligned} S_{A'B'C'} &=ad-\frac{cd}{2}-\frac{(a-c)(d-b)}{2}-\frac{ab}{2}\\ &=\frac{ad-bc}{2}. \end{aligned}

The last step uses only expansion and collection of like terms. You can draw B′=(4,2)B'=(4,2) and C′=(1,3)C'=(1,3) to check: the rectangle has area 1212, and the three pieces have areas 1.51.5, 1.51.5, and 44. The remaining area is 55, exactly (4×3−2×1)/2(4\times3-2\times1)/2.

The decomposition above illustrates one positional arrangement. In general, we also have

SABC=∣D∣2.S_{ABC}=\frac{|D|}{2}.

Here ∣D∣|D| does mean absolute value. To understand why this works in other arrangements, when a≠0a\ne0 choose the point PP on line A′B′A'B' with the same horizontal coordinate as C′C'. The vertical segment PC′PC' has length ∣h∣|h|. Using PC′PC' as a common base, the distances from A′A' and B′B' to this vertical line are ∣c∣|c| and ∣a−c∣|a-c|.

  • If cc lies between 00 and aa, then PP lies on segment A′B′A'B'. Add the two triangular areas; the sum of their heights is ∣a∣|a|.
  • If cc lies outside that range, then PP lies on the extension of the segment. Subtract the two triangular areas; the absolute difference of their heights is ∣a∣|a|.

Both cases give S=∣a∣ ∣h∣/2=∣D∣/2S=|a|\,|h|/2=|D|/2. If a=0a=0, use the vertical segment A′B′A'B' directly as the base. Its length is ∣b∣|b|, the height is ∣c∣|c|, and the area is ∣bc∣/2=∣D∣/2|bc|/2=|D|/2.

Thus DD 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 A=(1,1)A=(1,1) and B=(4,2)B=(4,2). The coordinate differences from AA to BB are (3,1)(3,1).

Third pointCoordinate differences from AA to CCDDConclusion
C=(2,3)C=(2,3)(1,2)(1,2)3×2−1×1=53\times2-1\times1=5Left side, left turn
C=(2,0)C=(2,0)(1,−1)(1,-1)3×(−1)−1×1=−43\times(-1)-1\times1=-4Right side, right turn
C=(7,3)C=(7,3)(6,2)(6,2)3×2−1×6=03\times2-1\times6=0Collinear

In the third example, (6,2)(6,2) is exactly twice (3,1)(3,1). Continuing from AA in the direction toward BB therefore reaches CC.

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 A,BA,B in the current chain and the new point CC to be added, then calculates DD.

  • D>0D>0: the path keeps turning left, so retain the current last point BB.
  • D<0D\lt 0: a right turn occurs, so pop BB and check the preceding points again.
  • D=0D=0: the three points are collinear. This project retains only the endpoints of convex hull edges, so it also pops the intermediate point BB.

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 BB and CC changes the expression to cb−da=−Dcb-da=-D, 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 A=BA=B, 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 A=(0,0)A=(0,0) and B=(0,3)B=(0,3): face straight upward from the origin.

  1. Is C=(−2,1)C=(-2,1) on the left or right?
  2. What about C=(2,1)C=(2,1)?
  3. What about C=(0,−2)C=(0,-2)? Does zero mean continuing forward in the original direction?
Show answers

Here a=0a=0 and b=3b=3, so D=−3cD=-3c.

  1. c=−2c=-2, so D=6>0D=6>0: on the left.
  2. c=2c=2, so D=−6<0D=-6\lt 0: on the right.
  3. c=0c=0, so D=0D=0: the three points are collinear. However, after walking from AA to BB, reaching CC requires turning back, not continuing forward.