I want to determine the distance between a point and a line (represented by two points).

I found the following calculation on Wikipedia, and cannot determine how it works. It's seems simple, but for some reason the explanation eludes me.

The following Java snippet provide distance from point P to the line that passes through A-B:

public double pointToLineDistance(Point A, Point B, Point P)
 double length = Math.hypot(B.x - A.x, B.y - A.y);
 return Math.abs((P.x - A.x) * (B.y - A.y) - (P.y - A.y) * (B.x - A.x)) / length;

Normal length is calculating the the distance from point A to B (the red line) by determining the x and y differences in the two points and using the Pythagorean theorem.

Example graph

(P.x - A.x) - green line

(B.y - A.y) - b

(P.y - A.y) - blue line

(B.x - A.x) - a

I'm not sure why we multiply the respective distances, perform subtraction, and then divide by the length of the red line.

Presumably it has to do with: $$\frac{|a\cdot x+b\cdot y+c|}{\sqrt{a^2+b^2}}$$

but I'm just not connecting the two.


According to bubba, the code isn't implementing the equation above. Bubba gave a good explanation, but I had two questions and a request in response to his answer:

  • How/why do we know that d is equal to that?
  • Why do we then divide the cross product by the length?
  • Would you mind running through the example?

2 Answers 2


The code isn't really using the formula you gave.

Instead, the reasoning in the code is as follows:

Let $\theta$ be the angle between the vector $P-A$ and the vector $B-A$. Then $d = \|(P-A)\| \sin\theta$.

But, also, we have, from the definition of the cross product: $$\|(P-A)\times(B-A)\|=\|(P-A)\|\cdot\|(B-A)\|\sin\theta$$ A little bit of algebra then gives us: $$d = \frac{\|(P-A)\times(B-A)\|}{\|(B-A)\|}$$ That's the formula used in the code.

The first line calculates $length = \|(B-A)\|$, and then the second line calculates the cross product divided by this $length$.

  • $\begingroup$ How/why do we know that d is equal to that? Why do we then divide the cross product by the length? Would you mind running through the example? It really helps me to see values plugged in. $\endgroup$
    – Ryan
    Commented Sep 16, 2012 at 17:32
  • $\begingroup$ It will be obvious if you draw a picture. It just comes from the relationship sine = opposite/hypotenuse. $\endgroup$
    – bubba
    Commented Sep 21, 2012 at 23:57

The link:Wiki-Distance from line to a point, should answer the first 2 bullets - See the section "Algebraic Proof".

As for an example you have asked for, I will use the points you gave.

point $(m,n)=(3,4)$

Line's equation is:

$$ax + by + c$$

from the points $(3,5)$ and $(1,2)$, you can find the line equation to be:

$$-3x + 2y - 1 = 0$$

as a result

$$a=-3, b=2, c=-1, m=4, n=3$$






enter image description here

  • $\begingroup$ Thank you. Why does the code use subtraction when determining the numerator, but you use addition? $\endgroup$
    – Ryan
    Commented Sep 18, 2012 at 23:14
  • $\begingroup$ The code is using the vector approach. The above formula is the Algebraic approach. If you want to see a good explanation of the Algebraic approach, please have a look at: intmath.com/plane-analytic-geometry/… $\endgroup$
    – NoChance
    Commented Sep 18, 2012 at 23:36
  • $\begingroup$ Oh, ok. Thank you. That is a very helpful link! I read through the whole thing. Though I'm actually looking to understand why the vector approach used in the code works. I apologize if that wasn't clear. $\endgroup$
    – Ryan
    Commented Sep 19, 2012 at 2:45
  • $\begingroup$ I suggest you start a new question and tag it with "vectors" to focus on a vector solution. I am not good enough with vectors. $\endgroup$
    – NoChance
    Commented Sep 19, 2012 at 6:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .