[Math] Shortest Distance Between Point and Line

geometry

I am working on a collision detection problem.

I wish to measure the distance from a point to a line segment.

i.e.
https://en.wikipedia.org/wiki/Distan…_by_two_points

I need to know the point on the line segment that the point (not on the line) is closest to.

i.e.

say the line segment is defined by

x1,y1 = 10,10

and

x2,y2 = 15,20

and there is a point at 16,16

how far is the shortest distance from the point to the line?

and where on the line is this shortest distance?

kind regards
W

Best Answer

As you say, we have 3 points, $A(10,10)$, $B(15,20)$, and $C(16,16)$.

The distance formula says that the distance between $A(x_1,y_1)$ and $B(x_2,y_2)$ is $$\sqrt {(x_2-x_1)^2+(y_2-y_1)^2}$$

Here, the distance $AB$ is $5\sqrt5$

Similarly, $AC$ is $6\sqrt2$

And $BC$ is $\sqrt{17}$

Since the perpendicular is the smallest distance, let a point $D(x,y)$ on $AB$ such that $AB\underline{|} CD$, we can form 2 right triangles $\delta ADC$ and $\delta BDC$

We already know that $AB$ is $5\sqrt5$

Let $CD=x$

Applying the Pythagorean theorem:-

$AD=\sqrt{72-x^2}$

And

$BD=\sqrt{17-x^2}$

Since $AB=AD+BD$

Therefore

$$5\sqrt5=\sqrt{72-x^2}+\sqrt{17-x^2}$$

Solving, $x={6\over\sqrt5}$

Therefore, here the smallest distance is $6\over\sqrt5$

Related Question