How to write a function for this fractal?

152 Views Asked by At

How can I write a function to describe this fractal's Nth stage?

I know it should be equal to 1 for some x and 0 for the rest but can't figure the math. I don't know if my drawing is good enough. But I think what I mean is clear. I need to use it in a computer code. and thanks in advance!

1

There are 1 best solutions below

1
On BEST ANSWER

Instead of a formula, a recursive specification is more natural, something like:

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;
}

Start with the interval $[a, b]$ on the $x$-axis and recursively perform the operation:

If an interval lies on the $x$-axis, trisect it and push the middle third up one unit.

The call cantor(x, 0, 1, 6), for example, starts with $[0, 1]$ and returns the height at $x$ after six iterations of the preceding operation.