Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. import math, random
  2. import matplotlib.pyplot as plt
  3. import matplotlib.cm as cm
  4. plt.style.use('dark_background')
  5.  
  6. def create_point():
  7. x = random.random()
  8. y = random.random()
  9.  
  10. return x,y
  11.  
  12. def print_points(points):
  13. print("pts:")
  14. for point in points:
  15. print(" ",point)
  16. print("end")
  17.  
  18. def plot_points(points):
  19. for pt in points: plt.scatter(pt[0],pt[1])
  20. plt.axis([0, 1, 0, 1])
  21. plt.show()
  22.  
  23. pts_x = []
  24. pts_y = []
  25. points = 7
  26.  
  27. for i in range(points):
  28. pt_x,pt_y = create_point()
  29. pts_x.append(pt_x)
  30. pts_y.append(pt_y)
  31.  
  32. #print and show points
  33. #plot_points(points)
  34.  
  35.  
  36. # import packages
  37. import numpy as np
  38. import matplotlib.pyplot as plt
  39. from matplotlib import animation
  40.  
  41. class windmill:
  42. """Gaussian Initial Condition, Fixed Boundary Condition"""
  43. def __init__(self,x0,y0,x_last,y_last,pts_x,pts_y,dt=0.01,length=1.5):
  44. self.x0=x0
  45. self.y0=y0
  46. self.x_last = x_last
  47. self.y_last = y_last
  48. self.theta0 = math.atan2(y_last-y0,x_last-x0)
  49. self.theta = self.theta0
  50. self.dt=dt
  51. self.length = length
  52. self.i=10000
  53.  
  54. self.pts_x=pts_x
  55. self.pts_y=pts_y
  56.  
  57. #find next point
  58. max_theta = 999
  59. for i in range(len(pts_x)):
  60. _x = pts_x[i]
  61. _y = pts_y[i]
  62. if ((_x is not self.x0) and (_y is not self.y0)) and ((_y is not self.y_last) and (_x is not self.x_last)):
  63. v1 = np.array([x_last-self.x0,y_last-self.y0])
  64. v2 = np.array([_x - self.x0, _y - self.y0])
  65. adotb = v1[0]*v2[0] + v1[1]*v2[1]
  66. _theta = math.acos(adotb / np.sqrt(v1.dot(v1)) / np.sqrt(v2.dot(v2)))
  67.  
  68. if v1[1]*v2[0] > v1[0]*v2[1]: dir = "cw"
  69. else: dir = "ccw"
  70. if dir == "ccw": _theta = math.pi - _theta
  71.  
  72. if _theta < max_theta:
  73. max_theta = _theta
  74. self.x1 = _x
  75. self.y1 = _y
  76. self.theta1 = self.theta0-_theta
  77.  
  78. self.X= [np.linspace(x0-(math.cos(self.theta))*self.length,
  79. x0+(math.cos(self.theta))*self.length,self.i,endpoint=True)]
  80. self.Y= [np.linspace(y0 - (math.sin(self.theta)) * self.length,
  81. y0 + (math.sin(self.theta)) * self.length, self.i, endpoint=True)]
  82. return None
  83.  
  84. def calculate(self):
  85.  
  86. def find_end(x0,y0,x_last,y_last,theta0,pts_x,pts_y):
  87. max_theta = 999
  88. for i in range(len(pts_x)):
  89. _x = pts_x[i]
  90. _y = pts_y[i]
  91. if ((_x is not x0) and (_y is not y0)) and (
  92. (_y is not y_last) and (_x is not x_last)):
  93. v1 = np.array([x_last - x0, y_last - self.y0])
  94. v2 = np.array([_x - x0, _y - self.y0])
  95. adotb = v1[0] * v2[0] + v1[1] * v2[1]
  96. _theta = math.acos(adotb / np.sqrt(v1.dot(v1)) / np.sqrt(v2.dot(v2)))
  97.  
  98. if v1[1] * v2[0] > v1[0] * v2[1]:
  99. dir = "cw"
  100. else:
  101. dir = "ccw"
  102. if dir == "ccw": _theta = math.pi - _theta
  103.  
  104. if _theta < max_theta:
  105. max_theta = _theta
  106. x1 = _x
  107. y1 = _y
  108. theta1 = theta0 - _theta
  109. return x1,y1,theta1
  110.  
  111. for i in range(20):
  112. while self.theta > self.theta1:
  113. self.theta -= self.dt
  114. self.X.append(np.linspace(self.x0-(math.cos(self.theta))*self.length,
  115. self.x0+(math.cos(self.theta))*self.length,self.i,endpoint=True))
  116. self.Y.append(np.linspace(self.y0 - (math.sin(self.theta)) * self.length,
  117. self.y0 + (math.sin(self.theta)) * self.length, self.i, endpoint=True))
  118. self.x_last = self.x0
  119. self.y_last = self.y0
  120. self.x0 = self.x1
  121. self.y0 = self.y1
  122.  
  123. #Find next point
  124. self.x1,self.y1,self.theta1 = find_end(self.x0,self.y0,self.x_last,self.y_last,self.theta,pts_x,pts_y)
  125. return None
  126. def plot(self,i):
  127. plt.plot(self.X[i],self.Y[i])
  128. return 0
  129. def movie(self):
  130. # New figure with white background
  131. fig = plt.subplots(figsize=(6,6), facecolor='white')
  132. # New axis over the whole figure, no frame and a 1:1 aspect ratio
  133. ax = fig.add_axes([0,0,1,1], frameon=False, aspect=1)
  134. line, = ax.plot([], [], lw=2)
  135. def init():
  136. line.set_data([], [])
  137. return line,
  138. def animate(i):
  139. x = self.X
  140. y = self.Y
  141. line.set_data(list(x), list(y))
  142. return line,
  143.  
  144. #anim1=animation.FuncAnimation(fig, animate, init_func=init, frames=2500, interval=1,blit=True)
  145.  
  146. #plt.show()
  147. return 0
  148.  
  149. A=windmill(pts_x[0],pts_y[0],pts_x[1],pts_y[1],pts_x,pts_y)
  150. A.calculate()
  151.  
  152.  
  153. # New figure with white background
  154. fig = plt.figure(figsize=(6,6)) #, facecolor='#ffa600'
  155. # New axis over the whole figure, no frame and a 1:1 aspect ratio
  156. ax = plt.axes(xlim=(0, 1), ylim=(0, 1))
  157. line, = ax.plot([], [], lw=2, zorder = 1, c='#003f5c')
  158.  
  159. def init():
  160. line.set_data([], [])
  161. return line,
  162. def animate(i):
  163. x = A.X[i]
  164. y = A.Y[i]
  165. line.set_data(list(x), list(y))
  166. return line,
  167.  
  168. colors = ['#00876c',
  169. '#459e70',
  170. '#9fc878',
  171. '#cedc80',
  172. '#fdcd70',
  173. '#f9ab5c',
  174. '#e5644e',
  175. '#d43d51']
  176.  
  177. scat = plt.scatter(pts_x,pts_y,c=colors[:len(pts_x)], zorder=2)
  178. #scat.set_alpha(0.5)
  179.  
  180. anim1=animation.FuncAnimation(fig, animate, init_func=init, frames=len(A.X), interval=25, repeat_delay = 100)
  181. plt.xticks([], [])
  182. plt.yticks([], [])
  183. #plt.show()
  184. anim1.save('windmill.gif', writer='pillow', fps=45)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement