# File:stoffractal.py # Stoffractal, Monte Carlo methode # Source: Lauwerier - Computer Simulatie from graphics import * # custom graphics import math # math module import random # random numbers #### Open a graphics window similar to SCREEN 3 in BASIC #### 720 * 348 pixels, 2 colors win = GraphWin("Stoffractal", 720, 348) ### Redefine the coordinate system win.setCoords(-1.1, -1.2, 2.1, 1.2) #### define some constants r1 = 0.6 a=r1*math.cos(2*math.pi/3) b=r1*math.sin(2*math.pi/3) r2 = 0.6 c=r2*math.cos(2*math.pi/3) d=-r2*math.sin(2*math.pi/3) ### define functions def rotation1(): global x,y,a,b z=x x=a*x-b*y y=b*z+a*y return x,y def rotation2(): global x,y,a,c,d z=x x=c*x-d*y+1-c y=d*z+a*y-d return x,y def spiegeling1(): global x,y,a,b z=x x=a*x+b*y y=b*z-a*y return x,y def spiegeling2(): global x,y,a,c,d z=x x=c*x+d*y+1-c y=d*z-a*y-d return x,y ### starting point x=a y=b ### picture building k = 1 for k in range(10000): j = random.random() if j < 0.5: # rotation1() spiegeling1() else: rotation2() # spiegeling2() #### Draw a point object win.plot(x,y) k = k + 1 #### Wait for a signal before exiting message = Text(Point (1,-1), "Click anywhere to quit") message.draw(win) win.getMouse()