V

Algorithm Visualizer

ZHEN
Share on WeiboShare on XGitHub repositoryAuthor website
Learning ToolsAlgorithm Complexity ReferenceAlgorithm Learning Paths
Data StructuresArrayLinked ListStackQueue and DequeBinary Search TreeBinary HeapHash TableGraphTrieDisjoint Set UnionLRU CacheSkip ListSegment TreeB+ TreeBloom FilterFenwick Tree
SortingBubble SortCocktail Shaker SortBitonic SortSelection SortInsertion SortBinary Insertion SortShell SortMerge SortTop-Down Merge SortQuick SortThree-Way Quick SortDual-Pivot Quick SortHeap SortCounting SortRadix SortBucket Sort
Graph AlgorithmsDijkstra's Shortest PathKruskal's Minimum Spanning TreePrim's Minimum Spanning TreeBellman-Ford Shortest PathsTopological SortFloyd-WarshallStrongly Connected Components2-SATMaximum FlowBipartite MatchingLowest Common AncestorEulerian Path
Dynamic ProgrammingEdit Distance0/1 KnapsackUnbounded KnapsackLongest Common SubsequenceLongest Increasing SubsequenceCoin ChangeStone MergingTraveling Salesperson DPTree Dynamic ProgrammingDigit DPRerooting DP
Backtracking and SearchN-QueensSubsetsPermutationsCombination SumMaze Solving with DFSNumber of IslandsWord SearchSudoku SolverA* Search
StringsKMP String MatchingRabin-Karp String MatchingBoyer-Moore String MatchingManacher's Longest Palindromic SubstringSuffix ArrayLCP ArrayAho-Corasick AutomatonZ Function
Math and Number TheorySieve of EratosthenesLinear SieveEuclidean AlgorithmBinary ExponentiationExtended Euclidean AlgorithmChinese Remainder TheoremEuler's Totient FunctionMiller-Rabin Primality TestFast Fourier TransformPollard's Rho Factorization
Computational GeometryConvex HullRotating CalipersClosest Pair of PointsLine Segment IntersectionBentley-Ottmann Sweep Line
SearchingBinary SearchLower and Upper BoundSearch in a Rotated Sorted ArrayBinary Search on the AnswerTernary Search

Rotating Calipers

Advance antipodal support lines around a convex polygon to find its diameter without checking every pair.

Core idea

On a convex polygon, an antipodal support point moves monotonically as each hull edge advances. This rotating pair visits all diameter candidates without restarting a scan.

Read the visualization

One pointer selects the current hull edge and the other rotates while triangle area increases. Candidate endpoint distances update the best diameter pair.

Load the convex polygon and place the first antipodal support pair.

1function diameter(hull: Pt[]): number {
2 const m = hull.length;
3 const area2 = (a: Pt, b: Pt, c: Pt) => Math.abs(cross(a, b, c));
4 let j = 1, best = 0;
5 for (let i = 0; i < m; i++) { // init count convex hull edge init
6 const ni = (i + 1) % m;
7 while (area2(hull[i], hull[ni], hull[(j + 1) % m]) >
8 area2(hull[i], hull[ni], hull[j])) {
9 j = (j + 1) % m; // advance the antipodal point monotonically (spin → spin)
10 }
11 best = Math.max(best, d2(hull[i], hull[j]), d2(hull[ni], hull[j])); // spin candidate
12 }
13 return best; // diameter ²( one done)
14}
convex hull vertex6
current farthest ²-
1 / 8

Complexity and tradeoffs

Time: O(n) after the hull. Space: O(1). Monotone orientation changes ensure each pointer makes at most one full circuit.

Invariant: The opposite pointer never needs to move backward because support direction rotates monotonically around a convex polygon.

Where it fits

Rotating calipers finds convex diameter, width, bounding rectangles, and distances between convex polygons after a hull has already been built.