0

I am trying to find the shortest linestring between two points. There is a constraint that there is an n-sided polygon possibly directly between the 2 points. I am not allowed to cross through the polygon but only pass through its edges.

eg.

start = (2,0)
end = (0,1)
poly = [(1,0),(1,1),(1,2),(2,1)]

passing it through the function would output 2.41

so far I have

from shapely.geometry import LineString, Polygon, Point

def shortest_linestring(start, end, poly):
    poly = Polygon(poly)
    p1 = Point(start)
    p2 = Point(end)

but I am completely stumped as to what to do next. Any hint would be appreciated.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Zito Relova
  • 1,041
  • 2
  • 13
  • 32
  • First figure out how to do this mathematically, and then people may be able to help you implement the algorithm. Just some thoughts- the shortest distance between the points is a straight line. Starting from the initial point, you can draw this line and find where the it intersects the polygon, then follow it around to the other intersection (there are 2 ways, pick the shorter one), and continue on the original line to the end point. – pault Jan 28 '18 at 16:25
  • I was thinking this also, but what if the polygon were shaped concave inwards say like a crescent moon or pacman-shaped? It would try to go through it directly only to hit the concave directly and go around, rather than go around it in the first place. – Zito Relova Jan 28 '18 at 23:29

0 Answers0