Skip to content

Chapter 8: Geometry

Overview

Computational geometry is the least forgiving topic in competitive programming — one wrong epsilon and you get WA on test 47. But when geometry problems appear (and they do, regularly), there's no substitute for knowing these techniques. This chapter starts with the essential primitives (cross product, orientation test) that are the building blocks for everything, then covers convex hull, rotating calipers, and half-plane intersection. The good news: once you nail the basics, most contest geometry reduces to a few clean patterns.

Coming from: Chapter 7 — Mathematics — number theory, combinatorics, and game theory
Going to: Chapter 9 — Offline Techniques — sweep line, Mo's algorithm, and query reordering

Topics at a Glance

#TopicDifficultyKey Trigger Phrase
01Geometry BasicsBeginner"Cross product," "orientation test," "point-in-polygon"
02Convex HullIntermediate"Smallest convex polygon containing all points"
03Rotating CalipersAdvanced"Diameter of point set" or "farthest pair"
04Half-Plane IntersectionAdvanced"Region satisfying linear inequalities" or "polygon kernel"
05Point in PolygonIntermediate"Is this point inside the polygon?"
06Closest PairIntermediate"Minimum distance between any two points"
07Half-Plane Intersection — Contest TemplateAdvancedLean paste-ready template companion to 04
08Rotating Calipers — Contest TemplateAdvancedLean paste-ready template companion to 03
09Sweep Line GeometryAdvanced"Segment intersection," "rectangle union area"
10Geometry RecipesIntermediate"Point-to-line distance," "circle intersection," "rotation"
11Floating-Point GotchasRequired"WA on test 47" — read this before any geometry submission

About files 03/08 and 04/07: files 03 and 04 are the conceptual lessons (intuition, proofs, walkthroughs, applications). Files 08 and 07 are the lean contest templates — minimal prose around a single tested implementation. Read the lesson once; keep the template open during contests.

If You Only Read 3 Files

  1. Geometry Basics — because the cross product and orientation test are the atoms of computational geometry. Every other algorithm in this chapter (and most geometry problems on CF) builds on them.
  2. Convex Hull — because Andrew's monotone chain is the most commonly needed geometry algorithm in contests. It's the answer to "farthest pair," "maximum area triangle," and many optimization problems on point sets.
  3. Geometry Recipes — because when geometry appears as a subtask in a non-geometry problem, you need compact, tested formulas you can paste immediately. This file is your emergency kit.

And keep Floating-Point Gotchas at hand — geometry's failure mode is almost never "wrong algorithm," it's "right algorithm, wrong epsilon."

Cross-Chapter Connections


File Listing

FileTopicLevel
geometry-basicsGeometry Basics
convex-hullConvex Hull⭐⭐
rotating-calipersRotating Calipers (lesson)⭐⭐⭐
half-plane-intersectionHalf-Plane Intersection (lesson)⭐⭐⭐
point-in-polygonPoint in Polygon⭐⭐
closest-pairClosest Pair of Points⭐⭐
half-plane-intersection-templateHalf-Plane Intersection (contest template)⭐⭐⭐
rotating-calipers-templateRotating Calipers (contest template)⭐⭐⭐
sweep-line-geometrySweep Line for Geometry⭐⭐⭐
geometry-recipesGeometry Recipes⭐⭐
floating-point-gotchasFloating-Point Gotchas⭐⭐

Suggested Reading Order

  1. geometry-basics — cross product, orientation test, foundational primitives
  2. floating-point-gotchas — read this alongside basics; integer-vs-floating choice should be made before you write any geometry code
  3. convex-hull — Andrew's monotone chain algorithm
  4. point-in-polygon — ray casting, winding number, O(log n) for convex
  5. closest-pair — divide-and-conquer and sweep-line approaches
  6. rotating-calipers — diameter and extremal measurements on convex hulls
  7. half-plane-intersection — intersect half-planes for LP and polygon kernels
  8. sweep-line-geometry — segment intersection, rectangle union area
  9. geometry-recipes — compact reference for common subroutines

Built for the climb from Codeforces 1100 to 2100.