Chapter+5.9 11+Notes
Chapter+5.9 11+Notes
In the previous notes we introduced two standard library modules turtle and random. Another
commonly used standard library module is the math module. The math module has several
mathematical library functions and named constants. The two widely used named constants are pi and
e, representing the value of π=3.141592653589793 and the natural exponent
e=2.718281828459045 .
Table 5-2 of the Textbook has list of commonly used library functions: acos(x), asin(x), atan(x), ceil(x),
cos(x), degrees(x), exp(x), floor(x), fabs(x), hypot(x), log(x), log10(x), radians(x), sin(x), sqrt(x) and tan(x).
Each function accepts a single argument. Note that fabs(x) will return the absolute value of x, similar to
the built-in function abs(x).
Similar to the random and turtle modules, to use the standard library functions and named constants
defined in the math module, your program must include the import statement:
import math
As mentioned in previous notes, a module is a file that contains Python code. A module will typically
contain function definitions and global named constants if properly modularized.
Example
Write a program that will draw an object of a square with an inscribed filled circle at any given location
in turtle graphics window; calculate and display the area between the square and inscribed circle using
2 π
the formula area=s (1− ). The program should include:
4
1. a function that will accept the following arguments:
a. x,y representing the location of the object (the top leftmost corner of the square.
s
b. s representing the size of the square, radius of the circle = .
2
c. c representing the filled color.
d. t representing the turtle
The function use the turtle t to draw the square of size s with inscribed filled circle at (x,y) and
with color c. Clear the all objects associated with the turtle t before drawing the new object.
2. a function that will accept the size s of the square, the function will calculate and display the
area of the region between the square and the circle using the formula above.
For draw the object you can use the forward function to advance forward s pixels, turn right and
advance forward s pixels again, repeat until the turtle is back to (x,y). Check the heading of the turtle to
ensure it is in the 0.0 direction (pointing eastward).
To draw the filled circle, the turtle must be moved to (x, y + s/2) and set turtle heading to -90 degrees.
def area_square_circle_region(s):
'''Calculate and return the area region between a square
and the inscribed circle.
s - the size of the square
'''
return s**2 *(1-pi/4)
Save the code above in a module – square_inscribed_circle.py. Write a program that will animate the
display of an object of a square with an inscribed circle using the function above. Use import statement
to include the function in your program.
Generate the object position randomly and move the object on the screen. Also change the color each
time using red, green and blue values between 0 and 255.. Use the time.sleep(0.2) to delay the turtle’s
movement by 0.2 seconds. The sleep function is defined in the time module.
def simulate():
#set the colormode of the red, green and blue to
#the range of 0 and 255.
colormode(255)
Write a program that will draw the pattern below with randomly generated filled colors.
import inscribed_circle as ic
from random import randint
from turtle import Turtle, colormode, hideturtle
def simulate():
#set the colormode of the red, green and blue to
#the range of 0 and 255.
colormode(255)
simulate()