Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Blog
Views: 88
Author: Vincent J. Matsko Date: 18 October 2015, Day009 Post: Creating Fractals III: Making Your Own
# Recursive function to output the highest factor of 2 within a positive integer. # If n is odd, we're done and return 0. # Otherwise, add 1 to the highest power of 2 within n/2. def highestpowerof2 (n): if n % 2 == 1: return 0 else: return 1 + highestpowerof2(n / 2)
highestpowerof2(1000000)
6
# These are necessary so the appropriate Python commands are available. #from graphics import * import matplotlib # angle1 and angle2 are the angles to turn. IMPORTANT: Make sure angle2 is negative! # n is the number of steps to draw. # len is the length of the steps. def drawfractal(angle1, angle2, n, len): # Color choices. See the blog post for details. colors = ["darkorchid", "firebrick", "blue", "forestgreen"] # currentangle keeps track of the orientation in the plane. currentangle = 0 # linsegments stores the lines we'll need to draw. linesegments = [line([vector((0,0)),vector((len,0))])] # currentpoint keeps track of the end of the path. currentpoint = vector((len,0)) for i in range(n): # First, find the appropriate angle to turn. if highestpowerof2(i + 1) % 2 == 0: currentangle += angle1 else: currentangle += angle2 # Converting to rectangular coordinates. nextpt = currentpoint + vector((len*cos(currentangle*pi/180),len*sin(currentangle*pi/180))) # Now adding the segment to the end of the path. linesegments.append(line([currentpoint,nextpt], color=colors[i % 4])) # Resetting currentpoint to the end of the path. currentpoint = nextpt show(sum(linesegments), aspect_ratio = 1, axes = False)
drawfractal(80, -140, 832, 0.2)
drawfractal(90, -210, 128, 0.2)
drawfractal(90, -250, 512, 0.2)
# angle1 and angle2 are the angles to check. # m is the maximum number of iterations to try. # This algorithm was created by Matthieu Pluntz, October 2015. # See the blog post for more details. def checkangles(angle1, angle2, m): sum = angle1 - angle2 currentangle = angle1 done = False count = 1 while (count <= m) and (not(done)): currentangle = (sum - 2 * currentangle) % 360 count += 1 if (currentangle == 180): print "Found it!", angle1, angle2, "Level = ", count done = True
checkangles(90, -210, 100)
Found it! 90 -210 Level = 4
checkangles(90, -250, 100)
Found it! 90 -250 Level = 10
checkangles(11, -169, 100)
for angle in range(-359,0): checkangles(90, angle, 100)
Found it! 90 -354 Level = 13 Found it! 90 -346 Level = 37 Found it! 90 -338 Level = 37 Found it! 90 -330 Level = 4 Found it! 90 -322 Level = 37 Found it! 90 -314 Level = 37 Found it! 90 -306 Level = 5 Found it! 90 -298 Level = 37 Found it! 90 -290 Level = 10 Found it! 90 -282 Level = 13 Found it! 90 -274 Level = 37 Found it! 90 -270 Level = 2 Found it! 90 -266 Level = 37 Found it! 90 -258 Level = 13 Found it! 90 -250 Level = 10 Found it! 90 -242 Level = 37 Found it! 90 -234 Level = 5 Found it! 90 -226 Level = 37 Found it! 90 -218 Level = 37 Found it! 90 -210 Level = 4 Found it! 90 -202 Level = 37 Found it! 90 -194 Level = 37 Found it! 90 -186 Level = 13 Found it! 90 -178 Level = 37 Found it! 90 -170 Level = 10 Found it! 90 -162 Level = 5 Found it! 90 -154 Level = 37 Found it! 90 -146 Level = 37 Found it! 90 -138 Level = 13 Found it! 90 -130 Level = 10 Found it! 90 -122 Level = 37 Found it! 90 -114 Level = 13 Found it! 90 -106 Level = 37 Found it! 90 -98 Level = 37 Found it! 90 -90 Level = 3 Found it! 90 -82 Level = 37 Found it! 90 -74 Level = 37 Found it! 90 -66 Level = 13 Found it! 90 -58 Level = 37 Found it! 90 -50 Level = 10 Found it! 90 -42 Level = 13 Found it! 90 -34 Level = 37 Found it! 90 -26 Level = 37 Found it! 90 -18 Level = 5 Found it! 90 -10 Level = 10 Found it! 90 -2 Level = 37
drawfractal(90, -306, 512, 0.2)