# How to project a point onto a line?

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 = vecY / vecX

Point on vector (0,0)

So axis line: y=(vecY/vecX) * x

Perp m = -(vecX/vecY)

So perpendicular line that crosses (x1, y1)

Y=mx + c

Y= - (vecX / vecY) * x + C

y1= - (vecX / vecY) * x1 + C

y1 – C = - (vecX / vecY) * x1

-C = - (vecX / vecY) * x1 – y1

C = (vecX / vecY) * x1 + y1

So the perpendicular line is:

Y = - (vecX / vecY) * x + (vecX / vecY) * x1 + y1

Intersect between axis and perpendicular line:

(vecY/vecX) * x = - (vecX / vecY) * x + (vecX / vecY) * x1 + y1

(vecY/vecX) * x + (vecX / vecY) * x = (vecX / vecY) * x1 + y1

( (vecY/vecX) + (vecX / vecY) )* x = (vecX / vecY) * x1 + y1

x = ( (vecX / vecY) * x1 + y1) / ( (vecY/vecX) + (vecX / vecY) )

y = (vecY/vecX) * x

so

y = (vecY/vecX) * ( (vecX / vecY) * x1 + y1) / ( (vecY/vecX) + (vecX / vecY) )

Is this right? (bold bits are x and y of the point of intersection with the axis line)

I have attempted to rewrite the last lines with MathJax:

$x = \frac{x1(\frac{vecX}{vecY}) + y1}{\frac{vecY}{vecX} + \frac{vecX}{vecY}}$

$y = \frac{\frac{vecY}{vecX} ( x1(\frac{vecX}{vecY}) + y1)}{( \frac{vecY}{vecX} + \frac{vecX}{vecY} )}$

• You find the point where the line passing through $(x,y)$ and perpendicular to your line meets the line. – uniquesolution Jun 28 '18 at 15:00
• It would be a little easier to read if you used MathJax (see math.stackexchange.com/help/notation), and this is not the easiest way to do the calculation, but the result looks correct to me. Try to simplify it so that there is only one division operation in each of the formulas for $x$ and $y,$ and I think you'll end up with the usual formulas. – David K Jun 29 '18 at 13:28
• @DavidK I'm not sure how to simplify from here, what are the usual formulas? – Toxic Tom Jun 30 '18 at 0:03
• You have fractions within fractions. You can "clear" most of the fractions by multiplying the numerator of the outermost fraction by $vecX \cdot vecY,$ and also multiplying the denominator of that fraction by the same amount so that the value of the fraction is not changed. It helps if you write fractions using horizontal lines rather than diagonal lines; it makes it much easier to see what the outermost fraction is--it's the one with the line that goes all the way across. – David K Jun 30 '18 at 0:58
• By the way, rather than something like vecX, a neater notation is $v_X,$ which is less likely to be mistaken for something completely unintended such as $v \times e \times c \times X.$ – David K Jun 30 '18 at 1:00

$\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$

• That's great, thanks! Do these formulas get much more complicated if the vector passed through a point other than the origin? for example (x2, y2)? – Toxic Tom Jul 3 '18 at 22:12
• It's not much more complicated. I've added the formula for other lines to the answer. – David K Jul 3 '18 at 22:26