DEV Community

tanjiagang
tanjiagang

Posted on

Linear algebra, How to calculate the distance from one point to a line

For this question, we usually think about a formula learned in junior high school:
Image description
but there are some issues where we find it difficult to define the values of A, B, and C, therefore we have a more convenient formula as follows:
float length = dot(ab, ap) / dot(ab, ab) // caculate distance p point to ab line which can generate the projected length of ap line on ab line. This is the projection scale factor. Then multiply ap vector by this length to obtain an ad vector, a line segment on ap. At last, we use lenght() function to compute the distance of pd line segment that is the distance p point to the ab straight line.

Top comments (0)