Question
In python using guizero drawing app please 5.13 Spirograph (Lab 5.2) In this exercise, you will use the GuiZero Drawing widget to generate pictures like
In python using guizero drawing app please
5.13 Spirograph (Lab 5.2)
In this exercise, you will use the GuiZero Drawing widget to generate pictures like those which can be made with a spirograph set. A Spirograph is formed by rolling a circle inside or outside of another circle. The pen is placed at any point on the rolling circle. See http://en.wikipedia.org/wiki/Spirograph .
Download this template code and rename it spirograph.py.
- template-drawing.py
We will use iterative development to develop a program that allows a user to generate spirograph pictures.
Iteration 1
Implement the following algorithm:
- Import the tkinter and math libraries.
- Create the GuiZero app and drawing.
- Set moving_radius equal to a floating point value read from the user for the radius of the moving circle.
- Set fixed_radius equal to a floating point value read from the user for radius of the fixed circle.
- Set pen_offset equal to a floating point value read from the user for the offset of the pen point in the moving circle.
- Set color equal to a string value read from the user for the color of the pen.
- Set center to be half the drawing width.
- Draw the spirograph. (Save the implementation of this step for a later iteration.)
Implement this non-functional part of the algorithm before proceeding. Here is a sample execution, with the required prompts and no other output.
moving radius: 55.0 fixed radius: 53.0 pen offset: 77.0 color: blue
Iteration 2
A spirograph picture can be drawn by using these parametric equations:
In these equations:
- t is the current time.
- R is the radius of the fixed circle (i.e., the fixed_radius).
- r is the radius of the moving circle (i.e., the moving_radius).
- p is the offset of the pen point in the moving circle (i.e., the pen_offset).
- center is half the width (or height) of the (square) drawing ( i.e., app.width / 2).
To draw a spirograph, you will need to do two things, both of which will be coded in the space you left in the algorithm specified above. First, you need to get set up and then you need to use a loop to draw lines between points over time. Do this as follows.
- Import the tkinter and math libraries.
- Create the GuiZero app and drawing.
- Set moving_radius (r in the equations) equal to a floating point value read from the user for the radius of the moving circle.
- Set fixed_radius (R in the equations) equal to a floating point value read from the user for radius of the fixed circle.
- Set pen_offset (p in the equations) equal to a floating point value read from the user for the offset of the pen point in the moving circle.
- Set color equal to a string value read from the user for the color of the pen.
- Set center equal to half the drawing width.
- # First, do the spirograph setup.
- Set x and y to the appropriate initial position, i.e., use the equations given above to compute x(0) and y(0) (n.b., because t is 0.0, sin(0) is 0 and cos(0) is 1, the x-y coordinates boil down to R + r + p + center and 0.0 + center).
- # Then, draw the spirograph.
- Set t equal to 0.0.
- Repeat while the t is less than 360.
- Increment t by 0.1.
- Compute a value for next_x using the t as the value of time. Use the equations specified above.
- Compute a value for next_y using the t as the value of time.
- Draw a line from (x, y) to (next_x, next_y), using drawing.line() and setting the color to color.
- Set x and y equal to next_x and next_y respectively (so that they can be used in the next repetition).
Note that this algorithm produces the spirograph image all at once. If you would like to see a slower animation of the drawing process, add the following lines to the end of the loop:
app.update() sleep(0.01)
Assuming you have carefully followed the algorithm, you should be able to test your code now. For example, you might try these values:
- 55.0, 53.0, 77.0, blue
- 25.0, 23.0, 77.0, red
- 79.0, 74.0, 38.0, gold
Here is the output for the first example (55, 53, 77, 'blue').
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