Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

  1. Import the tkinter and math libraries.
  2. Create the GuiZero app and drawing.
  3. Set moving_radius equal to a floating point value read from the user for the radius of the moving circle.
  4. Set fixed_radius equal to a floating point value read from the user for radius of the fixed circle.
  5. Set pen_offset equal to a floating point value read from the user for the offset of the pen point in the moving circle.
  6. Set color equal to a string value read from the user for the color of the pen.
  7. Set center to be half the drawing width.
  8. 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.

  1. Import the tkinter and math libraries.
  2. Create the GuiZero app and drawing.
  3. Set moving_radius (r in the equations) equal to a floating point value read from the user for the radius of the moving circle.
  4. Set fixed_radius (R in the equations) equal to a floating point value read from the user for radius of the fixed circle.
  5. 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.
  6. Set color equal to a string value read from the user for the color of the pen.
  7. Set center equal to half the drawing width.
  8. # First, do the spirograph setup.
  9. 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).
  10. # Then, draw the spirograph.
  11. Set t equal to 0.0.
  12. Repeat while the t is less than 360.
    1. Increment t by 0.1.
    2. Compute a value for next_x using the t as the value of time. Use the equations specified above.
    3. Compute a value for next_y using the t as the value of time.
    4. Draw a line from (x, y) to (next_x, next_y), using drawing.line() and setting the color to color.
    5. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

Examine various types of executive compensation plans.

Answered: 1 week ago

Question

1. What is the meaning and definition of banks ?

Answered: 1 week ago

Question

2. What is the meaning and definition of Banking?

Answered: 1 week ago

Question

3.What are the Importance / Role of Bank in Business?

Answered: 1 week ago