2d-polygon-self-intersections
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -25,3 +25,3 @@ var isect = require('exact-segment-intersect'); | ||
function selfIntersections(poly) { | ||
function selfIntersections(poly, filterFn) { | ||
var seen = {}; | ||
@@ -55,4 +55,13 @@ var l = poly.length; | ||
var key = r+''; | ||
if (!seen[key]) { | ||
var unique = !seen[key]; | ||
if (unique) { | ||
seen[key] = true; | ||
} | ||
var collect = unique; | ||
if (filterFn) { | ||
collect = filterFn(r, oc, on, pc, pn, unique); | ||
} | ||
if (collect) { | ||
isects.push(r); | ||
@@ -59,0 +68,0 @@ } |
@@ -12,3 +12,3 @@ { | ||
"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.", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"main": "intersections.js", | ||
@@ -15,0 +15,0 @@ "scripts": { |
@@ -34,5 +34,13 @@ # 2d-polygon-self-intersections | ||
__isects__(`polygon`) | ||
__isects__(`polygon`[, `filterFn`]) | ||
* `polygon` - an array of 2 component arrays (i.e. a triangle `[[0, 0], [10, 0], [10, 10]]`) or an array of objects: `[{x:0, y:0}, {x:10, y:0}, {x:10, y:10}]` | ||
* `filterFn` - a filter function called whenever an intersection is found: `filterFn`(`isect`, `start0`, `end0`, `start1`, `end1`, `unique`) | ||
* `isect` - current intersection (e.g. `[5, 5]`) - mutations in this array get collected | ||
* `start0` - start of the first segment (e.g `[0, 5]`) | ||
* `end0` - start of the first segment (e.g `[10, 5]`) | ||
* `start1` - start of the first segment (e.g `[5, 0]`) | ||
* `end1` - start of the first segment (e.g `[5, 10]`) | ||
* `unique` - boolean representing whether or not this intersection point has been seen before | ||
* __return__ `true` to collect and `false` to discard | ||
@@ -43,3 +51,3 @@ __returns__ an empty array if no interesections or an array of 2 component arrays representing the intersection points. | ||
Also note that there are 2 intersections per crossing, this library will only report one. All intersections will be unique. | ||
Also note that there are 2 intersections per crossing, this library by default will only report one - all intersections will be unique. This behavior can be changed with the `filterFn`. | ||
@@ -46,0 +54,0 @@ ## license |
32
test.js
@@ -14,4 +14,3 @@ var test = require('tape'); | ||
var r = isects(poly); | ||
var r = isects(poly, t.fail); | ||
t.equal(r.length, 0, 'no self-intersections'); | ||
@@ -31,3 +30,9 @@ | ||
var r = isects(poly); | ||
var calls = 0; | ||
var r = isects(poly, function(isect, s0, e0, s1, e1, unique) { | ||
calls++; | ||
return unique; | ||
}); | ||
t.equal(calls, 2, 'visitor called twice') | ||
t.equal(r.length, 1, 'no self-intersections'); | ||
@@ -38,3 +43,24 @@ t.deepEqual(r[0], [5, 5], 'isect at (5, 0)') | ||
test('interesection visitor', function(t) { | ||
var poly = [ | ||
[0, 0], | ||
[10, 0], | ||
[0, 10], | ||
[10, 10], | ||
]; | ||
var calls = 0; | ||
var r = isects(poly, function(isect, start0, end0, start1, end1, unique) { | ||
calls++; | ||
return true; | ||
}); | ||
t.equal(calls, 2, 'visitor called twice') | ||
t.equal(r.length, 2, 'no self-intersections'); | ||
t.deepEqual(r[0], [5, 5], 'isect at (5, 0)') | ||
t.deepEqual(r[1], [5, 5], 'isect at (5, 0)') | ||
t.end(); | ||
}); | ||
test('work with vec2', function(t) { | ||
@@ -41,0 +67,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7896
162
55