I already asked another question about a function for a fractal like this: link And received a well written recursive function with form
double cantor(double x, double a, double b, unsigned int depth)
{
if (depth == 0) // bottom of recursion
return 0;
else if (x <= a + (1.0/3)*(b - a)) // x in first third of interval
return cantor(x, a, a + (1.0/3)*(b - a), depth - 1);
else if (a + (2.0/3)*(b - a) <= x) // x in last third of interval
return cantor(x, a + (2.0/3)*(b - a), b, depth - 1);
else // middle third
return 1;
}
How should I modify it to give this fractal?
If I understand well what you are trying to do, you need modify only the final return value, which in your last else clause should be "depth/totalDepth", instead of 1. I.e., with Maple:
Gives (modulo a scaling factor):