├── README.md ├── package.json └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # graham-fast 2 | ## Usage 3 | ```javascript 4 | var grahamScan = require("graham-fast"); 5 | 6 | var points = [[0,0],[1,0],[1,1],[0,1],[.5,.5],[-1,-1]]; 7 | 8 | var boundaryPoints = grahamScan(points); 9 | 10 | console.log(boundaryPoints); 11 | ``` 12 | ## License 13 | MIT 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graham-fast", 3 | "version": "1.0.0", 4 | "description": "Fast implementation of the graham scan algorithm", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/lovasoa/graham-fast" 12 | }, 13 | "keywords": [ 14 | "graham", 15 | "scan", 16 | "algorithm", 17 | "convex", 18 | "hull", 19 | "envelope", 20 | "points", 21 | "stretch", 22 | "enclose" 23 | ], 24 | "author": "Ophir LOJKINE", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://gist.github.com/lovasoa/510ba3f1ece9a623b703" 28 | }, 29 | "homepage": "https://gist.github.com/lovasoa/510ba3f1ece9a623b703" 30 | } 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function graham_scan(points) { 2 | // The enveloppe is the points themselves 3 | if (points.length <= 3) return points; 4 | 5 | // Find the pivot 6 | var pivot = points[0]; 7 | for (var i=0; i 1 && (b[0]-a[0]) * (c[1]-a[1]) <= (b[1]-a[1]) * (c[0]-a[0]))) { 29 | len--; 30 | b = a; 31 | a = result[len-2]; 32 | } 33 | result[len++] = c; 34 | } 35 | result.length = len; 36 | return result; 37 | } 38 | 39 | module.exports = graham_scan; 40 | --------------------------------------------------------------------------------