Skip to content
Part IA Lent Term

Line Segments and Cross Products

The Cross Product

For two vectors a=(ax,ay)\mathbf{a} = (a_x, a_y) and b=(bx,by)\mathbf{b} = (b_x, b_y) in the plane, the cross product (or 2D cross product, or perpendicular dot product) is:

a×b=axbyaybx=det(axaybxby)\mathbf{a} \times \mathbf{b} = a_x b_y - a_y b_x = \det\begin{pmatrix} a_x & a_y \\ b_x & b_y \end{pmatrix}

This is a scalar representing the signed area of the parallelogram spanned by a\mathbf{a} and b\mathbf{b}.

Cross product geometry

Sign Interpretation

Given vectors a\mathbf{a} and b\mathbf{b} originating from the same point:

Cross productMeaning
a×b>0\mathbf{a} \times \mathbf{b} > 0b\mathbf{b} is counter-clockwise (left) from a\mathbf{a}
a×b<0\mathbf{a} \times \mathbf{b} < 0b\mathbf{b} is clockwise (right) from a\mathbf{a}
a×b=0\mathbf{a} \times \mathbf{b} = 0a\mathbf{a} and b\mathbf{b} are collinear (same or opposite direction)

Properties

  • Anti-symmetry: a×b=(b×a)\mathbf{a} \times \mathbf{b} = -(\mathbf{b} \times \mathbf{a})
  • Relation to angle: a×b=absinθ\mathbf{a} \times \mathbf{b} = |\mathbf{a}| |\mathbf{b}| \sin \theta, where θ\theta is the angle from a\mathbf{a} to b\mathbf{b}.
  • No trigonometry needed: computed with only two multiplications and one subtraction. This is exact for integer coordinates and fast in floating point.

Orientation Test

To determine the orientation of three points P,Q,RP, Q, R, use:

orient(P,Q,R)=(QP)×(RP)=(QxPx)(RyPy)(QyPy)(RxPx)\text{orient}(P, Q, R) = (Q - P) \times (R - P) = (Q_x - P_x)(R_y - P_y) - (Q_y - P_y)(R_x - P_x)

ResultMeaningTurn direction
>0> 0RR is left of PQP \to QCounter-clockwise (left turn)
<0< 0RR is right of PQP \to QClockwise (right turn)
=0= 0P,Q,RP, Q, R are collinearStraight

This is the fundamental building block for all geometric algorithms in this module. It is used for: sorting by polar angle, checking convexity, detecting segment intersections, and building convex hulls.

Line Segment Definition

A line segment p1p2p_1 p_2 is the set of points {αp1+(1α)p20α1}\{ \alpha p_1 + (1-\alpha) p_2 \mid 0 \le \alpha \le 1 \}. These are convex combinations of the endpoints. If we allow any αR\alpha \in \mathbb{R}, we get the (infinite) line through p1p_1 and p2p_2, which is the extension of the segment.

A directed segment p1p2p_1 \to p_2 has an implied direction from p1p_1 to p2p_2.

Convex Combinations

A point p3p_3 is on the segment p1p2p_1 p_2 iff there exists α[0,1]\alpha \in [0,1] such that:

p3=αp1+(1α)p2p_3 = \alpha p_1 + (1-\alpha) p_2

Or, in coordinates: x3=αx1+(1α)x2x_3 = \alpha x_1 + (1-\alpha)x_2, y3=αy1+(1α)y2y_3 = \alpha y_1 + (1-\alpha)y_2.

Worked Example: Orientation

Points: P=(1,1)P = (1,1), Q=(4,2)Q = (4,2), R=(2,5)R = (2,5).

PQ=(41,21)=(3,1)\overrightarrow{PQ} = (4-1, 2-1) = (3, 1)

PR=(21,51)=(1,4)\overrightarrow{PR} = (2-1, 5-1) = (1, 4)

orient(P,Q,R)=3×41×1=121=11>0\text{orient}(P,Q,R) = 3 \times 4 - 1 \times 1 = 12 - 1 = 11 > 0

RR is to the left of PQ\overrightarrow{PQ} — a counter-clockwise turn.

Another point S=(5,5/3)S = (5, 5/3): PS=(4,2/3)\overrightarrow{PS} = (4, 2/3), cross product 3×(2/3)1×4=24=2<03 \times (2/3) - 1 \times 4 = 2 - 4 = -2 < 0 → clockwise turn.

Avoiding Division and Trigonometry

Using the equation y=mx+cy = mx + c to find intersections requires division by (m1m2)(m_1 - m_2), which becomes numerically unstable when lines are nearly parallel (the problem is ill-conditioned: small changes in input cause large changes in output). Cross products use only addition and multiplication, maintaining infinite precision for integer inputs (no rounding until the final comparison).

Summary

ConceptFormula / Rule
Cross productaxbyaybxa_x b_y - a_y b_x
Positiveb\mathbf{b} is CCW from a\mathbf{a}
Negativeb\mathbf{b} is CW from a\mathbf{a}
ZeroCollinear
Orientation testorient(P,Q,R)=(QP)×(RP)\text{orient}(P,Q,R) = (Q-P) \times (R-P)
Convex combinationp3=αp1+(1α)p2p_3 = \alpha p_1 + (1-\alpha)p_2, 0α10 \le \alpha \le 1
Numerical advantageNo trig, no division, exact for integers