2D Plane Distance Calculation

Date
Tag

Distance from a point to a line

If the line passes through two points and , then the distance of to the line is:
notion image
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:
notion image
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