gator
gator

Reputation: 3523

Can an arithmetic expression be passed as argument to a function to describe the logic therein?

I'm working on visualizing the Mandelbrot set as well as a few other fractals and there's a lot of duplicated code but no code reuse.

One of the functions I am using is below:

/**
 * determines whether a pixel lies in the set
 * @params x, y - x and y coordinates on R/I axes
 * @param c - a complex number
 */
void calculateSet(int x, int y, Complex c) {
    Complex z = c.clone();
    int n = 0;
    for (; n < maxDepth; n++) {
        if (z.dis() > 4) { break; }
        z = z^2 + c;
    }
    // some code using n to color the set
}

This follows the Mandelbrot set:

z_(n+1) = z_n^2 + c

But look at the relevant code for the Burning Ship set:

void calculateSet(int x, int y, Complex c) {
    Complex z = c.clone();
    int n = 0;
    for (; n < maxDepth; n++) {
        if (z.dis() > 4) { break; }
        z = abs(z)^2 + c; // ***
    }
    // follows z_(n+1) = abs(z_1)^2 + c
}

All the code save for the starred line is identical. Right now I have separate classes for Mandelbrot, BurningShip, and a few others with the only difference being that one line.

Is there a way to define this expression and pass to a generalized Set class?

Some pseudocode:

class Set {
    // ...
    Set(Type expression) {
        // ...
        // x, y, c initialized
        // ...
        calculateSet(x, y, c, expression);
    }
    void calculateSet(int x, int y, Complex c, Type e) {
        Complex z = c.clone();
        int n = 0;
        for (; n < maxDepth; n++) {
            if (z.dis() > 4) { break; }
            z = e;
        }
    }
};

And I can just use Set to describe any kind of set I wish?

Set mandelbrot = Set(Type("z^2 + c"));
Set burningship = Set(Type("abs(z)^2 + c"));
// etc

I could use if/else statements to have just one class, but it's not generalized.

Upvotes: 0

Views: 486

Answers (3)

molbdnilo
molbdnilo

Reputation: 66451

You can make a template, with the function as template argument.
I believe this is the method that provides the most inlining opportunities.

typedef Complex (*Function)(const Complex&, const Complex&);

template<Function fn>
class Set
{
    // ...
    void calculateSet(int x, int y, Complex c) {
        Complex z = c;
        int n = 0;
        for (; n < maxDepth; n++) {
            if (z.dis() > 4) { break; }
                z = fn(z, c)
            }
        // some code...
    }
}

Complex mandelbrot_fn(const Complex& z, const Complex& c)
{
    return z^2 + c;
}

Complex burning_fn(const Complex& z, const Complex& c)
{
    return abs(z)^2 + c;
}


Set<mandelbrot_fn> mandelbrot;
Set<burning_fn> burning_ship;

Upvotes: 2

druckermanly
druckermanly

Reputation: 2741

Since you're limited to C++03, you can use a function pointer relatively painlessly.

Complex mandlebrotCompute(Complex z, Complex c) {
  return z*z + c;
}

void calculateSet(int x, int y, Complex c, Complex (*func)(Complex, Complex)) {
    Complex z = c.clone();
    int n = 0;
    for (; n < maxDepth; n++) {
        if (z.dis() > 4) { break; }
        z = func(z, c);
    }
}

It is used like the following:

Complex foo;
calculateSet(1, 2, foo, mandlebrotCompute);

It might help make the code cleaner to use a typedef for the function pointer.

Upvotes: 5

user3520616
user3520616

Reputation: 101

That is what lambdas are for I guess.

template<typename Lam>
class Set
{
private:
  Lam lam;

public:
  Set (Lam&& lam) : lam(lam) {}
  void calculateSet(int x, int y, Complex c)
  {
    Complex z = c.clone();
    int n = 0;
    for (; n < maxDepth; n++) {
      if (z.dis() > 4) { break; }
      z = lam(z, c);
    }
  }
};

You can use this class like this:

auto mandelbrot = Set([](Complex z, Complex c) -> Complex {
  return (z*z) + c;
});


auto burningShip = Set([](Complex z, Complex c) -> Complex {
  return abs((z*z)) + c;
});

mandelbrot.calculateSet(...);
burningShip .calculateSet(...);

Upvotes: 1

Related Questions