What is the quickest way to find the shortest cartesian distance between two polygons

后端 未结 13 1554
迷失自我
迷失自我 2021-02-01 05:54

I have 1 red polygon say and 50 randomly placed blue polygons - they are situated in geographical 2D space. What is the quick

相关标签:
13条回答
  • 2021-02-01 06:25

    The naive approach is to find the distance between the red and 50 blue objects -- so you're looking at 50 3d Pythagorean calculations + sorting to find the answer. That would only really work for finding the distance between center points though.

    If you want arbitrary polygons, maybe your best best is a raytracing solution that emits rays from the surface of the red polygon with respect to the normal, and reports when another polygon is hit.

    A hybrid might work -- we could find the distance from the center points, assuming we had some notion of the relative size of the blue polygons, we could cull the result set to the closest among those, then use raytracing to narrow down the truly closest polygon(s).

    0 讨论(0)
  • 2021-02-01 06:29

    This screening technique is intended to reduce the number of distance computations you need to perform in the average case, without compromising the accuracy of the result. It works on convex and concave polygons.

    Find the the minimum distance between each pair of vertexes such that one is a red vertex and one is a blue. Call it r. The distance between the polygons is at most r. Construct a new region from the red polygon where each line segment is moved outward by r and is joined to its neighbors by an arc of radius r is centered at the vertex. Find the distance from each vertex inside this region to every line segment of the opposite color that intersects this region.

    Of course you could add an approximate method such as bounding boxes to quickly determine which of the blue polygons can't possibly intersect with the red region.

    0 讨论(0)
  • 2021-02-01 06:33

    As others have mentioned using bounding areas (boxes, circles) may allow you to discard some polygon-polygon interactions. There are several strategies for this, e.g.

    1. Pick any blue polygon and find the distance from the red one. Now pick any other polygon. If the minimum distance between the bounding areas is greater than the already found distance you can ignore this polygon. Continue for all polygons.
    2. Find the minimum distance/centroid distance between the red polygon and all the blue polygons. Sort the distances and consider the smallest distance first. Calculate the actual minimum distance and continue through the sorted list until the maximum distance between the polygons is greater than the minimum distance found so far.

    Your choice of circles/axially aligned boxes, or oriented boxes can have a great affect on performance of the algorithm, dependent on the actual layout of the input polygons.

    For the actual minimum distance calculation you could use Yang et al's 'A new fast algorithm for computing the distance between two disjoint convex polygons based on Voronoi diagram' which is O(log n + log m).

    0 讨论(0)
  • 2021-02-01 06:34

    I know you said "the shortest distance" but you really meant the optimal solution or a "good/very good" solution is fine for your problem?

    Because if you need to find the optimal solution, you have to calculate the distance between all of your source and destination poligon bounds (not only vertexes). If you are in 3D space then each bound is a plane. That can be a big problem (O(n^2)) depending on how many vertexes you have.

    So if you have vertex count that makes that squares to a scarry number AND a "good/very good" solution is fine for you, go for a heuristic solution or approximation.

    0 讨论(0)
  • 2021-02-01 06:39

    I doubt there is better solution than calculating the distance between the red one and every blue one and sorting these by length.

    Regarding sorting, usually QuickSort is hard to beat in performance (an optimized one, that cuts off recursion if size goes below 7 items and switches to something like InsertionSort, maybe ShellSort).

    Thus I guess the question is how to quickly calculate the distance between two polygons, after all you need to make this computation 50 times.

    The following approach will work for 3D as well, but is probably not the fastest one:

    Minimum Polygon Distance in 2D Space

    The question is, are you willing to trade accuracy for speed? E.g. you can pack all polygons into bounding boxes, where the sides of the boxes are parallel to the coordinate system axes. 3D games use this approach pretty often. Therefor you need to find the maximum and minimum values for every coordinate (x, y, z) to construct the virtual bounding box. Calculating the distances of these bounding boxes is then a pretty trivial task.

    Here's an example image of more advanced bounding boxes, that are not parallel to the coordinate system axes:

    Oriented Bounding Boxes - OBB

    However, this makes the distance calculation less trivial. It is used for collision detection, as you don't need to know the distance for that, you only need to know if one edge of one bounding box lies within another bounding box.

    The following image shows an axes aligned bounding box:

    Axes Aligned Bounding Box - AABB

    OOBs are more accurate, AABBs are faster. Maybe you'd like to read this article:

    Advanced Collision Detection Techniques

    This is always assuming, that you are willing to trade precision for speed. If precision is more important than speed, you may need a more advanced technique.

    0 讨论(0)
  • 2021-02-01 06:39

    You might be able to reduce the problem, and then do an intensive search on a small set.

    Process each polygon first by finding:

    • Center of polygon
    • Maximum radius of polygon (i.e., point on edge/surface/vertex of the polygon furthest from the defined center)

    Now you can collect, say, the 5-10 closest polygons to the red one (find the distance center to center, subtract the radius, sort the list and take the top 5) and then do a much more exhaustive routine.

    0 讨论(0)
提交回复
热议问题