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.

📖 Family Introduction

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

  1. Represent the input exactly: integer coordinates where possible; define collinearity and touching conventions.
  2. Reduce the question to orientation tests (cross-product signs) or distance comparisons.
  3. For hulls: sort points, walk the boundary maintaining a stack of left (or right) turns, popping on violations.
  4. For sweeps: sort events by coordinate, keep an ordered active set, and check only neighbours in it.
  5. 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
Python · Geometry Algorithms in Code
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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.