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.


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


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}) $$


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.