In the landfill you have quite a few lines. The distance between the polygon is zero if the point lies inside the polygon or lies on an edge.
So you are looking for two cases:
- Check if the point is inside any polygon (remember that it can be more than one polygon
- Get a collection of all the edges and calculate each point distance from the edge. The closest distance gives you the distance from the polygon to which the edge belongs.
So, this is a simple algorithm that, if we look at 10 edges per polygon, takes O (10 * 10) * 142 for all of your points. This makes 100 * 142 = 14,200 operations. => O (m * deltaE) * n (m is the number of polygons, deltaE is the average number of edges per polygon, n is the number of points).
Now we are checking to see if we can speed it up. The first thing that comes to mind is that we can use bounding boxes or bounding circles for each polygon.
Another idea is to prepare the nearest edges for each polygon for a set of angles. For example, if you have 8 angles (every 45 Β°), you can remove all edges from the list, which will be replaced by another edge (therefore, any point on the far edge will always be a greater distance than any point on the other edge of the same polygon.
This way, as a rule, you can significantly reduce complexity. If you think of a rectangle, you only have one or two edges instead of 4. If you think of a regular 8x polygon, you can end up with one or two and a maximum of 3 edges per polygon.
If you add a normal vector to each edge, you can calculate whether the point can be inside, and you need to perform a scan or any check (or you know its konvex).
There are also mapping indices, such as dividing two-dimensional space into x and y in an equidistant mannor. In this case, you only need to check the polygons lying in 9 sectors.
In the next version, an R-tree can be used so that each bounding box (circle) of each node should be checked for the minimum expected distance and maximum expected distance. Therefore, you do not need to check node polygons that result in a much larger minimum distance than the maximum distances of another node.
Another thing is if you have a tree like yours, for example, with map data. On the street map you always have the world β region β country β county β city β urban sector β ...
In this case, you can find the closest location on the world map containing millions of polygons in a reasonable amount of time, mostly <10ms.
So you have a lot of options here. And pre-processing the list of polygons and extracting the corresponding edges either using binary spatial partitions of the polygon trees, or using the angular approach, or even with something even more attractive. It's up to you to decide. I expect you to end up doing something in the logarithmic range, like O (log (n) * log (deltaE)), becoming O (log (n)) as medium complexity.