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 segmentp1p2 is the set of points {αp1+(1−α)p2∣0≤α≤1}. These are convex combinations of the endpoints. If we allow any α∈R, we get the (infinite) line through p1 and p2, which is the extension of the segment.
A directed segmentp1→p2 has an implied direction from p1 to p2.
Convex Combinations
A point p3 is on the segment p1p2 iff there exists α∈[0,1] such that:
p3=αp1+(1−α)p2
Or, in coordinates: x3=αx1+(1−α)x2, y3=αy1+(1−α)y2.
Worked Example: Orientation
Points: P=(1,1), Q=(4,2), R=(2,5).
PQ=(4−1,2−1)=(3,1)
PR=(2−1,5−1)=(1,4)
orient(P,Q,R)=3×4−1×1=12−1=11>0
R is to the left of PQ — a counter-clockwise turn.
Another point S=(5,5/3): PS=(4,2/3), cross product 3×(2/3)−1×4=2−4=−2<0 → clockwise turn.
Avoiding Division and Trigonometry
Using the equation y=mx+c to find intersections requires division by (m1−m2), 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).