[Math] Calculate shortest distance between random point and line

geometry

Considering the following situation:

case number one

The red dot is located at $(0,0)$ and the blue dot is at $(0,-2)$. A black line is crossing the red dot and is rotated at $\alpha = 45°$ relative to the red dot. With this information, how can I find the length of the yellow dashed line?

My first intuition is to calculate the distance $d$ between the two dots using:

$$d = \sqrt{(x_2 – x_1)^2 + (y_2 – y_1)^2}$$

The next step should be to calculate the distance between red dot and the point where the black line and yellow dashed line cross. But I can't seem to find how to do that, and even then, I am not sure of what to do next.

The problem gets worse since the methodology to find the distance of the yellow dashed line should work for different cases in which the blue dot can be located somewhere else.

For example, the following image has the situation in which the black line is still rotated at $\alpha = 45°$, the red dot is still at $(0,0)$ but the blue dot is located at $(-1,-2)$

case number two

The ultimate goal is to find a methodology that enables calculating the length of the yellow dashed line for any location of the blue dot but also any degree of rotation of the black line knowing that the red dot will always be at $(0,0)$. An example of such situation is shown on the picture under:

enter image description here

Here the red dot is still at $(0,0)$, the blue dot is at $(-2,-2)$ but the black line is rotated by $\alpha = 60°$ relative to the red dot.

Is it even possible to achieve this?

Best Answer

The shortest distance between the blue point and the given line is measured along the perpendicular to that line through the blue point. Geometrically, this is a basic compass-and-straightedge construction.

Analytically, there are several ways to go about this. We’ll do it by translating the geometric construction above into analytic terms. Assume for the moment that the given line is neither vertical nor horizontal. Its equation is $y=-x\tan\theta$, where $\theta$ is the rotation angle. The negative sign appears because you’re taking positive angles as clockwise, which is the opposite of the usual sign convention for a right-handed coordinate system. Let the coordinates of the blue point $B$ be $(x_b,y_b)$. Two lines are perpendicular if the product of their slopes is $-1$, therefore the equation of the perpendicular line through $B$ is $y-y_b = (x-x_b) \cot\theta$. Now, solve for the intersection point, giving $$ x=(x_b\cos\theta-y_b\sin\theta) \cos\theta \\ y=(y_b\sin\theta-x_b\cos\theta) \sin\theta.$$ You can then use the formula for the distance between two points to get, after some simplification, $|x_b\sin\theta+y_b\cos\theta|$ for the distance from $B$ to the line.

We can’t use this derivation for a horizontal or vertical line because one of the slopes is infinite, but a slight modification to the equation of the line handles those cases, too. Expand $\tan\theta$ as ${\sin\theta\over\cos\theta}$ in the equation of the line, multiply through by $\cos\theta$ and rearrange to get $x\sin\theta + y\cos\theta = 0$. A similar transformation produces $(x-x_b)\cos\theta-(y-y_b)\sin\theta=0$ as the equation of the perpendicular. The rest of the derivation proceeds the same way as above.

Notice that the distance of $B$ from the line is just the absolute value of what you get from plugging its coordinates into the more general equation of the line. This can be generalized: Given the equation of any line $ax+by+c=0$, not necessarily passing through the origin, the distance of a point $(x_0,y_0)$ from it is given by $${|ax_0+by_0+c| \over \sqrt{a^2+b^2}}.$$ I’ll leave proving this more general formula to you. One way to prove it follows the same steps as the above construction for a line through the origin.

Related Question