Home > Art, Learning, Lesson, Problem, Projects/Experiments, Python > Python: Spirograph

Python: Spirograph


Learning : Python
Subject: Writing Python codes to generate Spirograph Art

Writing codes to draw always funny time for me, playing around numbers, changing attributes to get something new and changing it again .. and again ..
In this post, we will write a code to draw some spirograph shapes, easy and shrort code will do the job. So lets Start ..

We will use Python Library turtle to draw, and will write one Function call it def draw_it(h,sz,ang) will take three arguments: h:number of heads, sz: size, ang: angle, then we will call this function to draw our Spirograph Art.
Code:
First we will set-up the turtle:

# turtle set-up
import turtle
t = turtle.Turtle()
t.shape("dot")
t.speed(0)

t.hideturtle()

Then here is the main Function to draw our graphs

# draw_it() function 

def draw_it(h,sz,ang) : 
  c = 0
  while True : 
    for i in range (h) : 
      t.forward(sz)
      t.right(360/h)
      
    t.right(ang) 
    c +=1
    if c >=360/ang :
      break

Then we call the function and pass the parameters, I tried several combinations and will include them in the source file in Download section. Here are some out-puts.

Calling:
t.pencolor(‘lightgray’)
draw_it(19,19,19)
t.pencolor(‘gray’)
draw_it(17,17,17)
t.pencolor(‘black’)
draw_it(15,15,15)

Hope you enjoy, have fun and change the numbers to get new shapes ..

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

Follow me on Twitter..

By: Ali Radwani

  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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: