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

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

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.

• makes sense to me – gt6989b Apr 5 '20 at 15:13
• I think one possible cause of mistake is that the range of $\arctan$ function is $(-\pi/2, \pi/2)$. However your $\theta$ should assume values in the range $[0, 2 \pi)$ to account for different orientations. One way is to manually correct the value obtained in by $\arctan$ to the correct quadrant. For example, if $m = -1$, $\arctan(-1) = \pi/4$. However, your $\theta$ can also be $3\pi/4$ so you should also try with that value and see if it fits on your line. – sudeep5221 Apr 5 '20 at 15:48

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)$$
$$\cases{ x_3 = x_1+\frac{d}{D}(x_2-x_1)\\ y_3 = y_1+\frac{d}{D}(y_2-y_1)\\ }$$