LyWang

test

Dec 10th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.75 KB | None | 0 0
  1. """
  2.  
  3. This project is built up on the rule of Logarithmic spiral:
  4. where we have polar coordinate as
  5.  
  6.    r = a*e^(b*theta)                       (1)
  7.    
  8. It is then transformed to Cartesian coordinate system by:
  9.    
  10.    x = r*cos(theta) and y = r*sin(theta)   (2)
  11.    
  12. And combining (1) and (2):
  13.    
  14.    x = (a*e^(b*theta))*cos(theta)
  15.    y = (a*e^(b*theta))*sin(theta)          (3)
  16.    
  17. We will use some recursion to derive the natural beauty of Fibonacci number
  18. and may be some others.
  19.  
  20. Here is how the program works:
  21.  
  22. First we set up size, color, background, and some formula to build the
  23. initial point where we start, in function setup().
  24.  
  25. Remember our goal, draw 6 Logarithmic spirals. We do it by drawing only one
  26. of them first.
  27.  
  28. We can do it by recursion! As required by the course! But, the recursion in
  29. python, and in any other widely used language, there is a limit of recursion
  30. depth. The correct number of python mode of this code is no more than 999.
  31.  
  32. However, I need more than 6k points to build the graph, how?
  33.  
  34. The first trick I did is to divide the single sprial into separate parts. First, we draw 600 points,
  35. (to be precise, floor(2*PI*1000)), a tiny small part of spiral line. Then, we pass the end point,
  36. theta, a, b to recursion.
  37.  
  38. That is exactly what supersetup and draw_logarithmic_spiral is doing.
  39.  
  40. supersetup is the main recursion part: it recursions it self and call draw function
  41. draw function also recursion itself
  42.  
  43. Well, too many recursions indeed!
  44. All recursions will be marked as >>>>>>***MARK RECURSION***<<<<<<
  45.  
  46. And, a list of dictionaries (I know it's crazy) is to be established, to recored those
  47. points whose theta is happened to be near any times of PI/6.
  48.  
  49. So far, we only built a single spiral.
  50.  
  51. Then, doing rotation, will perfect solve the rest?
  52.  
  53. NO.
  54.  
  55. In fact, I need fpoint[0], a dictionary, to record each point we need in the first spiral
  56. and multiply by a rotation matrix, calculated by hand(computer)
  57.  
  58. It's all in the funcion called create_other_path
  59.  
  60. I make it recursion since, you know why.
  61.  
  62. Next, all points are connected with some rule and make transparancy easy, charm with some colors.
  63.  
  64. Done.
  65.  
  66. All in all, I am not got that impressed by this idea so,
  67. may be I will do another project if I got any idea.
  68.  
  69. """
  70. color_hash_table = {'red':[255,0,0,255], 'orange':[255,165,0,255],
  71.                    'gold':[255,215,0,255],'green':[124,252,0,255],
  72.                    'dark green':[0,128,0,355],'sky':[0,191,255,255],
  73.                    'blue':[0,0,255,255],'violet':[138,43,226,255]}
  74.  
  75. color_index_table = {0:'red',1:'orange',2:'gold',3:'green',
  76.                      4:'dark green', 5:'sky', 6:'blue',7:'violet'}
  77.  
  78. fpoint=[{},{},{},{},{},{}]
  79.  
  80.  
  81. def setup():
  82.     size(1000,1000)
  83.     strokeWeight(1.5)
  84.     colorMode(RGB,255,255,255,255)
  85.     background(0)
  86.     smooth(10)
  87.     stroke(255,255,255,255)
  88.    
  89.     """PLAY AROUND THE FOLLOWING VARIABLE IT's FUN!"""
  90.     a = 5     # This is how big you can see the whole picture
  91.     pitch = 70 # It affects how circle the graph will be like
  92.     """
  93.    TRY a = 10 pitch = 95 or pitch = 40, what will you see? Why? :)
  94.    """
  95.     b = radians(90-pitch)
  96.     theta = 0
  97.     r = a * exp(b*theta)
  98.     start_x = r * cos(theta) + 500
  99.     start_y = r * sin(theta) + 500
  100.     # ***MARK A*** set the end point for first part of the graph
  101.     repeat(start_x,start_y,a,b,theta,0)
  102.     create_other_path(1)
  103.     strokeWeight(1.0)
  104.     connect_points()
  105.     save("result_"+str(random(20000))+".jpg")
  106.  
  107. def create_other_path(t):
  108.     for k in range(len(fpoint[t-1])):
  109.         f_x = fpoint[t-1][k][0] - 500
  110.         f_y = fpoint[t-1][k][1] - 500
  111.         fpoint[t][k]=[f_x*cos(radians(60))-f_y*sin(radians(60))+500, f_x*sin(radians(60))+f_y*cos(radians(60))+500]
  112.     if t<5:
  113.         create_other_path(t+1)
  114.         """>>>>>>***MARK RECURSION***<<<<<<"""
  115.            
  116. def connect_points():
  117.     print (len(fpoint[1])-5)
  118.     for i in range(6):
  119.         if i == 5:
  120.             for k in range(0,len(fpoint[1])-5,1):
  121.                 stroke(122,200,200,255-9*k)
  122.                 line(fpoint[i][k+5][0],fpoint[i][k+5][1],fpoint[0][k][0],fpoint[0][k][1])
  123.         else:
  124.             for k in range(0,len(fpoint[1])-5,1):
  125.                 stroke(122,200,200,255-9*k)
  126.                 line(fpoint[i][k+5][0],fpoint[i][k+5][1],fpoint[i+1][k][0],fpoint[i+1][k][1])
  127.    
  128. def repeat(start_x,start_y,a,b,theta,counter):
  129.     translate(500,500)
  130.     rotate(radians(60))
  131.     translate(-500,-500)
  132.     c = color_hash_table[color_index_table[counter]]
  133.     supersetup(start_x,start_y,a,b,theta,0,c)
  134.     if counter < 7:
  135.         repeat(start_x,start_y,a,b,theta,counter+1)
  136.         """>>>>>>***MARK RECURSION***<<<<<<"""
  137.    
  138. def supersetup(start_x,start_y,a,b,theta,counter,c):
  139.     """
  140.    In first degree recursion we set up a small distance of spiral to be drawn
  141.    The previous drawn point is passed to this function as parameter
  142.    We then do nothing but transfer these parameter to the draw function which really does the job
  143.    However, we know the draw function will return the final position of the part it drawn
  144.    We utilize this, set the return value as array A
  145.    A[0] A[1] is the final position of last part of spiral
  146.    A[2] is the last ran theta. and we will go increasing it
  147.    """
  148.     A = draw_logarithmic_spiral(start_x,start_y,a,b,theta+0.01,theta+2*PI,c)
  149.     #***MARK B***
  150.     if counter < 15:
  151.     # counter defines whole number of parts we are going to draw
  152.         supersetup(A[0],A[1],a,b,A[2],counter+1,c)
  153.         """>>>>>>***MARK RECURSION***<<<<<<"""
  154.    
  155. def draw_logarithmic_spiral(start_x,start_y,a,b,theta,limit_theta,c):
  156.     """
  157.    Second degree recursion
  158.    It is the true part of the draw function
  159.    First, we have to know the end point of previous part of spiral
  160.    The initial value is set in the ***MARK A*** above, and the recursion initial value is recorded and used in ***MARK B***
  161.    """
  162.     u = theta % (PI/6)
  163.     u_mod = theta // (PI/6)
  164.     if u < 0.05:
  165.         ellipse(start_x,start_y,3,3)
  166.         if u_mod not in fpoint:
  167.             fpoint[0][int(u_mod)]=[start_x,start_y]
  168.     r = a * exp(b*theta)
  169.     fill(255,255,255,255)
  170.     stroke(c[0],c[1],c[2],255-r//2.4)
  171.     x = r * cos(theta) + 500
  172.     y = r * sin(theta) + 500
  173.     # x and y is calculated each time by the given e and a and b.
  174.     line(start_x,start_y,x,y)
  175.     # we connect each point by line
  176.     # A better way could be doing linear regression which will be super complicated in this case (but easy in matlab)
  177.     if theta <= limit_theta:
  178.         return draw_logarithmic_spiral(x,y,a,b,theta+0.05,limit_theta,c)
  179.         """>>>>>>***MARK RECURSION***<<<<<<"""
  180.     #0.05 is the step we are going to take, smaller number, smoother the line
  181.     else:
  182.         return (x,y,theta)
Add Comment
Please, Sign In to add comment