Advertisement
TonyMo

1_9 drawrectangle.py

Mar 20th, 2021
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. # 1_9 drawrectangle.py  
  2. # 2021-03-20  22:46
  3. # To draw the outline; adapt the circledraw.py a bit.
  4. # parameters x1, y1, x2, y2, color
  5. # Simpler if height, width, color. ok this here.
  6.  
  7. from turtle import Turtle
  8.  
  9. # import math   # not required here
  10.  
  11. amy =Turtle()
  12.  
  13. def rect_area(h,w):
  14.     return h*w
  15.  
  16. def rect_perimeter(h,w):
  17.     return (h+w)*2
  18.  
  19. def draw_rect(t_name, h, w, col):
  20.     t_name.color(col)
  21.     # t_name.dot(2 * r)
  22.     # t_name.goto(x1,y2)
  23.     # t_name.goto(x2,y2)
  24.     # t_name.goto(x2,y1)
  25.     # t_name.goto(x1,y1)
  26.     t_name.goto(0,h)
  27.     t_name.goto(w,h)
  28.     t_name.goto(w,0)
  29.     t_name.goto(0,0)
  30.  
  31.     t_name.penup()
  32.     t_name.goto(0,5) #Assumes square is at 0,0. How might you adapt if you have x,y parameters?
  33.     t_name.pendown()
  34.     t_name.color("black")
  35.     t_name.write("Area: " + str(rect_area(h,w)), align="right")
  36.  
  37.     t_name.penup()
  38.     t_name.goto(0,-5)
  39.     t_name.pendown()
  40.     t_name.color("black")
  41.     t_name.write("Perimeter: " + str(rect_perimeter(h,w)), align="right")
  42.  
  43.  # invoke the above functions
  44. draw_rect(amy, 100, 200, "blue")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement