Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

/* CS - Series 9 - Exercise Challenge * Program: lindenmayerChallenge_SW.cpp * Author: S. Weidmann (Group E) * Purpose: Draw a beautiful Lindenmayer system */ /* Production rules and meanings: * X -> X + Y + Z * Y -> Y + Z + X * Z -> Z + X + Y * +: ifm::left(120) * X: ifm::forward(), restore every third step * Y: Nothing * Z: ifm::forward(), restore every tenth step * starting word: X Y Z * best result: 14 iterations * should output: a fractal butterfly */ #include #include /* To set maximum stack size (if ever needed): * ulimit -s $(ulimit -Hs) */ // Macros, for laziness #define plus ifm::left(angle); #define X x(n); #define Y y(n); #define Z z(n); void x(unsigned iterations); void y(unsigned iterations); void z(unsigned iterations); const unsigned angle = 120; const unsigned preferredIterations = 14; void x(unsigned n) { static unsigned count = 1; if (n == 0) { if (count == 3) { ifm::save(); } ifm::forward(); if (count == 3) { ifm::restore(); count = 1; } ++count; return; } --n; X plus Y plus Z; return; } void y(unsigned n) { if (n == 0) { return; } --n; Y plus Z plus X; return; } void z(unsigned n) { static unsigned count = 1; if (n == 0) { if (count == 10) { ifm::save(); } ifm::forward(); if (count == 10) { ifm::restore(); count = 1; } ++count; return; } --n; Z plus X plus Y; return; } int main() { unsigned n; std::cout << "Please enter the number of iterations (best result: " << preferredIterations << ")\n"; std::cin >> n; X Y Z; return 0; }