Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1_9 drawrectangle.py
- # 2021-03-20 22:46
- # To draw the outline; adapt the circledraw.py a bit.
- # parameters x1, y1, x2, y2, color
- # Simpler if height, width, color. ok this here.
- from turtle import Turtle
- # import math # not required here
- amy =Turtle()
- def rect_area(h,w):
- return h*w
- def rect_perimeter(h,w):
- return (h+w)*2
- def draw_rect(t_name, h, w, col):
- t_name.color(col)
- # t_name.dot(2 * r)
- # t_name.goto(x1,y2)
- # t_name.goto(x2,y2)
- # t_name.goto(x2,y1)
- # t_name.goto(x1,y1)
- t_name.goto(0,h)
- t_name.goto(w,h)
- t_name.goto(w,0)
- t_name.goto(0,0)
- t_name.penup()
- t_name.goto(0,5) #Assumes square 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(rect_area(h,w)), align="right")
- t_name.penup()
- t_name.goto(0,-5)
- t_name.pendown()
- t_name.color("black")
- t_name.write("Perimeter: " + str(rect_perimeter(h,w)), align="right")
- # invoke the above functions
- draw_rect(amy, 100, 200, "blue")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement