Let's say I have an arbitrary linear line described by $y=mx+b$. I also have a point $P(x_1,y_1)$ that is not on that line. I suppose $P$ can be anywhere relative to the line. How can I find the vector $v$ the stretches from the point to the line (and is perpendicular to it)?
Here's what I tried: I figured that the difference in the $x$ and $y$ components ($\delta y$ and $\delta x$) basically describe a vector twice as large as $v$ but in the same direction. So I tried finding $\delta x$ and $\delta y$ by the following:
$\delta y=y_1-(m\cdot x_1 +b)$
$\delta x = x_1-(y_1/m - b/m)$
The vector V is then $[\delta x/2, \delta y/2]$. I divide by two because I figure the vector described by $[\delta x, \delta y]$ goes all the way to the top left corner of the imaginary rectangle that is completed by $\delta x$ and $\delta y$.
Am I correct? If not, why? Regardless if I am, is there an alternative or smarter way to accomplish this? I would appreciate any input here.
First I will write the line $l$, defined by $y=mx+b$, as a parametric vector equation,
$$ l = \left\{\vec{u}_0 + \lambda \vec{u}_1, \lambda \in \mathbb{R}\right\}, \tag{1} $$
where $\vec{u}_0$ and $\vec{u}_1$ are equal to,
$$ \vec{u}_0 = \begin{pmatrix} 0 \\ b \end{pmatrix}, \tag{2} $$
$$ \vec{u}_1 = \begin{pmatrix} 1 \\ m \end{pmatrix}. \tag{3} $$
This notation will be useful when projecting an arbitrary point $P$ onto the line. Any point on $l$ can be seen as a scaled version of $\vec{u}_1$ with its origin at $\vec{u}_0$. The point $P$ can be seen as a vector with its origin at $\vec{0}$. Both origins have to be the same in order to be able to project $P$ onto the line. This can be done by adding $\vec{u}_0$ to its origin and subtracting $\vec{u}_0$ from $P$, which is visualized in the following figure:
The projection of $P$ into $l$, denoted with $P'$ with its origin at $\vec{0}$, can be found with,
$$ P' = \frac{(P - \vec{u}_0) \cdot \vec{u}_1}{\vec{u}_1 \cdot \vec{u}_1} \vec{u}_1 + \vec{u}_0. \tag{4} $$
When writing $P$ as $(x_p,y_p)$ and using equation $(1)$ and $(2)$ for $\vec{u}_0$ and $\vec{u}_1$, then $P'$ can be simplified to,
$$ P' = \frac{(x_p + m (y_p - b)}{1 + m^2} \begin{pmatrix} 1 \\ m \end{pmatrix} + \begin{pmatrix} 0 \\ b \end{pmatrix}. \tag{5} $$
The vector $\vec{v}$, with its origin at $P'$ and point to $P$, can be found with,
$$ \vec{v} = P - P'. \tag{6} $$