mapbox / polylabel

A fast algorithm for finding the pole of inaccessibility of a polygon (in JavaScript and C++)
Other
1.44k stars 151 forks source link

Infinite loop while trying to find most promising cell from the queue #15

Closed demkalkov closed 8 years ago

demkalkov commented 8 years ago

In my case I've used us states map and have been trying to find the center of the states, but faced an infinite loop for some states because getCentroidCell used to return structure like { x: NaN, y: NaN, d: NaN . . . }

so when it tried to compare the numbers later, it had always the negative result

TWiStErRob commented 8 years ago

Is everything a Number in your passed in polygon? String coordinates won't work.

demkalkov commented 8 years ago

The polygon's value is [[0,0]]. Seems like for the current scale this states are too small. So in this case the area will be equal to zero, and there will be division by zero

TWiStErRob commented 8 years ago

Ah, yes, 0/0 is NaN, but N/0 is Infinity.

mourner commented 8 years ago

Agreed. We should probably bail out early in case of zero-area polygons.