Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This project is built up on the rule of Logarithmic spiral:
- where we have polar coordinate as
- r = a*e^(b*theta) (1)
- It is then transformed to Cartesian coordinate system by:
- x = r*cos(theta) and y = r*sin(theta) (2)
- And combining (1) and (2):
- x = (a*e^(b*theta))*cos(theta)
- y = (a*e^(b*theta))*sin(theta) (3)
- We will use some recursion to derive the natural beauty of Fibonacci number
- and may be some others.
- Here is how the program works:
- First we set up size, color, background, and some formula to build the
- initial point where we start, in function setup().
- Remember our goal, draw 6 Logarithmic spirals. We do it by drawing only one
- of them first.
- We can do it by recursion! As required by the course! But, the recursion in
- python, and in any other widely used language, there is a limit of recursion
- depth. The correct number of python mode of this code is no more than 999.
- However, I need more than 6k points to build the graph, how?
- The first trick I did is to divide the single sprial into separate parts. First, we draw 600 points,
- (to be precise, floor(2*PI*1000)), a tiny small part of spiral line. Then, we pass the end point,
- theta, a, b to recursion.
- That is exactly what supersetup and draw_logarithmic_spiral is doing.
- supersetup is the main recursion part: it recursions it self and call draw function
- draw function also recursion itself
- Well, too many recursions indeed!
- All recursions will be marked as >>>>>>***MARK RECURSION***<<<<<<
- And, a list of dictionaries (I know it's crazy) is to be established, to recored those
- points whose theta is happened to be near any times of PI/6.
- So far, we only built a single spiral.
- Then, doing rotation, will perfect solve the rest?
- NO.
- In fact, I need fpoint[0], a dictionary, to record each point we need in the first spiral
- and multiply by a rotation matrix, calculated by hand(computer)
- It's all in the funcion called create_other_path
- I make it recursion since, you know why.
- Next, all points are connected with some rule and make transparancy easy, charm with some colors.
- Done.
- All in all, I am not got that impressed by this idea so,
- may be I will do another project if I got any idea.
- """
- color_hash_table = {'red':[255,0,0,255], 'orange':[255,165,0,255],
- 'gold':[255,215,0,255],'green':[124,252,0,255],
- 'dark green':[0,128,0,355],'sky':[0,191,255,255],
- 'blue':[0,0,255,255],'violet':[138,43,226,255]}
- color_index_table = {0:'red',1:'orange',2:'gold',3:'green',
- 4:'dark green', 5:'sky', 6:'blue',7:'violet'}
- fpoint=[{},{},{},{},{},{}]
- def setup():
- size(1000,1000)
- strokeWeight(1.5)
- colorMode(RGB,255,255,255,255)
- background(0)
- smooth(10)
- stroke(255,255,255,255)
- """PLAY AROUND THE FOLLOWING VARIABLE IT's FUN!"""
- a = 5 # This is how big you can see the whole picture
- pitch = 70 # It affects how circle the graph will be like
- """
- TRY a = 10 pitch = 95 or pitch = 40, what will you see? Why? :)
- """
- b = radians(90-pitch)
- theta = 0
- r = a * exp(b*theta)
- start_x = r * cos(theta) + 500
- start_y = r * sin(theta) + 500
- # ***MARK A*** set the end point for first part of the graph
- repeat(start_x,start_y,a,b,theta,0)
- create_other_path(1)
- strokeWeight(1.0)
- connect_points()
- save("result_"+str(random(20000))+".jpg")
- def create_other_path(t):
- for k in range(len(fpoint[t-1])):
- f_x = fpoint[t-1][k][0] - 500
- f_y = fpoint[t-1][k][1] - 500
- 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]
- if t<5:
- create_other_path(t+1)
- """>>>>>>***MARK RECURSION***<<<<<<"""
- def connect_points():
- print (len(fpoint[1])-5)
- for i in range(6):
- if i == 5:
- for k in range(0,len(fpoint[1])-5,1):
- stroke(122,200,200,255-9*k)
- line(fpoint[i][k+5][0],fpoint[i][k+5][1],fpoint[0][k][0],fpoint[0][k][1])
- else:
- for k in range(0,len(fpoint[1])-5,1):
- stroke(122,200,200,255-9*k)
- line(fpoint[i][k+5][0],fpoint[i][k+5][1],fpoint[i+1][k][0],fpoint[i+1][k][1])
- def repeat(start_x,start_y,a,b,theta,counter):
- translate(500,500)
- rotate(radians(60))
- translate(-500,-500)
- c = color_hash_table[color_index_table[counter]]
- supersetup(start_x,start_y,a,b,theta,0,c)
- if counter < 7:
- repeat(start_x,start_y,a,b,theta,counter+1)
- """>>>>>>***MARK RECURSION***<<<<<<"""
- def supersetup(start_x,start_y,a,b,theta,counter,c):
- """
- In first degree recursion we set up a small distance of spiral to be drawn
- The previous drawn point is passed to this function as parameter
- We then do nothing but transfer these parameter to the draw function which really does the job
- However, we know the draw function will return the final position of the part it drawn
- We utilize this, set the return value as array A
- A[0] A[1] is the final position of last part of spiral
- A[2] is the last ran theta. and we will go increasing it
- """
- A = draw_logarithmic_spiral(start_x,start_y,a,b,theta+0.01,theta+2*PI,c)
- #***MARK B***
- if counter < 15:
- # counter defines whole number of parts we are going to draw
- supersetup(A[0],A[1],a,b,A[2],counter+1,c)
- """>>>>>>***MARK RECURSION***<<<<<<"""
- def draw_logarithmic_spiral(start_x,start_y,a,b,theta,limit_theta,c):
- """
- Second degree recursion
- It is the true part of the draw function
- First, we have to know the end point of previous part of spiral
- The initial value is set in the ***MARK A*** above, and the recursion initial value is recorded and used in ***MARK B***
- """
- u = theta % (PI/6)
- u_mod = theta // (PI/6)
- if u < 0.05:
- ellipse(start_x,start_y,3,3)
- if u_mod not in fpoint:
- fpoint[0][int(u_mod)]=[start_x,start_y]
- r = a * exp(b*theta)
- fill(255,255,255,255)
- stroke(c[0],c[1],c[2],255-r//2.4)
- x = r * cos(theta) + 500
- y = r * sin(theta) + 500
- # x and y is calculated each time by the given e and a and b.
- line(start_x,start_y,x,y)
- # we connect each point by line
- # A better way could be doing linear regression which will be super complicated in this case (but easy in matlab)
- if theta <= limit_theta:
- return draw_logarithmic_spiral(x,y,a,b,theta+0.05,limit_theta,c)
- """>>>>>>***MARK RECURSION***<<<<<<"""
- #0.05 is the step we are going to take, smaller number, smoother the line
- else:
- return (x,y,theta)
Add Comment
Please, Sign In to add comment