Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # assignment lesson 1.9
- import turtle
- turtle.setup(500,500)
- board = turtle.Turtle()
- def area_rectangle(width, height):
- return width*height
- def perimeter_rectangle(width, height):
- return (2*width)+(2*height)
- # draws a rectangle given top left position of a rectangle
- def draw_filled_rectangle(board,x,y,width,height,size,color,fill):
- board.fillcolor(fill)
- board.pencolor(color)
- board.pensize(size)
- board.setheading(0)
- board.begin_fill()
- board.up()
- board.goto(x,y)
- board.down()
- # draw top
- board.forward(width)
- # draw right
- board.right(90)
- board.forward(height)
- # draw bottom
- board.right(90)
- board.forward(width)
- # draw left
- board.right(90)
- board.forward(height)
- board.end_fill()
- board.penup()
- board.goto(-80, 0)
- board.pendown()
- board.write("perimeter: " + str(perimeter_rectangle(width, height)) + " area: " + str(area_rectangle(width, height)), align="left")
- #board.write("area: " + str(area_rectangle(width, height)) + "; perimeter: " + str(perimeter_rectangle(width, height)), align="left")
- # The center of the canvas in turtle is 0,0
- # Then the position the rectangle in the center
- # Note: We need to pass the top left co-ordinates of rectangle
- # The rectangle has 200 pixels in width and 100 pixels in height
- # Also specifies the rectangle color and the fill color
- draw_filled_rectangle(board,-100,50,200,100,5,"blue","lightgreen")
- turtle.done()
Advertisement
Add Comment
Please, Sign In to add comment