Home > Art, Problem, Projects/Experiments, Python > Python: Random Squares

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 ..



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 )

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: