Suppose that I have two points in the xy plane: pt1
and pt2init
. pt1
is fixed in space, while pt2init
is supplied by the user (but its returned value will in general be different). I wish to write a function that will return the coordinates of a point pt2result
that is located a user-specified distance d
from pt1
along the line connecting pt1
and pt2init
and that is closest to pt2init
.
I wrote the following function f
(and its supporting function dist2D
, which finds the distance between two points with x- and y-coordinates):
dist2D[pt1_, pt2_] :=
Sqrt[(pt2[[1]] - pt1[[1]])^2 + (pt2[[2]] - pt1[[1]])^2]
f[pt1_, pt2_, d_] := Module[{line, x2result, y2result, pt2result},
line = Normal[LinearModelFit[{pt1, pt2}, xvar, xvar]];
Solve[{dist2D[{pt1[[1]], pt1[[2]]}, {x2result, y2result}] == d,
y2result == (line /. xvar -> x2result)},
{x2result, y2result}];
pt2result = {x2result, y2result}
]
But when I run it:
f[{0.77825, 0.551441676}, {0.7075, 0.67398427}]
it does not return anything. Is it possible to do constrained optimization like this?
dist2D[]
is built-in asEuclideanDistance[]
. $\endgroup$