Computational Geometry

The cross product, polygon area and the convex hull.

Almost all contest geometry is built on one tool — the cross product: cross(O, A, B) = (A - O) × (B - O) = (Ax-Ox)(By-Oy) - (Ay-Oy)(Bx-Ox) Its sign tells you which way the polyline O→A→B turns: plus — counterclockwise, minus — clockwise, zero — the three points are collinear. And its absolute value equals twice the area of triangle OAB. The first application — the area of any polygon (the "shoelace formula"): the sum of cross products of consecutive vertices.

Polygon area. Enter: 4, then the vertices along the contour: 0 0, 4 0, 4 3, 0 3

The 4×3 rectangle gives 12. Notice: everything is computed in INTEGERS — we store twice the area and divide only when printing. The golden rule of geometry: stay in integers as long as possible; double with its rounding errors is the source of the most treacherous bugs. The second classic — the convex hull: the smallest convex polygon containing all the points. Andrew's monotone chain algorithm: sort the points by x, build the lower chain left to right, discarding points that make the "wrong" turn, then the upper chain right to left. O(n log n) for the sorting.

The convex hull. Enter: 6, then the points: 0 0, 2 0, 1 1, 2 2, 0 2, 1 -1

On the example the interior point (1,1) disappears, leaving the five hull vertices in counterclockwise order. The condition cross <= 0 also discards points on a straight line; replace it with < 0 if collinear points must be kept. Task: given the finished hull, compute its area with the shoelace formula — the lesson's two topics join into one solution. Mark the lesson as completed — you have walked the entire competitive path! What remains is practice: solve problems, compete, and come back to the lessons as a reference. Good luck!
Доска