Search code examples
pythonoptimizationscipyminimization

Finding distance from closest point in curve to any arbitrary point in bounded n dimensional space in Python


I am working with Python for this problem.

Say I have some point p and a 1-dimensional arbitrary curve in an n-dimensional (compact) space. How can I find the closest point in the curve to my designated point p? I found an answer in Find minimum distance from point to complicated curve, but Shapely only works on planes and the expressions for the curves I am working with reside in spaces whose number of dimension ranges from 2 to 16 due to the number of parameters defining the curves. The expressions of these curves are always known explicitly.

I also tried using scipy.optimize with SLSQP to minimize the distance function, but it is not always working. For example if the curve is np.sin(15*x), and the points are only in the unit square centered at (0.5, 0.5) there are parts of the curve that are inside the square in only one of the two dimensions and the minimization fails for some points.


Solution

  • If you know analytical form of curve, you always know the distance from point x(t), y(t) to your external point. And you can write distance in analytical form. Thus, you need to find derivative from distance expression and find roots.