Advertisement
pamalau

Untitled

Feb 7th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. from turtle import Turtle, Screen
  2. from random import randint
  3. import math #Explore specific import ?
  4.  
  5. def circle_area(r):
  6.     return 3.14 * r * r
  7.  
  8. def circle_circumference(r):
  9.     return 3.14 * 2 * r    
  10.  
  11. def draw_circle(t_name, r, col):
  12.     t_name.color(col)
  13.     t_name.dot(2 * r)
  14.     t_name.penup()
  15.     t_name.goto(0,5) #Assumes circle is at 0,0. How might you adapt if you have x,y parameters?
  16.     t_name.pendown()
  17.     t_name.color("black")
  18.     t_name.write("Area: " + str(circle_area(r)), align="center")
  19.     t_name.penup()
  20.     t_name.goto(0,-5)
  21.     t_name.pendown()
  22.     t_name.color("black")
  23.     t_name.write("Circumference: " + str(circle_circumference(r)), align="center")
  24.  
  25. #rectangle x,y is coord of bottom left corner    
  26. def draw_rectangle(t_name,width, height,x,y,color):
  27.     t_name.color(color,color)
  28.     t_name.penup()
  29.     t_name.goto(x,y)
  30.     t_name.pendown()
  31.     t_name.begin_fill()
  32.     for _ in range(2):
  33.         t_name.forward(width)
  34.         t_name.right(90)
  35.         t_name.forward(height)
  36.         t_name.right(90)
  37.     t_name.end_fill()
  38.     t_name.penup()
  39.     t_name.goto(x,y)
  40.     print("w="+str(width) + " h="+str(height))
  41.     print("x="+str(x) + " y="+str(y))
  42.     xc = x + (width/2)
  43.     yc = y - (height/2)
  44.     print("xc="+str(xc) + " yc="+str(yc))    
  45.     t_name.goto(xc,yc)
  46.     t_name.pendown()
  47.     t_name.color("black")
  48.     t_name.write("Area: " + str(rectangle_area(width,height)), align="center")
  49.     t_name.penup()
  50.     t_name.goto(xc,yc-10)
  51.     t_name.pendown()    
  52.     t_name.write("Circumference: " + str(rectangle_circumference(width,height)), align="center")
  53.     t_name.penup()
  54.     t_name.goto(xc,yc-20)
  55.     t_name.pendown()    
  56.     t_name.write("Diagonal " + str(round(rectangle_diagonal(width,height),2)), align="center")    
  57. def rectangle_area(width,height):
  58.     return width * height
  59.  
  60. def rectangle_circumference(width,height):
  61.     return 2 * (width + height)
  62.  
  63. def rectangle_diagonal(width, height):
  64.     return math.sqrt((width*width) + (height*height))
  65.  
  66. tina = Turtle()
  67. draw_circle(tina,60,"lightblue")
  68. draw_rectangle(tina,200,100,100,100,"lightblue")
  69. draw_rectangle(tina,200,300,-100,-100,"lightblue")
  70. #for _ in range(5):
  71. #    draw_rectangle(tina,randint(-400,400),randint(-300,300),randint(10,100),randint(10,80),"lightblue")
  72. tina.hideturtle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement