├── .gitignore ├── LICENSE ├── README.md ├── convexhull.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Andrey Naumenko 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Convexhull.js is a high-performance JavaScript 2D convex hull library. 2 | 3 | Checkout the demo: http://indy256.github.io/convexhull-js/ 4 | 5 | ![convexhull](https://raw.githubusercontent.com/indy256/convexhull-js/gh-pages/sample.png) -------------------------------------------------------------------------------- /convexhull.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 'use strict'; 3 | 4 | function convexHull(points) { 5 | points.sort(function (a, b) { 6 | return a.x != b.x ? a.x - b.x : a.y - b.y; 7 | }); 8 | 9 | var n = points.length; 10 | var hull = []; 11 | 12 | for (var i = 0; i < 2 * n; i++) { 13 | var j = i < n ? i : 2 * n - 1 - i; 14 | while (hull.length >= 2 && removeMiddle(hull[hull.length - 2], hull[hull.length - 1], points[j])) 15 | hull.pop(); 16 | hull.push(points[j]); 17 | } 18 | 19 | hull.pop(); 20 | return hull; 21 | } 22 | 23 | function removeMiddle(a, b, c) { 24 | var cross = (a.x - b.x) * (c.y - b.y) - (a.y - b.y) * (c.x - b.x); 25 | var dot = (a.x - b.x) * (c.x - b.x) + (a.y - b.y) * (c.y - b.y); 26 | return cross < 0 || cross == 0 && dot <= 0; 27 | } 28 | 29 | // export as AMD module / Node module / browser or worker variable 30 | if (typeof define === 'function' && define.amd) define(function () { return convexHull; }); 31 | else if (typeof module !== 'undefined') module.exports = convexHull; 32 | else if (typeof self !== 'undefined') self.convexHull = convexHull; 33 | else window.convexHull = convexHull; 34 | })(); 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "convexhull-js", 3 | "version": "1.0.0", 4 | "description": "A high-performance JavaScript 2D convex hull library", 5 | "homepage": "http://indy256.github.io/convexhull-js/", 6 | "author": "Andrey Naumenko", 7 | "keywords": [ 8 | "math", 9 | "geometry", 10 | "convex hull" 11 | ], 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/indy256/convexhull-js.git" 15 | }, 16 | "main": "convexhull.js", 17 | "devDependencies": { 18 | "benchmark": "^1.0.0", 19 | "faucet": "0.0.1", 20 | "jshint": "^2.6.3", 21 | "tape": "^3.5.0" 22 | }, 23 | "scripts": { 24 | "test": "jshint convexhull.js | faucet" 25 | }, 26 | "jshintConfig": { 27 | "quotmark": "single", 28 | "trailing": true, 29 | "unused": true 30 | } 31 | } 32 | --------------------------------------------------------------------------------