0% found this document useful (0 votes)
3 views

Chapter+5.9 11+Notes

The document introduces the math module in Python, detailing its constants and functions for mathematical operations. It provides an example program using the turtle graphics library to draw a square with an inscribed circle and calculate the area between them. Additionally, it includes instructions for creating a module and simulating random object placements with varying colors using turtle graphics.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter+5.9 11+Notes

The document introduces the math module in Python, detailing its constants and functions for mathematical operations. It provides an example program using the turtle graphics library to draw a square with an inscribed circle and calculate the area between them. Additionally, it includes instructions for creating a module and simulating random object placements with varying colors using turtle graphics.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Math Module and More Turtle Graphics

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

You can specify the functions you want to import.

from math import sin, pi, cos

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.

from math import pi


def square_circle(x,y,s,c,t):
'''The function will draw a square with an inscribed filled circle:
(x,y) - the top leftmost corner of the square
s - the size of the square
c - circle filled color
t - the turtle
'''
#goto the the assigned location
t.penup()
t.goto(x,y)
t.pendown()

#set heading to eastward (positive x-axis)


t.setheading(0)

#draw the square


t.forward(s)
t.right(90)
t.forward(s)
t.right(90)
t.forward(s)
t.right(90)
t.forward(s)

#move turtle to the mid size of the square


t.penup()
t.goto(x, y-s//2)

#set heading to southward (negative y-axis)


t.setheading(-90)
t.pendown()

#draw a filled circle


t.fillcolor(c)
t.begin_fill()
t.circle(s//2)
t.end_fill()

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.

#import the inscribed_circle module


import inscribed_circle as ic

from random import randint


from turtle import Turtle, colormode, hideturtle
from time import sleep

def simulate():
#set the colormode of the red, green and blue to
#the range of 0 and 255.
colormode(255)

#number of times to draw object


epochs = 20

#the size of the square


size = 90

#hide the origin started turtle


hideturtle()

#start a new turtle


t = Turtle()
#draw inscrible circle at random locations
while epochs>0:
#randomly generate x,y coordinates
x,y = randint(-200, 200), randint(-300, 300)

#randomly generate red, green and blue values


r,g,b = randint(0,255), randint(0,255), randint(0,255)

#assign the values to a tuple color object.


c = (r,g,b)
print(c)

#clear the turtle of all objects each time


t.clear()

#called the square_circle


ic.square_circle(x,y,size,c,t)
epochs -= 1

#set the delay time to draw the next object


sleep(0.2)
simulate()

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)

#number of times to draw object


epochs = 20

#the size of the square


size = 100

#hide the origin started turtle


hideturtle()

#start a new turtle


t = Turtle()

#draw inscribed circle at random locations


#the coordinates of a standard window is -400,-300, 400, 300. The size is (800, 600)
for x in range(-300,300,100):
for y in range(-200, 400, 100):

#randomly generate red, green and blue values


r,g,b = randint(0,255), randint(0,255), randint(0,255)

#assign the values to a tuple color object.


c = (r,g,b)
print(c)

#called the square_circle


ic.square_circle(x,y,size,c,t)

simulate()

You might also like