# Distance between a point and a curve

I have the general equation of an elipse and a point P0:

elipse=0.31368-0.113863x-0.00127066x^2+0.00329003y+0.000817666xy+0.0000929297y^2
P0={-6.4,172.0}


I would like to find the point on the curve which has the shortest distance from P0. What would it be the best solution? Is there a way to use Near function not only with tables?

• Does this - stackoverflow.com/questions/22959698/… - give you what you need to answer your own question ? – High Performance Mark Apr 7 '20 at 15:08
• 0.31368-0.113863x-0.00127066x^2+0.00329003y+0.000817666x*y+0.0000929297y^2==0 determines a hyperbola, not an ellipse. – user64494 Apr 7 '20 at 15:25
• This is the result from a FindFit applied to some points, i haven't check the fit yet...I wanted to write the full code before – Jane Apr 7 '20 at 15:39

We can use RegionDistance:

ellipse = 0.31368 - 0.113863 x - 0.00127066 x^2 + 0.00329003 y + 0.000817666 x y + 0.0000929297 y^2;

P0 = {-6.4, 172.0};

RegionDistance[ImplicitRegion[ellipse == 0, {x, y}], P0]

37.5323


The nearest point can be found in a similar fashion:

RegionNearest[ImplicitRegion[ellipse == 0, {x, y}], P0]

{-43.9319, 172.17}

• And is this the x or the y of the point on the ellipse? – Jane Apr 7 '20 at 15:40
• That's the distance. you can use RegionNearest to find the point. – Chip Hurst Apr 7 '20 at 15:45
• could I use RegionNearest when a region is defined by solutions of a given differential problem? – Jane Apr 7 '20 at 17:37
• In principle it can work with anything that is RegionQ. If you have a curve defined by an equation, wrapping it in ImplicitRegion gives a valid input. – Chip Hurst Apr 7 '20 at 17:47

The given conic is not an ellipse. It is a hyperbole.

elipse = 0.31368 - 0.113863 x - 0.00127066 x^2 + 0.00329003 y + 0.000817666 x y + 0.0000929297 y^2
P0 = {-6.4, 172.0};
sol = NMinimize[{(P0 - {x, y}).(P0 - {x, y}), elipse == 0}, {x, y}];
dist = Sqrt[sol[[1]]]

gr1 = ContourPlot[elipse == 0, {x, -100, 50}, {y, 100, 250}, PlotPoints -> 25];
gr2 = Graphics[{Red, PointSize[0.02], Point[P0]}];
grn = Graphics[{Blue, PointSize[0.02], Point[{x, y} /. sol[[2]]]}];
Show[gr1, gr2, grn]

• Alternatively, you could Minimize or NMinimize either Norm[P0 - {x, y}] or EuclideanDistance[P0, {x, y}] – Bob Hanlon Apr 7 '20 at 15:55