Distance from a point to a polygon

后端 未结 5 1017
花落未央
花落未央 2020-12-02 13:42

I am trying to determine the distance from a point to a polygon in 2D space. The point can be inside or outside the polygon; The polygon can be convex or concave.

If

相关标签:
5条回答
  • 2020-12-02 14:09

    I do not know about the difference in performance with respect to the rest of answers, but in boost C++ libraries there is an generic implementation called distance. It has information about complexity in every case and in your problem case it is linear.

    I was also looking for solutions to this problem some days ago and I want to share this finding. Hope it helps to someone.

    0 讨论(0)
  • 2020-12-02 14:10

    Your best bet is to iterate over all the lines and find the minimum distance from a point to a line segment.

    To find the distance from a point to a line segment, you first find the distance from a point to a line by picking arbitrary points P1 and P2 on the line (it might be wise to use your endpoints). Then take the vector from P1 to your point P0 and find (P2-P1) . (P0 - P1) where . is the dot product. Divide this value by ||P2-P1||^2 and get a value r.

    Now if you picked P1 and P2 as your points, you can simply check if r is between 0 and 1. If r is greater than 1, then P2 is the closest point, so your distance is ||P0-P2||. If r is less than 0, then P1 is the closest point, so your distance is ||P0-P1||.

    If 0<r<1, then your distance is sqrt(||P0-P1||^2 - (r * ||P2-P1||)^2)

    The pseudocode is as follows:

    for p1, p2 in vertices:
    
      var r = dotProduct(vector(p2 - p1), vector(x - p1))
      //x is the point you're looking for
    
      r /= (magnitude(vector(p2 - p1)) ** 2)
    
      if r < 0:
        var dist = magnitude(vector(x - p1))
      else if r > 1:
        dist = magnitude(vector(p2 - x))
      else:
        dist = sqrt(magnitude(vector(x - p1)) ^ 2 - (r * magnitude(vector(p2-p1))) ^ 2)
    
      minDist = min(dist,minDist)
    
    0 讨论(0)
  • 2020-12-02 14:18

    If you have a working point to line segment distance function, you can use it to calculate the distance from the point to each of the edges of the polygon. Of course, you have to check if the point is inside the polygon first.

    0 讨论(0)
  • 2020-12-02 14:20

    I can help you with this pointers:

    • The distance can be calculated using Wikipedia entry, even with an untested snipped of code.
    • The inside/outside border can be solved with this Stack Post and links

    and some remarks:

    • you should check only the nearest points, as Martin Beckett´s answer point outs, since another segment can "proyected" very near, but in reality don´t be close as need.
    0 讨论(0)
  • 2020-12-02 14:25

    Do you need fast or simple?
    Does it have to be always absolutely correct in edge cases or will good enough most of the time be OK?

    Typical solution are to find the distance to each vertex and find the pair with the smallest values ( note that for a point outside a convex polygon these might not be adjacent) and then check point to line intersections for each segment.

    For large complex shapes you can also store approx polygon bounding boxes (either rectangular or hexagons) and find the closest side before checking more detail.

    You may also need code to handle the special case of exactly on a line.

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