DEV Community

HandsomeTan
HandsomeTan

Posted on • Edited 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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay