/** * Computes the 2D convex hull of a set of points using Graham's scanning * algorithm. The algorithm has been implemented as described in Cormen, * Leiserson, and Rivest's Introduction to Algorithms. The running time of * this algorithm is O(n log n), where n is the number of input points. * * @param vertices [[x1, y1], [x2, y2], …] * @returns polygon [[x1, y1], [x2, y2], …] */ d3.geom.hull = function(vertices) { if (vertices.length < 3) return []; var len = vertices.length, plen = len - 1, points = [], stack = [], i, j, h = 0, x1, y1, x2, y2, u, v, a, sp; // find the starting ref point: leftmost point with the minimum y coord for (i=1; i= (x2*x2 + y2*y2)) { points[i].index = -1; } else { points[u].index = -1; a = points[i].angle; u = i; v = j; } } else { a = points[i].angle; u = i; v = j; } } // initialize the stack stack.push(h); for (i=0, j=0; i<2; ++j) { if (points[j].index !== -1) { stack.push(points[j].index); i++; } } sp = stack.length; // do graham's scan for (; j 0; }