{"record":{"id":"0abc3a9e561b4273","repo":"TheAlgorithms/JavaScript","slug":"minimum-of-3-points-is-required-to-form-closed-pol","errorCode":null,"errorMessage":"Minimum of 3 points is required to form closed polygon!","messagePattern":"Minimum of 3 points is required to form closed polygon!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Geometry/ConvexHullGraham.js","lineNumber":31,"sourceCode":"  return 1\n}\nfunction orientation(a, b, c) {\n  // Check orientation of Line(a, b) and Line(b, c)\n  const alpha = (b.y - a.y) / (b.x - a.x)\n  const beta = (c.y - b.y) / (c.x - b.x)\n\n  // Clockwise\n  if (alpha > beta) return 1\n  // Anticlockwise\n  else if (beta > alpha) return -1\n  // Colinear\n  return 0\n}\n\nfunction convexHull(points) {\n  const pointsLen = points.length\n  if (pointsLen <= 2) {\n    throw new Error('Minimum of 3 points is required to form closed polygon!')\n  }\n\n  points.sort(compare)\n  const p1 = points[0]\n  const p2 = points[pointsLen - 1]\n\n  // Divide Hull in two halves\n  const upperPoints = []\n  const lowerPoints = []\n\n  upperPoints.push(p1)\n  lowerPoints.push(p1)\n\n  for (let i = 1; i < pointsLen; i++) {\n    if (i === pointsLen - 1 || orientation(p1, points[i], p2) !== -1) {\n      let upLen = upperPoints.length\n\n      while (","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Geometry/ConvexHullGraham.js#L13-L49","documentation":"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.","triggerScenarios":"convexHull([]); convexHull([{x:0,y:0}]); convexHull([p1, p2]).","commonSituations":"Spatial datasets filtered down to a handful of points; empty result from a failed query; degenerate clusters where nearly all points were deduplicated.","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."],"exampleFix":"// before\nconst hull = convexHull(points) // throws when points.length <= 2\n\n// after\nif (points.length < 3) return points.slice()\nconst hull = convexHull(points)","handlingStrategy":"validation","validationCode":"function safeConvexHull(points) {\n  if (!Array.isArray(points) || points.length < 3) return points.slice()\n  return convexHull(points)\n}","typeGuard":"const canFormHull = (points) => Array.isArray(points) && points.length >= 3","tryCatchPattern":"try {\n  return convexHull(points)\n} catch (e) {\n  if (e instanceof Error && /3 points/i.test(e.message)) return points.slice()\n  throw e\n}","preventionTips":["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."],"tags":["geometry","convex-hull","input-size","polygon"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}