This is a followup to Draw contour plots (level curves) for a function with known derivatives using a different function z = F(x, y). The basic approach is the same. See that example for the details. This HowTo discusses some of the difficult issues that can arise for some functions.
This example uses the ellipsoid:
x^2/a^2 + y^2/b^2 + z^2/c^2 = d^2
This isn't a function F(x, y). Solving for z gives:
z = +/- Sqr(d^2 - x^2 / a^2 - y^2 / b^2)
The program deals only with the positive solution. The function's partial derivatives are:
dFdX = -c^2 * x / a^2 / Sqr(c^2 * (d^2 - x^2 / a^2 - y^2 / b^2))
dFdY = -c^2 * y / b^2 / Sqr(c^2 * (d^2 - x^2 / a^2 - y^2 / b^2))
If you try to use the method demonstrated by the previous example, you'll run into two problems. First, the function is not defined for all values of X and Y. It is also not differentiable for all (X, Y) and is not differentiable for all (X, Y) for which F(X, Y) is defined (Right on the edge of the ellipsoid, the partial derivatives require a division by zero). To avoid these problems, the program must never try to calculate a value or derivative outside of the safe area.
The second problem is that the gradient grows large near the edges of the differentiable area. The program starts with a point and follows the gradient to find a point on the surface with a particular Z value. If the point strays too close to the edge, the gradient may push the point out of the differentiable area so the program fails to find the point it needs.
To avoid this problem, when the program is pushed out of the safe area it should reduce the distance it moves along the gradient repeatedly until the next point is in the safe area. The program will need a cutoff to ensure that it stops if the point really is out of the safe area (so it doesn't keep sneaking closer and closer to the edge without giving up).
Note that this example doesn't solve this problem. Its code tries to draw red contours for Z = 0 and Z = 1/10 but the program falls off the edge and the curves are not drawn.
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.