I'm having two sets of polygon coordinates selected from leaflet geoJSON map. The parent and child coordinates are coordinates are:
var parentCoordinates=[
[
32.05898221582174,
-28.31004731142091
],
[
32.05898221582174,
-28.308044824292978
],
[
32.06134255975485,
-28.308044824292978
],
[
32.06134255975485,
-28.31004731142091
],
[
32.05898221582174,
-28.31004731142091
]
]
var childCoordinates=[
[
32.059904895722866,
-28.30970726909422
],
[
32.059904895722866,
-28.308743809931784
],
[
32.06089194864035,
-28.308743809931784
],
[
32.06089194864035,
-28.30970726909422
],
[
32.059904895722866,
-28.30970726909422
]
]
The child is drawn inside the parent area as shown in the picture:
Using Ray Casting algorithm to determine if the point lies inside polygon I 'm not able to determine as the result I'm getting is false. Please let me know where I'm doing wrong or any other way to determine the solution.Thanks
I've had good experience with Turf. It works well, is well documented and the examples are already shown with leaflet.
For your problem, you could use turf.within with
parentCoordinates
asturf.polygon
andchildCoordinates
as an array ofturf.point
:Here's a Fiddle example.
I tried with your algorithm and another found here https://rosettacode.org/wiki/Ray-casting_algorithm and both return the right value.
Maybe this fiddle can help you in the implementation :
https://jsfiddle.net/4psL2hoo/1/
Your algo
Rosetta code algo
You can try Leaflet's api for that -
contains
. You create a parent polygon with LatLngBounds and then child too.