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

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

/* * Name: Spiral * Author: Andreas J. Weiss * Date: 15.11.2013 * Description: * Lindenmayer system with 4 words, and iterated mapping. * Recommended use of 20 to 25 iterations. 21 is Optimal. */ #include #include #include #define FORWARD ifm::forward(); #define LEFT(A) ifm::left(A); #define RIGHT(A) ifm::right(A); // Variables unsigned int map_counter = 0; // Prototypes void A(unsigned int i); void B(unsigned int i); void C(unsigned int i); void D(unsigned int i); // Enumerate the words enum { WA, WB, WC, WD, WLast }; // Dictionary void (*Word[WLast])(unsigned int) = {A, B, C, D}; // Map iteration void Map(unsigned int i) { Word[ map_counter++ % WLast ](i); } void A(unsigned int i) { if( i <= 0 ) return; FORWARD Map(i - 1); FORWARD Map(i - 1); } void B(unsigned int i) { if( i <= 0) return; Map(i - 1); LEFT(90) FORWARD Map(i - 1); LEFT(90) } void C(unsigned int i) { if( i <= 0) return; Map(i - 1); RIGHT(90) Map(i - 1); RIGHT(90) FORWARD } void D(unsigned int i) { if( i <= 0) return; LEFT(1) Map(i - 1); RIGHT(1) Map(i - 1); } int main (int argc, char **argv) { // Allow the iteration count to be passed as an argument if(argc == 2) { std::cout << "Number of iterations = " << atoi(argv[1]) << "\n"; Word[0](atoi(argv[1])); } else { std::cout << "Number of iterations =? "; unsigned int n; std::cin >> n; Word[0](n); } return 0; }