0
$\begingroup$

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

$\endgroup$
2
  • 1
    $\begingroup$ makes sense to me $\endgroup$
    – gt6989b
    Commented Apr 5, 2020 at 15:13
  • $\begingroup$ 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. $\endgroup$
    – sudeep5221
    Commented Apr 5, 2020 at 15:48

1 Answer 1

2
$\begingroup$

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

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .