The distance betwen a point P and a line AB is the length of the segment that crosses the line, is perpendicular to it, and passes through P:
To compute it, assume that we have the locations of two points on the line: (x1,y1) and (x2,y2). In another article of this series, we saw that a vector v that is perpendicular to this line will have the following coordinates:

The distance d is the length of the projection of vector AP onto this vector v:
And this projection in turn is given by:
Here's a Java snippet that computes this distance:
public double pointToLineDistance(Point A, Point B, Point P) { double normalLength = Math.sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y)); return Math.abs((P.x-A.x)*(B.y-A.y)-(P.y-A.y)*(B.x-A.x))/normalLength; }