mikolalysenko / point-in-big-polygon

Industrial strength point in polygon test
MIT License
19 stars 3 forks source link

Suitability for multiple polygons #3

Open letmaik opened 8 years ago

letmaik commented 8 years ago

If you have a bunch of non-overlapping polygons, is it possible to use this library by collecting all loops of all polygons instead of checking each polygon separately in a for loop?

For example this seems to work:

var loops = [
  [[0,0],[0,1],[1,1],[1,0]],
  [[10,10],[10,11],[11,11],[11,10]]
]
var classifyPoint = require('point-in-big-polygon')(loops)
assert.equal(classifyPoint([0.5,0.5]), -1)
assert.equal(classifyPoint([10.5,10.5]), -1)
assert.equal(classifyPoint([5,5]), 1)

In the example in the README, the second loop is given in anti-clockwise order, whereas in my example both are in clockwise order. Is that the trick? Do all holes have to be in anti-clockwise order otherwise?

letmaik commented 8 years ago

Just found an example where it doesn't work:

var loops = [
  [ [ -1, 55 ], [ 21, 55 ], [ 21, 51 ], [ -1, 51 ] ],
  [ [ 15, 51 ], [ 35, 51 ], [ 35, 47 ], [ 15, 47 ] ] 
]
var classifyPoint = require('point-in-big-polygon')(loops)
assert.equal(classifyPoint([20,50]), -1) // fails, is 1

One edge of each loop is touching each other. Could that be the problem?

mikolalysenko commented 8 years ago

Currently point-in-big-polygon assumes that there are no self intersections or double points in the polygon.

If you want to classify a point against many polygons you should use point-in-region instead.