ClaasM / Algorithms

A comprehensive and exhaustive resource for algorithms in all languages and versions.
MIT License
19 stars 7 forks source link

[Error] "Point in polygon" JS - On edges #1

Open cavasinf opened 3 years ago

cavasinf commented 3 years ago

Hi, I'm using your code of Point in Polygon in JS.

But it seems to not work in every case.

I'm currently using leaflet, and with your code I want to know if my point is IN a polygon. It works like a charm when the point is inside or in the bottom edge or in left edge.

image ✔️ image ✔️ image ✔️

But not when it's inside the top or right edge.

imageimage

It's maybe due to the fact that we need the "line" from the point to cross the edge, and not being on it.

cavasinf commented 3 years ago

@return {boolean} whether the point is in the polygon (not on the edge, just turn < into <= and > into >= for that)

Even with that, if I apply this logic it only reverse my case above :

error

gerritnowald commented 2 years ago

I think I may have solved this in the Python implementation of the algorithm. There is a singularity in the original condition which may be responsible for this. I already created a pull request for it. If you want to check if a point is on the edge, I would check this separately. You can check out my polygon module in my repositories, I have a function for that.