Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Blog
Views: 68
Author: Vincent J. Matsko Date: 17 April 2016, Day036 Post: Fractals VII: Iterated Function Systems, III
# This is the basic algorithm. # You can adapt it many ways. Feel free to experiment! # n is the number of points to plot def sierpinski(n): # These variables keep track of the differently colored points. # For more transformations, add more variables. ptscolor1 = [] ptscolor2 = [] ptscolor3 = [] # Begin at (0,0). You may begin anywhere you like. last = (0,0) for i in range(n): select = random() (x,y) = last if 0 <= select < 0.333: # 1/3 chance for 1st transformation. last = (0.5*x,0.5*y) # Note how the affine transformation is represented. ptscolor1.append(last) elif 0.333 <= select < 0.667: # 1/3 chance for 2nd transformation. last = (0.5*x + 0.5,0.5*y) ptscolor2.append(last) else: # remaining 1/3 chance for last transformation. last = (0.5*x,0.5*y + 0.5) ptscolor3.append(last) # You may use any colors you wish! # The "aspect_ratio=1" keeps the axes at the same scale. show(points(ptscolor1, color='red') + points(ptscolor2, color='blue') + points(ptscolor3, color='orange'), aspect_ratio=1)
sierpinski(1000)
# The same algorithm works even if you do not use affine transformations. # The possibilites are virtually endless! # n is the number of points to plot def nonaffine(n): # These variables keep track of the differnetly colored points. # For more transformations, add more variables. ptscolor1 = [] ptscolor2 = [] ptscolor3 = [] # Begin at (0,0). You may begin anywhere you like. last = (0,0) for i in range(n): select = random() (x,y) = last if 0 <= select < 0.5: # 1/3 chance for 1st transformation. last = (x**2,0.75*y) # No longer affine! ptscolor1.append(last) elif 0.5 <= select < 0.8: # 1/3 chance for 2nd transformation. last = (-0.5*x + 0.5,-y**2) # No longer affine! ptscolor2.append(last) else: # remaining 1/3 chance for last transformation. last = (0.5*x,-0.75*(y**2) + 0.5) # No longer affine! ptscolor3.append(last) # You may use any colors you wish! # The "size=3" made the red points smaller. You may adjust this parameter. # The "aspect_ratio=1" keeps the axes at the same scale. show(points(ptscolor1, size=3, color='red') + points(ptscolor2, color='blue') + points(ptscolor3, color='orange'), aspect_ratio=1)
nonaffine(3000)
# For the adventurous, you can create 3D fractals as well! # The algorithm is the same, except the affine transformations are three-dimensional. def sierpinski3d(n): ptsclr1 = [] ptsclr2 = [] ptsclr3 = [] ptsclr4 = [] last = (0,0,0) for i in range(n): select = random() (x,y,z) = last if 0 <= select < 0.25: last = (0.5*x,0.5*y,0.5*z) ptsclr1.append(last) elif 0.25 <= select < 0.5: last = (0.5*x + 0.5,0.5*y,0.5*z) ptsclr2.append(last) elif 0.5 <= select < 0.75: last = (0.5*x,0.5*y + 0.5,0.5*z) ptsclr3.append(last) else: last = (0.5*x,0.5*y,0.5*z + 0.5) ptsclr4.append(last) P=point3d(ptsclr1, size=2, color='red') + point3d(ptsclr2, size=2, color='blue') + point3d(ptsclr3, size=2, color='orange') + point3d(ptsclr4, size=2, color='purple',viewpoint=(1,1,1)) show(P)
sierpinski3d(3000)
3D rendering not yet implemented