# Find a third point along a line at a given distance from point 2.

I know the positions of Point 1 and Point 2 on a 2D plane/graph. I want to find the coordinates of a third point that is X distance away from Point 2, in a straight line connecting all 3 points. So that no matter where the first two points are, in any direction, I want to create Point 3 at X distance away from Point 2, and be able to know the coordinates.

I'm not very good at maths and haven't been able to find an answer for this specific type of problem.

If your first point is $$(a_1,b_1)$$ and your second point is $$(a_2,b_2)$$, then you are looking for a solution of the system$$\left\{\begin{array}{l}y=\frac{b_2-b_1}{a_2-a_1}(x-a_1)+b_1\\(x-a_2)^2+(y-b_2)^2=X^2.\end{array}\right.$$It should be clear geometrically that this system has two solutions.

• This might be correct but I can't say for sure. Turns out I was asking the wrong question as I needed programming help not geometry. Thanks anyway. Commented Jul 8, 2019 at 4:09

By substituting, I obtain: $$(( b_2−b_1)/ (a_2−a_1) * (x−a_1)+b_1 – b_2 )^2 = X_2 - (x−a_2)^2$$

Now we need to express x using this...scamble

$$(( b_2−b_1)/ (a_2−a_1)*x - (( b_2−b_1)/(a_2−a_1) * a_1)-b_1–b_2) ) ^2 = X^2 - x^2+ 2xa_2 - a_2^2$$

now we need to reduce the square also on the left side, which is super annoying and long:

(( b2−b1)/ (a2−a1))²x² - 2 * (b2−b1)/ (a2−a1)x (( b2−b1)/(a2−a1) * a1)-b1–b2) + (( b2−b1)/(a2−a1) * a1)-b1–b2)² = X² - x²+ 2x*a2 - a2²

Next we can try to combine the x² and x together by shifting all to one side of the equation:

((( b2−b1)/ (a2−a1))² - 1 ) * x² - 2 * ((b2−b1)/(a2−a1) * (( b2−b1)/(a2−a1) * a1)-b1–b2) + a2) * x - X² = 0

Now we have a "simple" equation of the type:

a * x² + b * x + c = 0

And it will then have 2 intercepts (geometrically but also mathematically, as there are two potential products that can yield this equation. If we use the quadratic formula (which we don't need to demonstrate), the 2 solutions will be:

x = (-b - √(b² - 4 ac))/2a

or

x = (-b + √(b² - 4 ac))/2a

Now we only have to substitute a and b with the values from our equation.

Since you are coding, I suggest using a two step approach, define your x solutions as per the above and then define a and b as the calculated coefficients from above, or better even, you could have even more intermediary variables that avoid the need for large expressions.

If I didn't make any mistakes, it should give you the two possible x values.

Then you can use the x values to define y.

And you can also define from which point the solution should be closest, which will reduce your 2 possible value to only one!

Please report if you find any issues with my math, it's been a long time!

I think maybe using code you can avoid the complicated workings and instead of working out the complicated coefficients, you could just work with the pre-calculated real numbers and amnipulate those instead, it should simplify this a great deal.