from visual import * from visual.graph import * from random import * import Image #Create a 601 by 601 pixels image, black and white) im= Image.new(mode="1", size=(601,601)) x=0.0 y=0.0 count=0 p=1.0/8.0 #Iterates count times the 1/2 distance, 3 corners chaos game while count<1000000: count=count+1 RandomNumber=random() if RandomNumber<=p: x=0.333*x y=0.333*y if (RandomNumber>p and RandomNumber<=2*p): x=0.333*x + 0.333 y=0.333*y if (RandomNumber>2*p and RandomNumber<=3*p): x=0.333*x + 0.666 y=0.333*y if (RandomNumber>3*p and RandomNumber<=4*p): x=0.333*x y=0.333*y + 0.333 if (RandomNumber>4*p and RandomNumber<=5*p): x=0.333*x + 0.666 y=0.333*y + 0.333 if (RandomNumber>5*p and RandomNumber<=6*p): x=0.333*x y=0.333*y + 0.666 if (RandomNumber>6*p and RandomNumber<=7*p): x=0.333*x + 0.333 y=0.333*y + 0.666 if (RandomNumber>7*p and RandomNumber<=8*p): x=0.333*x + 0.666 y=0.333*y + 0.666 #Puts a white pixel at position x,y #Needs to move the top left corner (pixel (0,0)) to the bottom left. im.putpixel((int(600*x),600-int(600*y)),1) im.save("sierpcarpet.jpg") #User should open this jpg with a picture viewer #The im.show() method does not work well in most computers