Get closest Point from P1 to P2 when I can only go a distance d

Unfortunately, I don't know much about this topic, that's why I apologize for my language. I have two points P1 and P2, now I want "to get from one to the other", but I can only go a distance d. And I want to get as close as possible from P1 to P2.

Here is an example:

d=400, P1(0,0), P2(500,500)

Now the formula should give me P3(282.843, 282.843).

This works with the formula $$d = \sqrt{(x_2-x_1)^2 + (y_2-y_1)^2}$$ but this only works if x and y of P3 are equal, and I can't control where P2 is.

I hope you understand what I mean. This might be a very stupid question, but I don't know how to search for a solution. Maybe you can help me with a keyword for this problem, formatting the answer right or a solution.

Thank you

• will it not just be on the line joining those two points? then you can easily find the coordinates... Commented May 14, 2021 at 16:27
• Find the distance from $x\to y$, divide the max distance by it, call that quotient $t$. The answer is $tp_2+(1-t)p_1$. Commented May 14, 2021 at 16:28
• @Aditya_math You are right, thanks. Sometimes its so easy. Commented May 14, 2021 at 16:37
• But is there a way, to get this from a formuala, like x,y=...? Maybe @DonThousand has the solution, but I dont understand what he means. Commented May 14, 2021 at 16:41
• Or is there a way to calculate on which point I am, when I go e.g. 400 "steps" on the graph? Commented May 14, 2021 at 16:53

We travel along the straight line $$x=y$$ where as this line interporlate the two points. We are interested with the case when $$x>0$$.

$$2x^2=d^2$$

$$x=\frac{d}{\sqrt2}=200\sqrt2$$

Hence the solution is $$(200\sqrt2, 200\sqrt2)$$

• Thanks. Does this also work if x != y? Commented May 15, 2021 at 12:26
• use the same idea that it lies on the straight line, the rest are computational details. Commented May 15, 2021 at 12:29
• Okay, Thank you Commented May 15, 2021 at 12:29

Although Siong Thye Goh already provided the correct answer, I wanted to show the answer explicitly in parametric form.

Let $$\vec{p}_0$$ be the starting point, $$\vec{p}_1$$ the target, and $$d$$ the distance one can travel.

If $$d \ge \lVert \vec{p}_1 - \vec{p}_0 \rVert$$, one can travel from $$\vec{p}_0$$ to $$\vec{p}_1$$. Otherwise, one can travel only to $$\vec{p}$$, $$\vec{p} = \vec{p}_0 + \left( \vec{p}_1 - \vec{p}_0 \right) \frac{d}{\left\lVert \vec{p}_1 - \vec{p}_0 \right\rVert} \tag{1}\label{G1}$$ where $$\lVert\vec{v}\rVert = \sqrt{\vec{v} \cdot \vec{v}} = \sqrt{\sum_i v_i^2}$$ is the Euclidean norm (length) of vector $$\vec{v}$$.

In two dimensions, with $$\vec{p} = (x, y)$$, $$\vec{p}_0 = (x_0, y_0)$$, and $$\vec{p}_1 = (x_1, y_1)$$, $$\lambda = \frac{d}{\sqrt{ (x_1 - x_0)^2 + (y_1 - y_0)^2 }}$$ and \left\lbrace \begin{aligned} x &= (1 - \lambda) x_0 + \lambda x_1 \\ y &= (1 - \lambda) y_0 + \lambda y_1 \\ \end{aligned} \right. and in three dimensions, $$\lambda = \frac{d}{\sqrt{ (x_1 - x_0)^2 + (y_1 - y_0)^2 + (z_1 - z_0)^2 }} \\$$and\left\lbrace \begin{aligned} x &= (1 - \lambda) x_0 + \lambda x_1 \\ y &= (1 - \lambda) y_0 + \lambda y_1 \\ z &= (1 - \lambda) z_0 + \lambda z_1 \\ \end{aligned} \right. noting that $$(1-\lambda) x_0 + \lambda x_1 = x_0 + \lambda ( x_1 - x_0 )$$; where $$\lambda$$ is just a temporary parameter ("lambda", for "length scale factor").

In a computer program that uses floating-point numbers the first form is preferable, because the latter form can suffer from domain cancellation. When say $$x_1$$ is large in magnitude and $$x_0$$ relatively small in magnitude (so much closer to zero), $$x_1 - x_0 = x_1$$ using floating_point numbers due to the finite precision! Then, at very small $$\lambda$$ ($$d$$ very small compared to the distance between $$\vec{p}_0$$ and $$\vec{p}_1$$), $$x = 0$$ and not $$x_0$$. Similarly for the case when $$x_0$$ is close to zero, and $$x_1$$ is very large, and for the other Cartesian coordinates. Using $$(1 - \lambda)$$ and $$\lambda$$ avoids the domain cancellation (because it calculates the contribution of each point separately), so this is precise near both the start and end points, regardless of the numerical magnitudes.