This lab will follow examples from the wonderful book "The Algorithmic Beauty of Plants (ABOP)". This book is available free at the link, and is well worth perusing. We will be generating plants using the grammars and approach summarized in Figure 1.24, "Examples of plant-like structures generated by bracketed OL systems", from that book. For this problem, you will implement a class called PLANT. The class has two methods: (1) An initializer. The function will take an initial state (string), a generator (dictionary), the number of generation iterations to run (n) and an angle delta (deltaTheta) for changing direction while drawing. When the class is initialized, you must run the generator with the specified parameter, and make the resulting string available as a member variable PLANT.str . To run the generator, every character in the input string is either (a) replaced by the corresponding value from the generator dictionary if it is in the generator dictionary, or (b) copied directly to the output string if it is not in the generator dictionary. This is repeated n times. The resulting string is stored in the PLANT.str member variable. For example: myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) myPlant.str =='abaababa' --> True and myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) myPlant.str=='FF[+F[+X]F[-X]+X]FF[-F[+X]F[-X]+X]+F[+X]F[-X]+X' --> True (2) A drawPlant method (no parameters). The draw method works as follows: start with a currentPt=(200,0). This is the current draw location. start with a theta of 90 degrees any alphabet character e.g. ('A','b','X') moves forward one step, drawing a line from currentPt to nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)). ***This is the only drawing code you should use. Do not call plt.show() or run any other test code except in your testing __main__ block ***: plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='black') After drawing, set currentPt=nextPt. '[' saves current drawing state [currentPt, theta] on a stack (see Python Stack for details). ']' resets the currentPt and theta to the most recently saved drawing state from the stack. '+' increases the current drawing angle theta by deltaTheta degrees. '-' decreases the current drawing angle theta by deltaTheta degrees. The sample output provided in the file abop_1_24_a.png downloadable here was generated using the grammar given in figure 1.24(a) of ABOP. The sample image was generated using plt.axis('image') as well. Your plot from e.g. the main section does not need to look exactly like the sample png - just be sure to only use the plot command as given above. If in doubt, check with zyBooks.
11.30 Final Project -- Algorithmic Beauty of Plants
This lab will follow examples from the wonderful book "The Algorithmic Beauty of Plants (ABOP)". This book is available free at the link, and is well worth perusing. We will be generating plants using the grammars and approach summarized in Figure 1.24, "Examples of plant-like structures generated by bracketed OL systems", from that book.
For this problem, you will implement a class called PLANT. The class has two methods:
(1) An initializer. The function will take an initial state (string), a generator (dictionary), the number of generation iterations to run (n) and an angle delta (deltaTheta) for changing direction while drawing. When the class is initialized, you must run the generator with the specified parameter, and make the resulting string available as a member variable PLANT.str .
To run the generator, every character in the input string is either (a) replaced by the corresponding value from the generator dictionary if it is in the generator dictionary, or (b) copied directly to the output string if it is not in the generator dictionary. This is repeated n times. The resulting string is stored in the PLANT.str member variable.
For example:
myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) myPlant.str =='abaababa' --> Trueand
myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) myPlant.str=='FF[+F[+X]F[-X]+X]FF[-F[+X]F[-X]+X]+F[+X]F[-X]+X' --> True(2) A drawPlant method (no parameters).
The draw method works as follows:
-
start with a currentPt=(200,0). This is the current draw location.
-
start with a theta of 90 degrees
-
any alphabet character e.g. ('A','b','X') moves forward one step, drawing a line from currentPt to nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)).
***This is the only drawing code you should use. Do not call plt.show() or run any other test code except in your testing __main__ block ***:
plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='black')After drawing, set currentPt=nextPt.
-
'[' saves current drawing state [currentPt, theta] on a stack (see Python Stack for details).
-
']' resets the currentPt and theta to the most recently saved drawing state from the stack.
-
'+' increases the current drawing angle theta by deltaTheta degrees.
-
'-' decreases the current drawing angle theta by deltaTheta degrees.
The sample output provided in the file abop_1_24_a.png downloadable here was generated using the grammar given in figure 1.24(a) of ABOP.
The sample image was generated using plt.axis('image') as well. Your plot from e.g. the main section does not need to look exactly like the sample png - just be sure to only use the plot command as given above. If in doubt, check with zyBooks.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps