# Distance between a line segment and a point equation

This is definitely a duplicate question but I'm not asking for a formula. I have a formula that I found somewhere but I don't understand how it works.

On a (two-dimensional) Euclidean plane, suppose you have a line segment with end points AB and a point C.

The distance of C from AB is (B.x - A.x) * (A.y - C.y) - (B.y - A.y) * (A.x - C.x)

Edit: The objective was to find the shortest distance from the line segment AB and point C, where the distance(line segment) is perpendicular to the line segment AB. We can assume that points A and B are the leftmost and rightmost points, where C is between them

Question: how does this formula meet this objective?

• There doesn't appear to be a question here! Are you asking if that final formula is correct? Since AB is a line segment, not a line there are two different cases: – user247327 Nov 6 '18 at 17:48
• It takes a little investment of time and learning effort, but you can post mathematical notation here. If you wish, I'll replace your formula with a more "mathematical" expression. However as @user247327 points out, the formula cannot be right because there is the case when the closest distance from $C$ to line segment $\overline{AB}$ reaches an endpoint and the case when it reaches the interior of $\overline{AB}$. – hardmath Nov 6 '18 at 18:04
• hardmath, if you could, that would be great. I'm not a maths student and this formula, if you could call it that, I found from code since I'm an amateur software developer – SoftwareEngineerStudent Nov 6 '18 at 18:15
• It’s going to be hard to explain how this formula computes the distance between the point and segment because it doesn’t. For a simple example like $A=(-1,0)$, $B=(1,0)$, $C=(1,1)$, this formula gives $2$, but the actual distance is quite obviously $1$. There is a formula that involves the quotient of some cross products, so I suspect that the one you have here is missing something. – amd Nov 6 '18 at 20:25

## 1 Answer

There doesn't appear to be a question here! Are you asking if that final formula is correct? Since AB is a line segment, not a line, there are three different cases: (1) the perpendicular from C to the line AB lies between A and B. In this case the distance is the distance from C to the foot of that perpendicular. (2) The perpendicular from C to the line lies outside A and B, closer to A than to B. In this case the distance is the distance from C to A. (3) The perpendicular from C to the line lies outside A and B, closer to B than to A. In this case the distance is the distance from C to B.

• I'm sorry, I was looking for an explanation as to what this particular formula does. I found it while reading some code. The objective was to find the shortest distance from the line segment AB and point C, where the distance(line segment) is perpendicular to the line segment AB. We can assume that points A and B are the leftmost and rightmost points, where C is between them – SoftwareEngineerStudent Nov 6 '18 at 18:13
• Oh, my problem is case one. How does this formula solve this case? I've looked at the final formula here: mathworld.wolfram.com/Point-LineDistance2-Dimensional.html and the formula from the question and I don't understand how it can do the same thing from mathworld – SoftwareEngineerStudent Nov 6 '18 at 18:24
• Since it's case 1, I can just represent the line segment as a line, and use the many distance from line to point resources on the web, such as khan academy. But I want to understand how this formula works because somehow it is able to solve it.... – SoftwareEngineerStudent Nov 6 '18 at 18:30