Home > Art, Projects/Experiments, Python > Python: Drawing Shapes

Python: Drawing Shapes



Learning : Drawing Shapes
Subject: New shapes function

To Draw a Square shape, we need to know the width ( W ) of the square side, and then we draw a line and moving in 90 degree and drawing another line and so on until we finished the 4 side of the square. In the same principle if we want to draw a triangle (equilateral one), we need to know length of its sides and in mathematics we know that in equilateral triangles the angles (corners) are 120 degree, so we draw a line and move in 120 degree and drawing another two sides.

In coming code, we will write a general function in Python to pass the number on sides we want to draw (triangle =3, Square=4,Pentagon = 5, Hexagon =6 .. and so on), the width (size) of the shape and the position (x,y) of the first angle or point.

The Codes:

def d_shape(s_heads,w,x1,y1):

t.goto(x1,y1)

# To get t.right angle

rang = 360 / s_heads

t.pendown()

for x in range (s_heads +1) :

t.forward(w)

t.right(-rang)

t.penup()



Results after using the new function we can pass any number of sides and the function will draw the shape, here are a sample execution of it. .. .. Click to enlarge ..




Now if we call the function number of times equal to it’s heads what we will get ? let’s see . .. Click to enlarge ..


And take a look when we set the numbers to 20. .. Click to enlarge ..



To Download my Python code (.py) files Click-Here




Follow me on Twitter..





  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: