Python: Random Squares
Random Squares Art
Subject: Python, Graphics and simulation
In This project say we have a Square (10 x 10 cm) the square has four corners labeled as (a, b, c, d) as in figure i.
![]() |
then we select a random corner (c or d) [assume it is c] then we select an angle (ang) of rotation between (10, 45), and we draw another square positioning (a) on the (c) and rotating it with (ang) anticlockwise as figure ii.
![]() |
Now if we repeat this two steps .. S1. Selecting a random corner (c or d). S2. Selecting a random rotation angle between (10, 45). and draw the square. let’s see what we may have as a random art generator.
![]() |
![]() |
![]() |
Python Code for Random Squares Art
Codes to select corner (c or d)
def select_c_or_d():
if random.randrange(0,2) == 0 :
x = cdpos[0][0]
y = cdpos[0][1]
else:
x = cdpos[1][0]
y = cdpos[1][1]
t.setheading(0)
t.right(random.randrange(rotmin,rotmax)*f)
Codes to draw the Square (c or d)
def d_square(w,x1,y1):
t.goto(x1,y1)
t.pendown()
t.forward(w)
t.right(-90)
t.forward(w)
# save corner c position
cdpos.append([t.xcor(),t.ycor()])
t.right(-90)
t.forward(w)
# save corner d position
cdpos.append([t.xcor(),t.ycor()])
t.right(-90)
t.forward(w)
t.penup()
I notes that if we increase the number of Squares, we start to have some interesting results.
.. Have Fun ..
To Download my Python code (.py) files Click-Here