I'm assuming you mean the Euclidean distance. If you're working in the plane, then the answer is simple.
First, compute the equation of the line in the form
ax + by + c = 0
In slope-intercept form, this is the same as
y = (-a/b)x + (-c/b)
Now compute the distance from any point (p,q) to the line by
|a*p + b*q + c| / (a^2 + b^2)^(1/2)
For more than 2 dimensions, it's probably easiest to think in terms of parametrized vectors. This means think of points on the line as
p(t) = (A1 + (B1-A1)*t, A2 + (B2-A2)*t, ..., An + (Bn-An)*t)
where the two points are A = (A1,...,An)
and B = (B1,...,Bn)
. Let X = (X1,...,Xn)
be any other point. Then the distance between X
and p(t)
, the point on the line corresponding to t
, is the square root of
[(A1-X1) + (B1-A1)t]^2 + ... + [(An-Xn) + (Bn-An)t]^2
The distance to the line is the distance to p(t)
where t
is the unique value minimizing this distance. To compute that, just take the derivative with respect to t
and set it to 0
. It's a very straightforward problem from here, so I'll leave that bit to you.
If you want a further hint, then check out this link for the 3-dimensional case which reduces nicely.