Distance from a point to a line
If the line passes through two points and , then the distance of to the line is:
Letβs denote
Then
Thus
Distance from a point to a line segment
Different to distance to a line, projection of the point might not fall on line segment and thus should be calculated differently based on followings 3 cases:
If project point falls on segment , then the distance is , otherwise it will be either or .
Β
Letβs define
Β
Β
And letβs have
If ,
If ,
Thus
When , projection is out of segment and closer to , distance
When , projection is out of segment and closer to , distance
When , projection is on segment, distance , where