Given an irregular polygon and a point within that polygon, how do I determine which edge in the polygon is closest to the point?

I will likely have to run this calculation for a large set of points within the polygon (e.g. 50-200 points).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Each step of this algorithm is linear time (O(n)).
Here are the basic formulas for each of the steps:
Calculate closest point on the line that is tangent to each edge of the polygon.
p1 = {x1, y1}.p2 = {x2, y2}.p3 = {x3,y3}.ube the percentage of the distance between p1 and p2, that is needed to find the point on the line formed by p1 and p2, such thatp1+u(p2-p1)= the point on the line that is closest to p3 (the line segment between this point and p3 also happens to be perpendicular to the line going through p1 and p2).u = ((x3 - x1)(x2 - x1)+(y3 - y1)(y2 - y1)) / ((x2 - x1)^2 + (y2 - y1)^2)pu = {xu, yu}xu = x1 + u (x2 - x1)yu = y1 + u (y2- y1)pu = {xu, yu}Calculate closest point on each line segment (edge of the polygon) to the point in question.
The point
puis only the closest point on the line segment when0 <= u <= 1. Otherwise the appropriate endpoint of the line segment is the closest point to the point in question. Thus for eachpu, p1, p2, and ucalculated in the above step do the following:Let pc = {xc, yc}be denoted as the closest point on the line segment of the polygon edge to the point in question.IF u<0 THEN pc = p1ELSE IF u>1 THEN pc = p2ELSE pc = puCalculate the distance from the closest point on each line segment to the point in question.
p3andpc= `sqrt((x3 – xc)^2 + (y3 – yc)^2)Find the minimum distance. The corresponding polygonedge with the minimum distance is the answer.
Here is a diagram to help you understand what the points and terminology in this post represent:
….