Calculating a point between two points on a slant line with a distance $d$

geometrytrigonometry

I have points $(x_1, y_1)$ and $(x_2, y_2)$ on a line. I need to calculate the point $(x_3, y_3)$ which is some distance $d$ away from $(x_1, y_1)$ . I had calculated the slope using the formula,
$$
m = \frac{y_2-y_1}{x_2-x_1}
$$

where $m$ is the slope

enter image description here

Then I used the slope to calculate the angle $\theta = \arctan(m)$. With the angle and start and end points I used the following formula to calculate the coordinates of the point $(x_3, y_3)$
$$
\begin{split}
x_3 &= x_2 + d\sin \theta\\
y_3 &= y_2 + d\cos \theta
\end{split}
$$

I tried to plot the point I obtained. However, the angle of the new line always seems wrong and I am not sure where I am making the mistake, slope or the angle formula? This is the line generated after the calculations, the $(x_3, y_3)$ seems to be with completely wrong angles.

enter image description here

Best Answer

Given $p_1,p_2$ and computed $D = \|p_1-p_2\|=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$ you can construct $p_3$ as follows

$$ p_3 = p_1 + \frac{d}{D}(p_2-p_1) $$

or

$$ \cases{ x_3 = x_1+\frac{d}{D}(x_2-x_1)\\ y_3 = y_1+\frac{d}{D}(y_2-y_1)\\ } $$