Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem Overview: Drawing Dragon Curves A dragon curve is a fascinating mathematical object, with many interesting properties. We're mostly going to use it as an
Problem Overview: Drawing Dragon Curves A dragon curve is a fascinating mathematical object, with many interesting properties. We're mostly going to use it as an exercise to draw pictures using turtle graphics, but if you want to learn more, you can't go wrong starting with Wikipedia's introduction. How to get started We have provided a partially complete Python program to get you started a3q1-starter.py. You can find it on the Moodle page as a resource for Assignment 3. Download it and add it to your Python project We've also provided a very small module called makedragon that produces the information you'll need to draw a dragon. You can download the module from the course Moodle page. This is not a built-in Python module! It should be placed in the same project or folder as your a3q1-starter.py. It can be imported the same way as any Python module, so long as it is in the same folder as a3q1-starter.py. You can look at this file if you want, but you don't need to understand the contents of this file to complete the assignment The starter code imports this module as dragonsegg. The function dragon) in the makedragon module takes one argument, which must be a positive integer This integer describes the level of the the dragon you want, and should be in the range from O to about 18 (if you give an integer that's any higher than this, you might have to wait a very long time). The function returns a string consisting of only of the characters 'F'(meaning move Forward), L (meaning turn Left 90 degrees), and 'R' (meaning turn Right 90 degrees). This string describes the actions that the turtle needs to take to draw a dragon. For example, calling dragon (4) returns the string 'FRFRFLFRFRFLFLFRFRFRFLFLFRFLFLF'. You will use the string to tell the turtle where to go, and what to draw. Notice that 'F' alternates with either 'L' or 'R' There are never two , F, in a row. If we ignore the 'F*. we can see one, two, or three ,R, in a row, but never more. And sometimes we see one, two, or three 'L in a row (again, ignoring 'F). The string starts with two 'R. and ends with two 'L in a row The starter file a3q1-starter.py defines a function called dra) where you'll add your code to draw the dragon. The rest of the file creates a few variables and calculates a few necessary values. Near the bottom of the file is a function call to drav) In the dra) function, there is a comment that tells you to write some code to draw the dragon curve. Use a loop! Look at each character in the string: 'F' means move forward. This is where the drawing happens. The amount you move forward deter- mines how big the dragon will appear on the screen 'L' means turn the turtle left (clockwise) 90 degrees. No drawing happens here: it's just changing the direction of the turtle R' means turn the turtle right (counter-clockwise) 90 degrees How to colour your dragon First, just get the dragon to appear on the screen without worrying about the colour. Any colour will do. When you can correctly draw the dragon curve. you can think about how to colour it We want you to colour your dragon using the following arbitrary rule: Every time the string changes from L' to 'R or from 'R' to 'L' (ignoring the 'F' between them). change the pen colour to a new random colour. This rule is a bit tricky. so think about strings and indices carefully! A colourized dragon curve with level 4 should resemble the following diagram Problem Overview: Drawing Dragon Curves A dragon curve is a fascinating mathematical object, with many interesting properties. We're mostly going to use it as an exercise to draw pictures using turtle graphics, but if you want to learn more, you can't go wrong starting with Wikipedia's introduction. How to get started We have provided a partially complete Python program to get you started a3q1-starter.py. You can find it on the Moodle page as a resource for Assignment 3. Download it and add it to your Python project We've also provided a very small module called makedragon that produces the information you'll need to draw a dragon. You can download the module from the course Moodle page. This is not a built-in Python module! It should be placed in the same project or folder as your a3q1-starter.py. It can be imported the same way as any Python module, so long as it is in the same folder as a3q1-starter.py. You can look at this file if you want, but you don't need to understand the contents of this file to complete the assignment The starter code imports this module as dragonsegg. The function dragon) in the makedragon module takes one argument, which must be a positive integer This integer describes the level of the the dragon you want, and should be in the range from O to about 18 (if you give an integer that's any higher than this, you might have to wait a very long time). The function returns a string consisting of only of the characters 'F'(meaning move Forward), L (meaning turn Left 90 degrees), and 'R' (meaning turn Right 90 degrees). This string describes the actions that the turtle needs to take to draw a dragon. For example, calling dragon (4) returns the string 'FRFRFLFRFRFLFLFRFRFRFLFLFRFLFLF'. You will use the string to tell the turtle where to go, and what to draw. Notice that 'F' alternates with either 'L' or 'R' There are never two , F, in a row. If we ignore the 'F*. we can see one, two, or three ,R, in a row, but never more. And sometimes we see one, two, or three 'L in a row (again, ignoring 'F). The string starts with two 'R. and ends with two 'L in a row The starter file a3q1-starter.py defines a function called dra) where you'll add your code to draw the dragon. The rest of the file creates a few variables and calculates a few necessary values. Near the bottom of the file is a function call to drav) In the dra) function, there is a comment that tells you to write some code to draw the dragon curve. Use a loop! Look at each character in the string: 'F' means move forward. This is where the drawing happens. The amount you move forward deter- mines how big the dragon will appear on the screen 'L' means turn the turtle left (clockwise) 90 degrees. No drawing happens here: it's just changing the direction of the turtle R' means turn the turtle right (counter-clockwise) 90 degrees How to colour your dragon First, just get the dragon to appear on the screen without worrying about the colour. Any colour will do. When you can correctly draw the dragon curve. you can think about how to colour it We want you to colour your dragon using the following arbitrary rule: Every time the string changes from L' to 'R or from 'R' to 'L' (ignoring the 'F' between them). change the pen colour to a new random colour. This rule is a bit tricky. so think about strings and indices carefully! A colourized dragon curve with level 4 should resemble the following diagram
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started