Many other questions ask about how to compute the distance between a point and a line, but my question is slightly different:
how can I compute the separation vector (maybe called the height?) between a point and a line?
Many other questions ask about how to compute the distance between a point and a line, but my question is slightly different:
how can I compute the separation vector (maybe called the height?) between a point and a line?
The following answer is general and works in $\mathbb{R}^n $, and more in general in euclidian spaces where the scalar product is defined.
Line as a parametric equation
Say that your line is specified by the parametric equation: $$ l = \{\mathbf{q} + {\mathbf{\hat n}} \,t \,\forall \,t \mbox{ in }\mathbb{R} \} $$ for some point $\mathbf q$ and a versor $\mathbf {\hat n}$, and say that we want to compute the separation vector $\mathbf{s}$ between the point $\mathbf{r}$ and the line $l$.
We can just take the separation vector between $\mathbf r$ and $\mathbf q$ and subtract its projection on the line from it, like this:
$$ \mathbf{s} = (\mathbf{r}-\mathbf{q}) - ((\mathbf r - \mathbf q)\cdot \mathbf {\hat n})\mathbf{\hat n}\,. $$
Line defined by two points that lie on it
If you don't have an equation for your line, but all you know is that it passes between two points (let's call them $\mathbf {x}_1$ and $\mathbf {x}_2$), then the parametric equation for your line is simply $$ l = \left\{\mathbf{x}_1 + \frac{(\mathbf{x}_2-\mathbf{x}_1)}{||\mathbf{x}_2-\mathbf{x}_1||} \,t \,\forall \,t \mbox{ in }\mathbb{R} \right\} $$ so that the solution above becomes $$ \mathbf{s} = (\mathbf{r}-\mathbf{x}_1) - \frac{[(\mathbf r - \mathbf x_1)\cdot (\mathbf{x}_2-\mathbf{x}_1)](\mathbf{x}_2-\mathbf{x}_1)}{||\mathbf{x}_2-\mathbf{x}_1||^2}\,. $$
If someone has the time to make a picture feel free to add it below the answer.