# How do I plot a distance map in $R^2$ to a curve given in form $y=f(x)$?

As in the title, I'd like to display image using DensityPlot representing distance to a given curve, e.g., $y=x^2$.

I find it difficult to derive distance function to from a an arbitrary point to a curve (even for such simple quadratic function).

Is it possible to tell Mathematica to find numerically the distance to a given function from every $x,\,y$ location?

• Not the easiest thing to do. Could use "fast marching" to get points of constant distance from the given curve, and color them accordingly. A notebook with some code can be found here. Aug 14 '18 at 20:42
• Interesting approach, over kill tho :) thanks anyway! Aug 14 '18 at 20:47

Maybe

rd = RegionDistance @ ParametricRegion[{x, x^2}, {{x, 0, 1}, {y, 0, 1}}];

ContourPlot[Evaluate@rd[{x, y}], {x, 0, 1}, {y, 0, 1},
Contours -> 10, Exclusions -> None,
Epilog -> {Plot[x^2, {x, 0, 1}, PlotStyle -> Directive[Thick, Red]][]}] • Simply beautiful, thank you @kglr :) Aug 14 '18 at 22:57
• @Anonymous, my pleasure. Thank you for the accept.
– kglr
Aug 14 '18 at 23:06
• I found another way, simpler to type but way harder for Mathematica: func[x_] := x^2; dist[x_, y_] := Sqrt[Minimize[(x - z)^2 + (y - func[z])^2, z][]]; ContourPlot[dist[x, y], {x, 0, 1}, {y, 0, 1}, AspectRatio -> Automatic] Aug 15 '18 at 19:45