# 点到直线距离和投影

Let P be the point with coordinates (x0, y0) and let the given line have equation ax + by + c = 0. Also, let Q = (x1, y1) be any point on this line and n the vector (a, b) starting at point Q. The vector n is perpendicular to the line, and the distance d from point P to the line is equal to the length of the orthogonal projection of \overrightarrow{QP} on n. The length of this projection is given by:
$d = \frac{|\overrightarrow{QP} \cdot \mathbf{n}|}{| \mathbf{n}|}$.
Now,
$\overrightarrow{QP} = (x_0 – x_1, y_0 – y_1)$, so $\overrightarrow{QP} \cdot \mathbf{n} = a(x_0 – x_1) + b(y_0 – y_1)$ and $| \mathbf{n} | = \sqrt{a^2 + b^2}$,
thus
$d = \frac{|a(x_0 – x_1) + b(y_0 – y_1)|}{\sqrt{a^2 + b^2}}$.
Since Q is a point on the line,$c = -ax_1 – by_1$, and so
$d = \frac{|ax_0 + by_0 + c|}{\sqrt{a^2 + b^2}}$.