Writing an implicit equation as $f(x,y) = 0$, the closer $f(x,y)$ is to $0$ the closer a point is to the graph of the implicit equation. For $y^x = x \cos(xy)$, $f(x,y)$ would be $y^x - x \cos(xy)$, for example.
Simply using $f(x,y)$ to calculate the distance results in a very uneven estimate; the faster $f(x,y)$ is changing, the smaller the estimated distance. To compensate for this I divide by the magnitude of the rate of change in $x$ and $y$:
$d(x,y) = \dfrac{f(x,y)}{\sqrt{\left(\frac{\partial}{\partial x} f(x,y)\right)^2 + \left(\frac{\partial}{\partial y} f(x,y)\right)^2}}$
This is the best approximation for the distance $d$ that I've come up with that estimates distance from a point to a general implicit curve. When graphing $d(x,y) = r$ where $r$ is the approximate distance from $(x,y)$ to the graph of $f(x,y) = 0$, $d(x,y) = r$ should be approximately the same distance from $f(x,y) = 0$ at each nearest pair of points.
It seems accurate enough visually for most functions, for some functions like $e^x$ are particularly troublesome. For small values of $r$ the image looks acceptable but for values > 1 the distance gets incorrectly shrunken for $x > 0$, which blows the graph way out of proportion when $x > 0$.
Is there a way to do this more accurately?
Edit: The motivation for this is to rasterize user-defined implicit functions, so the distance function will be sampled for each pixel in the image.