├── .gitignore ├── bench.js ├── package.json ├── LICENSE.txt ├── intersections.js ├── README.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /bench.js: -------------------------------------------------------------------------------- 1 | var isects = require('./intersections') 2 | var Suite = require('benchmark').Suite 3 | 4 | var suite = new Suite; 5 | 6 | var square = [ 7 | [0, 0], 8 | [10, 0], 9 | [10, 10], 10 | [0, 10], 11 | [0, 0] 12 | ]; 13 | 14 | var hourglass = [ 15 | [0, 0], 16 | [10, 0], 17 | [0, 10], 18 | [10, 10], 19 | ]; 20 | 21 | var hourglassVec2 = [ 22 | { x: 0, y: 0 }, 23 | { x: 10, y: 0 }, 24 | { x: 0, y: 10 }, 25 | { x: 10, y: 10 } 26 | ]; 27 | 28 | 29 | // add tests 30 | suite 31 | .add('isect', function() { 32 | isects(hourglass); 33 | }) 34 | .add('isect vec2', function() { 35 | isects(hourglassVec2); 36 | }) 37 | .add('no isect', function() { 38 | isects(square); 39 | }) 40 | 41 | // add listeners 42 | .on('cycle', function(event) { 43 | console.log(String(event.target)); 44 | }) 45 | .on('complete', function() { 46 | console.log('Fastest is ' + this.filter('fastest').pluck('name')); 47 | }) 48 | // run async 49 | .run({ 'async': true }); 50 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "benchmark": "^1.0.0", 4 | "tape": "^4.0.0" 5 | }, 6 | "dependencies": { 7 | "exact-segment-intersect": "^2.0.0", 8 | "robust-estimate-float": "^1.0.0" 9 | }, 10 | "name": "2d-polygon-self-intersections", 11 | "description": "This library may not be fast, but it is robust. Robust in the fact that it will find all of the self-intersections in a polygon - minus of course shared endpoints.", 12 | "version": "1.3.1", 13 | "main": "intersections.js", 14 | "scripts": { 15 | "test": "tape test.js" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/tmpvar/2d-polygon-self-intersections.git" 20 | }, 21 | "keywords": [ 22 | "2d", 23 | "polygon", 24 | "math", 25 | "intersections", 26 | "self-intersections", 27 | "robust" 28 | ], 29 | "author": "Elijah Insua ", 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/tmpvar/2d-polygon-self-intersections/issues" 33 | }, 34 | "homepage": "https://github.com/tmpvar/2d-polygon-self-intersections" 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright © 2015 Elijah Insua 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the “Software”), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /intersections.js: -------------------------------------------------------------------------------- 1 | var isect = require('exact-segment-intersect'); 2 | var float = require('robust-estimate-float'); 3 | 4 | module.exports = selfIntersections; 5 | 6 | function cmp(a, b) { 7 | return a[0] === b[0] && a[1] === b[1]; 8 | } 9 | 10 | var pc = [0, 0]; 11 | var pn = [0, 0]; 12 | var oc = [0, 0]; 13 | var on = [0, 0]; 14 | 15 | function arrayOrObject(v, ret) { 16 | if (Array.isArray(v)) { 17 | ret[0] = v[0]; 18 | ret[1] = v[1]; 19 | } else { 20 | ret[0] = v.x; 21 | ret[1] = v.y; 22 | } 23 | } 24 | 25 | function selfIntersections(poly, filterFn) { 26 | var seen = {}; 27 | var l = poly.length; 28 | var isects = []; 29 | for (var o=0; o