Algorithms · Visualized
Computational Geometry
Computational Geometry
Points, polygons, and hulls — geometry made computational.
Big-O: Cross products and sweeping tell left/right and inside/outside.
Computational Geometry
Compute with coordinates: orientation tests, hulls and sweep lines.
Computational geometry answers positional questions about points, lines and polygons. The core primitive is the cross product of two edge vectors: its sign tells whether the path A→B→C turns counter-clockwise (positive), clockwise (negative), or is collinear (zero) — that single test drives convex hulls (Graham scan sorts by angle then keeps only left turns), point-in-polygon ray casting, and segment intersection. Sweep-line algorithms move a vertical line across sorted x-coordinates, maintaining an active set so pairs that could interact are examined once. Exact arithmetic matters: integer cross products avoid floating-point drift, and values like (x₂−x₁)(y₃−y₁) can overflow 32 bits.
The general recipe
- Represent the input exactly: integer coordinates where possible; define collinearity and touching conventions.
- Reduce the question to orientation tests (cross-product signs) or distance comparisons.
- For hulls: sort points, walk the boundary maintaining a stack of left (or right) turns, popping on violations.
- For sweeps: sort events by coordinate, keep an ordered active set, and check only neighbours in it.
- Test degenerate inputs: duplicate points, all-collinear sets, and polygon vertices repeated.
Vocabulary you'll meet everywhere here
- Cross product sign
- orientation of an ordered triple of points — the fundamental 2D geometric test
- Convex hull
- the smallest convex polygon containing all points (Graham scan O(n log n), Jarvis march O(nh))
- Sweep line
- process events left-to-right with an active set, turning geometric proximity into 1D adjacency
- Ray casting
- count edge crossings of a ray from the point; odd = inside
- Manhattan vs Euclidean
- grid distance |dx|+|dy| versus straight-line distance — pick per problem
Where this family shows up
- Collision detection and hit-testing in games and UI frameworks
- GIS: map boundaries, area of polygons, spatial indexing
- Computer graphics clipping, vessel/flight corridor mapping, closest-facility queries
Common pitfalls
- Floating-point equality: use epsilon comparisons or integer coordinates for exact orientation tests
- Overflow in cross products of large coordinates — widen to 64-bit intermediates
- Collinear points break naive hull code: handle zero-area turns explicitly
- Assuming simple (non-self-intersecting) polygons when the input may not be
def convex_hull(points):
pts = sorted(points) # sort by x, then y
def cross(o, a, b):
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
lower = []
for p in pts:
while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0:
lower.pop()
lower.append(p)
upper = []
for p in reversed(pts):
while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0:
upper.pop()
upper.append(p)
return lower[:-1] + upper[:-1]Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.