The Algorithm
Given a convex polygon's vertices in counterclockwise order, walk two indices i (the current base edge) and j (the current farthest vertex from that edge) around the hull together. The key test at each step: is vertex j+1 farther from edge (i, i+1) than j is? "Farther" is measured with the same cross product from orientation() — the magnitude of the cross product of the edge vector and the vector to the candidate vertex is twice the triangle area, which is proportional to perpendicular distance for a fixed edge.
Core Insight: j never moves backward and never restarts from 0 on a new i — across the whole outer loop, j advances at most $n$ total steps, which is exactly why the whole algorithm is $O(n)$ rather than $O(n^2)$, even though it looks like a nested loop.
Trace It Yourself
Run polygon_diameter on the unit square [(0,0),(4,0),(4,4),(0,4)]. The diameter pair should be a diagonal (distance $4\sqrt2$), not an edge (distance $4$) — confirm j skips past the adjacent vertices and lands on the opposite corner as i advances around each edge.
The same i/j walking pattern, applied to the polygon's width instead of its diameter (tracking the perpendicular extent rather than the farthest vertex), gives the width and minimum-area enclosing rectangle computations described in the parent part.