How to describe this fractal with a recursive function?

243 Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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:

> cantor:=proc(x,a,b, depth)   if depth=0 then 0
> elif (x <= evalf(a + (1/(3))*(b - a))) then
> cantor(x, a, a + (1/(3))*(b - a), depth - 1);
> elif (evalf(a + (2/(3))*(b - a)) <= x) then
> cantor(x, a + (2/(3))*(b - a), b, depth - 1);
> else depth/totalDepth;
> fi; end:

Gives (modulo a scaling factor):

with(plots);
totalDepth:=4;
for depth from 1 to totalDepth do
 plot('cantor'(x,0,1,depth),x=0..1);
 od;

enter image description here enter image description here enter image description here enter image description here