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?

  • 2
    $\begingroup$ 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. $\endgroup$ – Daniel Lichtblau Aug 14 '18 at 20:42
  • $\begingroup$ Interesting approach, over kill tho :) thanks anyway! $\endgroup$ – Anonymous Aug 14 '18 at 20:47


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]][[1]]}]

enter image description here

  • $\begingroup$ Simply beautiful, thank you @kglr :) $\endgroup$ – Anonymous Aug 14 '18 at 22:57
  • $\begingroup$ @Anonymous, my pleasure. Thank you for the accept. $\endgroup$ – kglr Aug 14 '18 at 23:06
  • $\begingroup$ 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][[1]]]; ContourPlot[dist[x, y], {x, 0, 1}, {y, 0, 1}, AspectRatio -> Automatic] $\endgroup$ – Anonymous Aug 15 '18 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.