TheAlgorithms/JavaScript · error · Error
Minimum of 3 points is required to form closed polygon!
Error message
Minimum of 3 points is required to form closed polygon!
What it means
Thrown by convexHull(points) (plain Error) when points.length <= 2. A convex hull requires at least three distinct points to bound a non-degenerate polygon; with 0, 1, or 2 points the algorithm cannot form a hull, so it refuses rather than returning an arbitrary segment.
Source
Thrown at Geometry/ConvexHullGraham.js:31
return 1
}
function orientation(a, b, c) {
// Check orientation of Line(a, b) and Line(b, c)
const alpha = (b.y - a.y) / (b.x - a.x)
const beta = (c.y - b.y) / (c.x - b.x)
// Clockwise
if (alpha > beta) return 1
// Anticlockwise
else if (beta > alpha) return -1
// Colinear
return 0
}
function convexHull(points) {
const pointsLen = points.length
if (pointsLen <= 2) {
throw new Error('Minimum of 3 points is required to form closed polygon!')
}
points.sort(compare)
const p1 = points[0]
const p2 = points[pointsLen - 1]
// Divide Hull in two halves
const upperPoints = []
const lowerPoints = []
upperPoints.push(p1)
lowerPoints.push(p1)
for (let i = 1; i < pointsLen; i++) {
if (i === pointsLen - 1 || orientation(p1, points[i], p2) !== -1) {
let upLen = upperPoints.length
while (View on GitHub (pinned to 5c39e87a9a)
Solutions
- Check points.length >= 3 before calling convexHull.
- For small sets, return the points themselves as the 'hull' rather than invoking the algorithm.
- Deduplicate points before counting, since duplicates can drop the effective count below 3.
- Validate at the API boundary and surface a domain-specific 'not enough points' error.
Example fix
// before const hull = convexHull(points) // throws when points.length <= 2 // after if (points.length < 3) return points.slice() const hull = convexHull(points)
Defensive patterns
Strategy: validation
Validate before calling
function safeConvexHull(points) {
if (!Array.isArray(points) || points.length < 3) return points.slice()
return convexHull(points)
} Type guard
const canFormHull = (points) => Array.isArray(points) && points.length >= 3
Try / catch
try {
return convexHull(points)
} catch (e) {
if (e instanceof Error && /3 points/i.test(e.message)) return points.slice()
throw e
} Prevention
- Check points.length >= 3 before calling.
- Deduplicate points first; duplicates can drop the effective count below 3.
- Return the input points as the 'hull' for small/degenerate sets.
- Validate at the boundary and surface a domain-specific error.
When it happens
Trigger: convexHull([]); convexHull([{x:0,y:0}]); convexHull([p1, p2]).
Common situations: Spatial datasets filtered down to a handful of points; empty result from a failed query; degenerate clusters where nearly all points were deduplicated.
Related errors
- Cannot normalize vectors of length 0
- Triplet cannot exist with the given array
- Invalid Triangle sides.
- The ${paramName} only accepts non-negative values
- The ${noName} should be Number type
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/0abc3a9e561b4273.
Report an issue: GitHub.