Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from turtle import Turtle, Screen
- from random import randint
- import math #Explore specific import ?
- def circle_area(r):
- return 3.14 * r * r
- def circle_circumference(r):
- return 3.14 * 2 * r
- def draw_circle(t_name, r, col):
- t_name.color(col)
- t_name.dot(2 * r)
- t_name.penup()
- t_name.goto(0,5) #Assumes circle is at 0,0. How might you adapt if you have x,y parameters?
- t_name.pendown()
- t_name.color("black")
- t_name.write("Area: " + str(circle_area(r)), align="center")
- t_name.penup()
- t_name.goto(0,-5)
- t_name.pendown()
- t_name.color("black")
- t_name.write("Circumference: " + str(circle_circumference(r)), align="center")
- #rectangle x,y is coord of bottom left corner
- def draw_rectangle(t_name,width, height,x,y,color):
- t_name.color(color,color)
- t_name.penup()
- t_name.goto(x,y)
- t_name.pendown()
- t_name.begin_fill()
- for _ in range(2):
- t_name.forward(width)
- t_name.right(90)
- t_name.forward(height)
- t_name.right(90)
- t_name.end_fill()
- t_name.penup()
- t_name.goto(x,y)
- print("w="+str(width) + " h="+str(height))
- print("x="+str(x) + " y="+str(y))
- xc = x + (width/2)
- yc = y - (height/2)
- print("xc="+str(xc) + " yc="+str(yc))
- t_name.goto(xc,yc)
- t_name.pendown()
- t_name.color("black")
- t_name.write("Area: " + str(rectangle_area(width,height)), align="center")
- t_name.penup()
- t_name.goto(xc,yc-10)
- t_name.pendown()
- t_name.write("Circumference: " + str(rectangle_circumference(width,height)), align="center")
- t_name.penup()
- t_name.goto(xc,yc-20)
- t_name.pendown()
- t_name.write("Diagonal " + str(round(rectangle_diagonal(width,height),2)), align="center")
- def rectangle_area(width,height):
- return width * height
- def rectangle_circumference(width,height):
- return 2 * (width + height)
- def rectangle_diagonal(width, height):
- return math.sqrt((width*width) + (height*height))
- tina = Turtle()
- draw_circle(tina,60,"lightblue")
- draw_rectangle(tina,200,100,100,100,"lightblue")
- draw_rectangle(tina,200,300,-100,-100,"lightblue")
- #for _ in range(5):
- # draw_rectangle(tina,randint(-400,400),randint(-300,300),randint(10,100),randint(10,80),"lightblue")
- tina.hideturtle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement