In: Computer Science
Python 3:
Create functions to draw each of these with sides of
size side:triangle, square, pentagon, hexagon
The function should be the shape + "2d"
Use import turtle and the turtle commands: forward(), left(),
right(), pendown(), penup(), done(), color()
Use the goto(X,Y) function to allow the shape to be drawn at a
specific location
Use the x,y, and side attributes for screen position and
size
import turtle
#draw triangle
def draw_triangle(x,y,side):
pen.penup()
#set position to given location
pen.setpos([x,y])
pen.pendown()
#forward by the side given
#rotate the turtle pen to right by -120 degrees
#if you want use positive angle then rotate left
#iterate 3 times.
for i in range(3):
pen.forward(side)
pen.right(-120)
pen.penup()
#draw square
def draw_square(x,y,side):
pen.penup()
#set position to given location
pen.setpos([x,y])
pen.pendown()
#forward by the side given
#rotate the turtle pen to right by 90 degrees
#iterate 4 times.
for i in range(4):
pen.forward(side)
pen.right(90)
pen.penup()
def draw_pentagon(x,y,side):
pen.penup()
#set position to given location
pen.setpos([x,y])
pen.pendown()
#forward by the side given
#rotate the turtle pen to left by 72 degrees
#iterate 5 times.
for i in range(5):
pen.forward(side)
pen.left(72)
pen.penup()
def draw_hexagon(x,y,side):
pen.penup()
#set position to given location
pen.setpos([x,y])
pen.pendown()
#forward by the side given
#rotate the turtle pen to right by 60 degrees
#iterate 6 times.
for i in range(6):
pen.forward(side)
pen.right(60)
pen.penup()
pen = turtle.Turtle()
#take input and call the drawing methods
x,y = map(int,input("Enter x and y positions of Triangle separated
by space: ").strip().split(" "))
side = int(input("Enter side of Triangle(>50 for bigger shape):
").strip())
draw_triangle(x,y,side)
print()
x,y = map(int,input("Enter x and y positions of Square separated
by space: ").strip().split(" "))
side = int(input("Enter side of Square(>50 for bigger shape):
").strip())
draw_square(x,y,side)
print()
x,y = map(int,input("Enter x and y positions of Pentagon separated
by space: ").strip().split(" "))
side = int(input("Enter side of Pentagon(>50 for bigger shape):
").strip())
draw_pentagon(x,y,side)
print()
x,y = map(int,input("Enter x and y positions of Hexagon separated
by space: ").strip().split(" "))
side = int(input("Enter side of Hexagon(>50 for bigger shape):
").strip())
draw_hexagon(x,y,side)
turtle.done()