Interactive Fractals

390 days ago by edinan

# EMILY DINAN # Interactive Fractals 
       
# Fractal 1 is drawn using an iterated function system. The parameter "n" can be changed interactively, producing different pictures. The starting values (x,y) can also be changed. @interact def fractal1(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), n = slider(srange(0.1,0.35,0.01),default = 0.3)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(2*b) temp = (0.6*x) - (n*y) + z y = x + (n*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal (Fractal 1) drawn with a different default parameter value n=0.1 . @interact def fractal1(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), n = slider(srange(0.1,0.35,0.01),default = 0.1)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(2*b) temp = (0.6*x) - (n*y) + z y = x + (n*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal (Fractal 1) drawn with a different default parameter value n=0.25 . @interact def fractal1(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), n = slider(srange(0.1,0.35,0.01),default = 0.25)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(2*b) temp = (0.6*x) - (n*y) + z y = x + (n*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

#################### 
       
# Fractal 2 is drawn using an iterated function system. The parameter value "r" can be changed to create different pictures. The starting values (x,y) can also be changed. @interact def fractal2(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), r = slider(srange(0.1,1,0.1),default = 0.7)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(b+1) temp = (r*x) - (0.3*y) + z y = (0.8*x) + (0.3*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal (Fractal 2) drawn with a different default parameter value r=0.3 . @interact def fractal2(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), r = slider(srange(0.1,1,0.1),default = 0.3)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(b+1) temp = (r*x) - (0.3*y) + z y = (0.8*x) + (0.3*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal (Fractal 2) drawn with a different default parameter value r=0.5 . @interact def fractal2(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), r = slider(srange(0.1,1,0.1),default = 0.5)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(b+1) temp = (r*x) - (0.3*y) + z y = (0.8*x) + (0.3*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

################### 
       
# Fractal 3 is drawn using an iterated function system. The parameter value "r" can be changed to create different pictures. The starting values (x,y) can also be changed. @interact def fractal3(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), r = slider(srange(0.1,1,0.1),default = 0.4)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(b+1) temp = (r*x) - (0.1*y) + z y = (0.2*x) + (0.7*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal (Fractal 3) drawn with a different default parameter value r=0.6 . @interact def fractal3(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), r = slider(srange(0.1,1,0.1),default = 0.6)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(b+1) temp = (r*x) - (0.1*y) + z y = (0.2*x) + (0.7*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal (Fractal 3) drawn with a different default parameter value r=0.1 . @interact def fractal3(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), r = slider(srange(0.1,1,0.1),default = 0.1)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): M+=[(x,y)] b = a.get_random_element() z = floor(b+1) temp = (r*x) - (0.1*y) + z y = (0.2*x) + (0.7*y) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

################### 
       
# Fractal 4 is drawn using an iterated function system. The parameter values "r" and "s" can be changed to create different pictures. The starting values (x,y) can also be changed. @interact def fractal4(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), r = slider(srange(0.1,1,0.05),default = 0.1), s = slider(srange(0.1,1,0.05),default = 0.9)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): if (i>100): M+=[(x,y)] b = a.get_random_element() if (b==0): b = -1 z = floor(b)*0.25 temp = (r*x) - (s*y) + z y = s*(x^2) + r*(y^2) x = temp Mplot = scatter_plot(M, markersize=1, xmin=-0.5, xmax=0.3, ymin=-0.03, ymax=0.22) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal (Fractal 4) drawn with a different default parameter values r=0.85, s=0.1 . @interact def fractal4(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), r = slider(srange(0.1,1,0.05),default = 0.85), s = slider(srange(0.1,1,0.05),default = 0.1)): M = [] P = [0.5, 0.5] a = GeneralDiscreteDistribution(P) for i in range (20000): if (i>100): M+=[(x,y)] b = a.get_random_element() if (b==0): b = -1 z = floor(b)*0.25 temp = (r*x) - (s*y) + z y = s*(x^2) + r*(y^2) x = temp Mplot = scatter_plot(M, markersize=1, xmin=-1.5, xmax=1.5, ymin=-0.03, ymax=0.3) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

################# 
       
# This is a picture of the Barnsley Fern drawn using an iterated function system. This picture is very sensitive to changes, but the number of iterations done can be changed using the slider. @interact def barnsley(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), iterations= slider(srange(200,20000,100),default = 10000)): M = [] P = [0.77,0.12,0.10,0.01] a = GeneralDiscreteDistribution(P) for i in range (iterations): M+=[(x,y)] b = a.get_random_element() if (b==0): temp = (0.85*x) + (0.04*y) + 0.075 y = (-0.04*x) + (0.85*y) + 0.18 x = temp if (b==1): temp = (0.2*x) - (0.26*y) + 0.4 y = (0.23*x) + (0.22*y) + 0.045 x = temp if (b==2): temp = (-0.15*x) + (0.28*y) + 0.575 y = (0.26*x) + (0.24*y) - 0.086 x = temp if (b==3): x = 0.5 y = 0.16*y Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# For example, this is the Barnsley Fern drawn with only 1000 iterations. @interact def barnsley(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), iterations= slider(srange(200,20000,100),default = 1000)): M = [] P = [0.77,0.12,0.10,0.01] a = GeneralDiscreteDistribution(P) for i in range (iterations): M+=[(x,y)] b = a.get_random_element() if (b==0): temp = (0.85*x) + (0.04*y) + 0.075 y = (-0.04*x) + (0.85*y) + 0.18 x = temp if (b==1): temp = (0.2*x) - (0.26*y) + 0.4 y = (0.23*x) + (0.22*y) + 0.045 x = temp if (b==2): temp = (-0.15*x) + (0.28*y) + 0.575 y = (0.26*x) + (0.24*y) - 0.086 x = temp if (b==3): x = 0.5 y = 0.16*y Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

#################### 
       
# This fractal is drawn using an iterated function system. There are two functions to be chosen from for each iteration. The probability "p" that the first function is chosen over the second function can be changed using the slider. The starting values marker size, number of iterations can also be changed. @interact def triangles(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), p= slider(srange(0,1,0.00001),default = (2/3)), MarkerSize= slider(srange(0,3,0.1),default = 1), iterations= slider(srange(200,30000,100),default = 10000)): M = [] q = 1- p P = [p, q] a = GeneralDiscreteDistribution(P) for i in range (iterations): M+=[(x,y)] b = a.get_random_element() z = floor(b) u = a.get_random_element() w = floor(u) temp = (x+z)/2 y = (y+w)/2 x = temp Mplot = scatter_plot(M, markersize=MarkerSize) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal drawn with the default probability that the first function is chosen over the second at p=(1/3) rather than the original (p=2/3). Observe that the picture is opposite. @interact def triangles(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), p= slider(srange(0,1,0.00001),default = (1/3)), MarkerSize= slider(srange(0,3,0.1),default = 1), iterations= slider(srange(200,30000,100),default = 10000)): M = [] q = 1- p P = [p, q] a = GeneralDiscreteDistribution(P) for i in range (iterations): M+=[(x,y)] b = a.get_random_element() z = floor(b) u = a.get_random_element() w = floor(u) temp = (x+z)/2 y = (y+w)/2 x = temp Mplot = scatter_plot(M, markersize=MarkerSize) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is the same fractal drawn with the default probability that the first function is chosen over the second at p=0.5 rather than the original (p=2/3). Observe that the dots are spread uniformly throughout. @interact def triangles(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), p= slider(srange(0,1,0.00001),default = 0.5), MarkerSize= slider(srange(0,3,0.1),default = 1), iterations= slider(srange(200,30000,100),default = 10000)): M = [] q = 1- p P = [p, q] a = GeneralDiscreteDistribution(P) for i in range (iterations): M+=[(x,y)] b = a.get_random_element() z = floor(b) u = a.get_random_element() w = floor(u) temp = (x+z)/2 y = (y+w)/2 x = temp Mplot = scatter_plot(M, markersize=MarkerSize) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

##################### 
       
# This is the Sierpinski Triangle drawn using an iterated function system. The starting values, marker size, and number of iterations can be changed using the sliders. @interact def sierpinski(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), MarkerSize= slider(srange(0,3,0.1),default = 1), iterations= slider(srange(200,40000,100),default = 35000)): M = [] P = [(1/3),(1/3),(1/3)] a = GeneralDiscreteDistribution(P) for i in range (iterations): M+=[(x,y)] b = a.get_random_element() if (b==0): temp = (x/2) y = (y/2) x = temp if (b==1): temp = ((2*x)+1)/4 y = (y+1)/2 x = temp if (b==2): temp = (x+1)/2 y= (y/2) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# For example, this is the Sierpinski Triangle drawn with only 4,000 iterations. @interact def sierpinski(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), MarkerSize= slider(srange(0,3,0.1),default = 1), iterations= slider(srange(200,40000,100),default = 4000)): M = [] P = [(1/3),(1/3),(1/3)] a = GeneralDiscreteDistribution(P) for i in range (iterations): M+=[(x,y)] b = a.get_random_element() if (b==0): temp = (x/2) y = (y/2) x = temp if (b==1): temp = ((2*x)+1)/4 y = (y+1)/2 x = temp if (b==2): temp = (x+1)/2 y= (y/2) x = temp Mplot = scatter_plot(M, markersize=1) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

################ 
       
# This is a snowflake drawn using an iterated function system. The starting values, marker size, and number of iterations can be changed using the sliders. @interact def snowflake(x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), MarkerSize= slider(srange(0,3,0.1),default = 1), iterations= slider(srange(200,40000,100),default = 25000)): M = [] P = [0.25, 0.25, 0.25, 0.25] a = GeneralDiscreteDistribution(P) for i in range (iterations): if (i>100): M+=[(x,y)] b = a.get_random_element() if (b==0): temp = (0.333*x) y = (0.333*y) -0.333 x = temp if (b==1): temp = (0.333*x) y = (0.333*y) + 0.333 x = temp if (b==2): temp = (0.167*x) - (0.289*y) - 0.083 y = (0.289*x) + (0.167*y) + 0.144 x = temp if (b==3): temp = (0.167*x) + (0.289*y) + 0.083 y = -(0.289*x) + (0.167*y) + 0.144 x = temp Mplot = scatter_plot(M, markersize=1,xmin=-0.25, xmax=0.25, ymin=-0.55, ymax=0.55) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# This is is the same snowflake fractal with different viewing options. options = ['Upper Left', 'Upper Right', 'Lower Left', 'Lower Right', 'Normal'] @interact def snowflake(option = selector(options, nrows = 2, width=8), x= slider(srange(1,100,1),default = 1), y= slider(srange(1,100,1),default = 1), MarkerSize= slider(srange(0,3,0.1),default = 1), iterations= slider(srange(200,40000,100),default = 25000)): M = [] P = [0.25, 0.25, 0.25, 0.25] a = GeneralDiscreteDistribution(P) for i in range (iterations): if (i>100): M+=[(x,y)] b = a.get_random_element() if (b==0): temp = (0.333*x) y = (0.333*y) -0.333 x = temp if (b==1): temp = (0.333*x) y = (0.333*y) + 0.333 x = temp if (b==2): temp = (0.167*x) - (0.289*y) - 0.083 y = (0.289*x) + (0.167*y) + 0.144 x = temp if (b==3): temp = (0.167*x) + (0.289*y) + 0.083 y = -(0.289*x) + (0.167*y) + 0.144 x = temp if option == 'Upper Left': Mplot = scatter_plot(M, markersize=1,xmin=-0.25, xmax=0, ymin=0, ymax=0.55) if option == 'Upper Right': Mplot = scatter_plot(M, markersize=1,xmin=0, xmax=0.25, ymin=0, ymax=0.55) if option == 'Lower Left': Mplot = scatter_plot(M, markersize=1,xmin=-0.25, xmax=0, ymin=-0.55, ymax=0) if option == 'Lower Right': Mplot = scatter_plot(M, markersize=1,xmin=0, xmax=0.25, ymin=-0.55, ymax=0) if option == 'Normal': Mplot = scatter_plot(M, markersize=1,xmin=-0.25, xmax=0.25, ymin=-0.55, ymax=0.55) show (Mplot) 
       

Click to the left again to hide and once more to show the dynamic interactive window