// Prog: lindenmayer.cpp // Draw turtle graphics for the Lindenmayer system with // nontrivial production F -> F+F+ and start word F #include #include #include // POST: returns the production of c std::string production (const char c) { switch (c) { case 'F': return "F+F+"; default: return std::string (1, c); // trivial production c -> c } } // POST: replaces all symbols in word according to their // production and returns the result std::string next_word (const std::string& word) { std::string next; for (unsigned int k = 0; k < word.length(); ++k) next += production (word[k]); return next; } // POST: draws the turtle graphic interpretation of word void draw_word (const std::string& word) { for (unsigned int k = 0; k < word.length(); ++k) switch (word[k]) { case 'F': ifmp::forward(); // move one step forward break; case '+': ifmp::left(90); // turn counterclockwise by 90 degrees break; case '-': ifmp::right(90); // turn clockwise by 90 degrees } } int main () { std::cout << "Number of iterations =? "; unsigned int n; std::cin >> n; std::string w = "F"; // w_0 for (unsigned int i = 0; i < n; ++i) w = next_word (w); // w_i -> w_{i+1} draw_word (w); // draw w_n return 0; }