How can i find the closest perimeter point from a given point on any edge of a polygon

geometry

How can I find the closest edge point to a given point X, where X is a point either on the edge or at a vertex of a polygon? There are lots of answers and ways of finding this for points inside polygons, but I'm looking to find an answer for edge to edge.

2 Examples:

  1. I want to find the closest point on the polygon perimeter from vertex E
  2. I want to find the closest point on the polygon perimeter from random point on the DE line.

Note: neither of the closest points in this example are on the same edge as either of the two points given, but easily could be in another example.

I'm currently using the Shapely python library for all of my other calculations, but cannot seem to find any way to get to this kind of solution.

enter image description here

Best Answer

Assuming you know the point's coordinates and which line segment (or segments, if it's a vertex) the point is located on, you just need to calculate the minimum distance from the point to each remaining line segment. You can see how to calculate this minimum distance here. The minimum of these minimums will then be the closest point.