How to project a point onto a line?

5.2k Views Asked by At

How do I project a point (x, y) onto a line represented by a vector $[vecX, vecY]$?

My efforts so far:

Projecting points onto a line:

Point $(x1, y1)$ and vector $[vecX, vecY]$

$y=mx + c$

$m = \dfrac{vecY}{vecX}$

Point on vector $(0,0)$. So axis line: $y=\dfrac{vecY}{vecX} * x$

Perpendicular $m' = -\dfrac{vecX}{vecY}$

So the perpendicular line that crosses $(x1, y1)$

\begin{align} y &=m'x + c'\\ y1 &= - \dfrac{vecX}{vecY} * x1 + c' \\ y1 – c' &= - \dfrac{vecX}{vecY} * x1 \\ -c' &= - \dfrac{vecX}{vecY} * x1 – y1 \\ c' &= \dfrac{vecX}{vecY} * x1 + y1 \end{align}

So the perpendicular line is: $y = - \dfrac{vecX}{vecY} * x + \dfrac{vecX}{vecY} * x1 + y1$

Now the intersect between axis and perpendicular line:

\begin{align} \dfrac{vecY}{vecX} * x &= - \dfrac{vecX}{vecY} * x + \dfrac{vecX}{vecY} * x1 + y1\\ \dfrac{vecY}{vecX} * x + \dfrac{vecX}{vecY} * x &= \dfrac{vecX}{vecY} * x1 + y1\\ \left( \dfrac{vecY}{vecX} + \dfrac{vecX}{vecY} \right)* x &= \dfrac{vecX}{vecY} * x1 + y1\\ x &= \dfrac{\dfrac{vecX}{vecY} * x1 + y1}{ \dfrac{vecY}{vecX} + \dfrac{vecX}{vecY}} \end{align} And $y = \dfrac{vecY}{vecX} * x$,

Hence,

\begin{align} y = \dfrac{vecY}{vecX} * \dfrac{\dfrac{vecX}{vecY} * x1 + y1}{ \dfrac{vecY}{vecX} + \dfrac{vecX}{vecY}} \end{align}

Is this right?

1

There are 1 best solutions below

2
On BEST ANSWER

$\require{begingroup} \begingroup \newcommand{vx}{vecX} \newcommand{vy}{vecY} \newcommand{xi}{x1} \newcommand{yi}{y1} \newcommand{xii}{x2} \newcommand{yii}{y2} $The formulas in the question are correct, provided that line identified by the vector $[\vx,\vy]$ is a line through the origin. You can simplify the result with some algebraic manipulation, for example:

\begin{align} x &= \frac{\xi\left(\frac{\vx}{\vy}\right) + \yi} {\frac{\vy}{\vx} + \frac{\vx}{\vy}}\\ &= \frac{\left(\xi\left(\frac{\vx}{\vy}\right) + \yi\right)\vx\cdot\vy} {\left(\frac{\vy}{\vx} + \frac{\vx}{\vy}\right)\vx\cdot\vy}\\ &= \frac{\left(\xi\cdot\vx + \yi\cdot\vy\right)\vx}{\vy^2 + \vx^2}\\ &= \left(\frac{\xi\cdot\vx + \yi\cdot\vy}{\vy^2 + \vx^2}\right)\vx,\\[1ex] y &= \frac{\frac{\vy}{\vx}\left(\xi\left(\frac{\vx}{\vy}\right) + \yi\right)} {\frac{\vy}{\vx} + \frac{\vx}{\vy}} \\ &= \frac{\frac{\vy}{\vx} \left(\xi\left(\frac{\vx}{\vy}\right) + \yi\right)\vx\cdot\vy} {\left( \frac{\vy}{\vx} + \frac{\vx}{\vy} \right)\vx\cdot\vy}\\ &= \frac{\vy \left(\xi\cdot\vx + \yi\cdot\vy\right)}{\vy^2 + \vx^2}\\ &= \left(\frac{\xi\cdot\vx + \yi\cdot\vy}{\vy^2 + \vx^2}\right)\vy. \end{align}

Now we can see that the same factor, $\frac{\xi\cdot\vx + \yi\cdot\vy}{\vy^2 + \vx^2},$ occurs prominently in both the formula for $x$ and the formula for $y.$ In fact, we can write the formulas like this:

\begin{align} p &= \frac{\xi\cdot\vx + \yi\cdot\vy}{\vy^2 + \vx^2},\\ x &= p\cdot\vx,\\ y &= p\cdot\vy. \end{align}

If the line does not go through the origin, but instead goes through a point $(\xii,\yii),$ you can subtract $\xii$ and $\yii$ from the coordinates of both points to translate the problem so the line goes through the origin; solve the translated problem; and then add $\xii$ and $\yii$ to the resulting point's coordinates to translate it back. That is,

\begin{align} p &= \frac{(\xi - \xii)\vx + (\yi - \yii)\vy}{\vy^2 + \vx^2},\\ x &= p\cdot\vx + \xii,\\ y &= p\cdot\vy + \yii. \end{align}


There is another way to get this formula. If you are familiar with the usual inner product (aka "dot product") of linear algebra on vectors of two real numbers, you may recognize that the inner product of the vectors $[\xi,\yi]$ and $[\vx,\vy]$ is $$ [\xi,\yi] \cdot [\vx,\vy] = \xi\cdot\vx + \yi\cdot\vy, $$ while the square of the magnitude of the vector $(\vx,\vy)$ is $$ \lVert[\vx,\vy]\rVert^2 = \vy^2 + \vx^2. $$

We also know about the inner product that $$ [\xi,\yi]\cdot [\vx,\vy] = \lVert[\xi,\yi]\rVert \lVert[\vx,\vy]\rVert \cos\theta, $$ where $\theta$ is the angle between the vectors $[\xi,\yi]$ and $[\vx,\vy].$ The origin $(0,0),$ the point $(\xi,\yi),$ and the desired projected point $(x,y)$ make a right triangle with angle $\theta$ at the origin, and the length of the side adjacent to the origin is $\lVert[x,y]\rVert = \lVert[\xi,\yi]\rVert \cos\theta.$ So the formulas work out to this: we start with the vector $[\vx,\vy],$ which has length $\lVert[\vx,\vy]\rVert.$ We want a vector of length $\lVert[\xi,\yi]\rVert \cos\theta$ in the same direction. So first we multiply $[\vx,\vy]$ by a factor of $[\xi,\yi]\cdot [\vx,\vy],$ which gives us a vector of length $(\lVert[\xi,\yi]\rVert \lVert[\vx,\vy]\rVert \cos\theta) \lVert[\vx,\vy]\rVert$ in the desired direction; then multiply by $1/\lVert[\vx,\vy]\rVert^2$ to cancel out the unwanted factors $\lVert[\vx,\vy],$ leaving a vector of length $\lVert[\xi,\yi]\rVert \cos\theta$ as desired. The result is $$ [x,y] = \frac{[\xi,\yi]\cdot [\vx,\vy]}{\lVert[\vx,\vy]\rVert^2} [\vx,\vy]. $$ and the remaining steps (to get to the two-equation form without the linear-algebra notation) are to write the $x$ and $y$ component separately and to convert $[\xi,\yi]\cdot [\vx,\vy]$ and $\lVert[\vx,\vy]\rVert^2$ into algebraic formulas as shown above.$\endgroup$