# Given a point, slope and a distance along that slope, easily (?) find a second point.

I have a point [x1,y1], a slope m of a line that passes through that point. I'd like to find either point [x,y] that is d distance away from that original point.

Work so far:

$$y = m(x - x_1) + y_1$$ $$x = \frac{y + mx_1 - y_1}m$$

And then (if my algebra is correct)

$$d = \sqrt{ \left(\frac{y + mx_1 - y_1} m\right)^2 +y^2}$$ $$y^2 = d^2 - \left(\frac{y + mx_1 - y_1}{m}\right)\left(\frac{y + mx_1 - y_1}{m}\right)$$ And then, if I plugged in some real numbers and struggled long enough, I suppose I could solve for y. And then solve for x. My first attempt ended in a few pages of poorly-remembered math and an incorrect answer.

My question is that this seems like a long slog. Is there an easier way?

More details: The general problem I'm trying to solve for a computer program is given a line segment find a point that is perpendicular and a fixed distance away from the mid-point.

• Using the $x$ perspective instead of the $y$ perspective would allow for less division involved... $$d^2=(m(x-x_1)+y_1)^2+x^2$$ – abiessu Jan 29 '14 at 21:28

## 3 Answers

You can get rid of lots of algebra with a little trigonometry. $m=\tan \theta$, the angle between the $x$ axis and the line. Then $x-x_1=d \cos \theta = d \cos( \arctan (m))=d\frac 1{\sqrt {1+m^2}}$ Similarly $y-y_1=d \sin (\arctan(m))=d\frac m{\sqrt{1+m^2}}$

• Thank you for this. My first attempt was with trig, but it turns out I was even more rusty in my trig that in my geometry. – GSP Jan 30 '14 at 14:27
• Can you please explain. How to use it in programming language. I know (x1,y1) , slope m and distance need to find the (x2,y2). Help please. – Sabish.M Apr 22 '18 at 15:22
• I give you equations for $x,y$. Those are your $x_2,y_2$ – Ross Millikan Apr 22 '18 at 16:31

Nicer solution: you know that the vector $(1, m)$ points in the direction of your line. Divide by $r= \sqrt{1 + m^2}$ to get a unit-length vector $(1/r, m/r)$ that points along the line. Now add $d$ times that vector to $(x, y)$: $$r = \sqrt{1 + m^2}\\ (x_1, y_1) = (x + \frac{d}{r}, y + \frac{d\cdot m}{r})$$

• Perhaps the new point is actually $$(x_1, y_1) = (x + \frac{d}{r}, y + \frac{d\cdot m}{r})$$ given the multiplication of $\frac dr$ times the vector $(1,m)$... – abiessu Jan 29 '14 at 21:38

It might be simpler to work with vectors, here. You can rewrite as a vector equation $$\langle x,y\rangle = \langle x_1,y_1\rangle +t\langle 1,m\rangle,\tag{\star}$$ where $t$ is allowed to vary over all real numbers. Then the distance from $\langle x_1,y_1\rangle$ to a point $\langle x,y\rangle$ on the line is precisely the magnitude of the vector $$\langle x,y\rangle-\langle x_1,y_1\rangle=t\langle 1,m\rangle=\langle t,mt\rangle.$$ Hence, we need $$d=\sqrt{t^2+(mt)^2}=\sqrt{t^2(1+m^2)}=|t|\sqrt{1+m^2},$$ and so we need $$t=\pm\frac{d}{\sqrt{1+m^2}}.$$ Substituting these values of $t$ into $(\star),$ we find that the solutions are $$\left\langle x_1\pm\frac{d}{\sqrt{1+m^2}},y_1\pm\frac{dm}{\sqrt{1+m^2}}\right\rangle.$$

Added: Obviously, this doesn't work with vertical lines, but that's even easier, since only the $y$-value is changing.