Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | None | 0 0
  1. from __future__ import division
  2. from colorsys import hsv_to_rgb
  3. from mpl_toolkits.mplot3d import Axes3D
  4. from random import random, choice
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7.  
  8.  
  9.  
  10.  
  11. # vectorfield - main formula
  12. def vectorfield(x, y, z, params):
  13.  
  14.     b,sc,dt,ax,a1,a2,a3,f,p,q,r,s,v,w,u = params       
  15.     xyz = [x,y,z]
  16.  
  17.     i = q * np.cos(xyz[a3] / (xyz[a2] + p * (xyz[a1] + q) * np.pi + z) * np.pi)
  18.     j = r * np.sin(xyz[a2] * np.cos(np.pi * x) / (xyz[a2] + r * -(x + s)) * np.pi + x)
  19.     k = w * np.sin(xyz[a1] / (xyz[a2] + v / (xyz[a3] - xyz[a2] + w)) * np.pi + xyz[a1])
  20.  
  21.     return i, j,
  22.  
  23.  
  24.  
  25.  
  26. def plot(params):
  27.  
  28.     b,sc,dt,ax,a1,a2,a3,f,p,q,r,s,v,w,u = params
  29.  
  30.     for pts in range(sc*2):
  31.  
  32.         hue = random()
  33.         # set up initial coordinates
  34.         x = f * np.cos((pts/sc)*np.pi)
  35.         y = f * np.sin((pts/sc)*np.pi)
  36.         z = f * np.sin((pts/sc)*np.pi)
  37.  
  38.         xl, yl, zl = [], [], []
  39.  
  40.         # length of curves
  41.         for t in np.arange(0, 2000, dt):
  42.  
  43.             i, j, k = vectorfield(x, y, z, params)
  44.             x = x + (i * dt)
  45.             y = y + (j * dt)
  46.             z = z + (k * dt)
  47.             xl.append(x)
  48.             yl.append(y)
  49.             zl.append(z)
  50.            
  51.         # show curves
  52.         ax.plot(xl, yl, zl, '-', zdir='z', alpha=0.4, color = hsv_to_rgb(hue, 1, 1))
  53.  
  54.     ax.set_xlim((-b,b))
  55.     ax.set_ylim((-b,b))
  56.     ax.set_zlim((-b,b))
  57.  
  58.  
  59.  
  60.  
  61. def log(params, log=False, savepic=True, show=False):
  62.    
  63.     b,sc,dt,ax,a1,a2,a3,f,p,q,r,s,v,w,u = params   
  64.     n_frames = 1
  65.     zoom_depth = 3.
  66.     unique_id = str(random()*3) + '_'      
  67.  
  68.     if log:
  69.        
  70.         # create log file of all parameters
  71.         set_wr = open('FS%s log.txt' % unique_id, 'w+')
  72.         set_wr.write('ID: FS %s' % unique_id + '\n')
  73.         set_wr.write('b = %s (initial x,y,z lim)' % str(b) + '\n')
  74.         set_wr.write('u = %s' % str(u) + '\n')
  75.         set_wr.write('p = %s' % str(p) + '\n')
  76.         set_wr.write('q = %s' % str(q) + '\n')
  77.         set_wr.write('r = %s' % str(r) + '\n')
  78.         set_wr.write('s = %s' % str(s) + '\n')
  79.         set_wr.write('v = %s' % str(v) + '\n')
  80.         set_wr.write('w = %s' % str(w) + '\n')
  81.         set_wr.write('f = %s' % str(f) + '\n')
  82.         set_wr.write('for a1,a2,a3: 0 = x, 1 = y, 2 = z' + '\n')
  83.         set_wr.write('a1 = %s' % str(a1) + '\n')
  84.         set_wr.write('a1 = %s' % str(a2) + '\n')
  85.  
  86.         set_wr.write('a1 = %s' % str(a3) + '\n')
  87.         set_wr.write('number of drawn lines: %.0f' % sc)
  88.         set_wr.close()
  89.  
  90.     if savepic:
  91.    
  92.         for frames in range(n_frames):
  93.  
  94.             # zooms from b to b-zoom_depth and back
  95.  
  96.             fstep = (frames/(1.*n_frames)) * np.pi
  97.             fsin = zoom_depth * (np.sin(fstep))**2
  98.             lim = b - fsin
  99.            
  100.             ax.set_xlim((-lim,lim))
  101.             ax.set_ylim((-lim,lim))
  102.             ax.set_zlim((-lim,lim))
  103.  
  104.             ax.view_init(-90+frames,30+frames)
  105.             remaining_time = (2880 - (8 * frames))/60.
  106.            
  107.             fname = 'FS' + unique_id + str(frames) + '.png'
  108.             print "Saving: %s" % fname
  109.             plt.savefig(fname, format='png',dpi=300, bbox_inches='tight', pad_inches=0)
  110.    
  111.     if show:
  112.         plt.show()
  113.  
  114.  
  115.  
  116.  
  117. if __name__ == '__main__':
  118.    
  119.     from sys import argv
  120.  
  121.     b = 3.5         # axis limits
  122.     sc = 20         # number of lines  
  123.     dt = np.pi/32       # time step
  124.    
  125.     if (len(argv) == 1):
  126.         numpics = 1
  127.     else:
  128.         numpics = int(argv[1])
  129.  
  130.     print "Generating %d pictures" %numpics
  131.     for images in range(numpics):
  132.         print "Image: %d" % (images+1)
  133.        
  134.         # set up figures and axes
  135.         fig = plt.figure(figsize=(12, 12))
  136.         ax = fig.add_subplot(111, projection = '3d', aspect='equal')
  137.         ax.axis('off')
  138.         ax.set_axis_bgcolor((0,  0,  0))
  139.         ax.view_init(-90, 30)           # viewing angle
  140.    
  141.         # random vars
  142.         [a1, a2, a3] = [choice(range(3)) for _ in range(3)] # indices
  143.         f = random() * 4                    # initial radius
  144.         u = random() * 5                    # scalar size  
  145.         p = (-(u/2) + (u * random())) * np.pi           # random scalars
  146.         [q,r,s,v,w] = [-(u/2)+(u*random()) for _ in range(5)]   # random scalars
  147.        
  148.         params = [b,sc,dt,ax,a1,a2,a3,f,p,q,r,s,v,w,u]
  149.    
  150.         print "Calculating lines..."
  151.         plot(params)
  152.         log(params)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement