You can draw all possible shapes with Python Turtle. Turtle is a Python module to draw.
To do so, take 360 degrees and divide it by the number of sides. That how many turns the turtle will make.
With 1 side, it will draw a line. With 3 sides, it will draw a triangle. With 4 sides a square and so on.
In code that looks like this:
#!/usr/bin/python3
def draw_shape(sides):
t.pensize(3)
t.pencolor("black")
for i in range(sides):
t.right(360/sides)
t.fd(200/sides)
So the shape is 360/sides. What is 200/sides? That's the size of the shape. Naturally the shape gets bigger with each increment in sides. So shrink it based on the sides. The more sides, the less steps the turtle takes.
Now in order to not draw everything on top of each other, the turtle needs to move on the space. We simply use x,y movement to put every space in a unique position.
#!/usr/bin/python3
import turtle as t
t.setup(800,600,200,200)
def draw_shape(sides):
t.pensize(3)
t.pencolor("black")
for i in range(sides):
t.right(360/sides)
t.fd(200/sides)
x = -400
y = 200
for i in range(0,100):
t.up()
t.goto(x,y)
t.down()
draw_shape(i)
x = x + 80
print(x)
if x > 400:
x = x - 800
y = y - 100
t.done()
Whew, that was fun :) You see how all these shapes are related. Only the number of sides changes a line into a triangle, square, circle. Interesting isn't it?
Related links:
Top comments (0)