Advertisement
Guest User

Galaxies

a guest
Mar 14th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. from sage.all import Tachyon
  2. from math import sin,cos,tan,log,sqrt
  3. from random import random
  4.  
  5. pi = 3.14159
  6.  
  7. # How long the arms should be
  8. th_max = 3.75*pi
  9.  
  10. A = 20
  11. N = 8
  12. B = 0.04
  13.  
  14. star_size_max = 0.02
  15.  
  16. if(N < th_max/2):
  17.     print("N has to be greater than th_max/2")
  18.  
  19. # Defines how the star spread changes along the arms
  20. def star_spread(dist):
  21.     return (cos(pi/2*dist)+0.8)**1*2
  22. # Defines how the vertical star spread changes along the radius
  23. def star_vert_spread(rad):
  24.     return  (cos(pi*rad)+1)**2/4
  25. # Defines how the number of stars changes along the arms
  26. def star_count(dist):
  27.     return 200*(1-th/th_max)**2
  28.  
  29. # Galactic arm equation
  30. def r(theta):
  31.     return -A/log(B*tan(theta/(2*N)))
  32.  
  33. def X(theta):
  34.     return cos(theta)*r(theta)
  35. def Y(theta):
  36.     return sin(theta)*r(theta)
  37.  
  38. t = Tachyon(xres=1000,yres=1000, camera_center=(17,0,0))
  39. t.light((4,3,2), 100.2, (1,1,1))
  40. t.texture('star', color=(0,0,0), ambient = .4)
  41.  
  42. # Sample the path every 0.5 degree
  43. th_delta = pi/360
  44. old_px = 0
  45. old_py = 0
  46. old_sx = 0
  47. old_sy = 0
  48. path_delta = 0.1
  49.  
  50. # Move along the path until we've moved a distance of path_delta
  51. th = th_delta
  52. while th < int(th_max):
  53.     # How far have we moved in units of path_delta?
  54.     # I.e. how many steps do we need to make this loop
  55.     path_x = X(th)
  56.     path_y = Y(th)
  57.     dx = path_x-old_px
  58.     dy = path_y-old_py
  59.     delta = sqrt(dx**2 + dy**2)/path_delta
  60.    
  61.     # Normalize to a distance of path_delta
  62.     dx = dx/delta
  63.     dy = dy/delta
  64.     print(str(th/th_max*100)+"% done")
  65.  
  66.     # Always make at least one star cluster
  67.     delta = max(int(delta), 1)
  68.  
  69.     for step in range(0, delta):
  70.         step_x = old_px + dx*step
  71.         step_y = old_py + dy*step
  72.  
  73.         # Ensure that the distance between drawing clusters is always >= path_delta
  74.         if (step_x - old_sx)**2 + (step_y - old_sy)**2 < path_delta**2:
  75.             continue
  76.         old_sx = step_x
  77.         old_sy = step_y
  78.  
  79.         # How many stars, and how spread out should they be?
  80.         count   = int(star_count(th/th_max))
  81.         max_rad = star_spread(th/th_max)
  82.  
  83.         for n in range(0, count):
  84.  
  85.             # Move the star a random distance in a random direction
  86.             # Square the distance to make smaller radii more likely
  87.             alpha = random()*2*pi
  88.             beta  = (random()-0.5)*pi
  89.             displacement = random()**2*max_rad
  90.  
  91.             # Move away from the path
  92.             x = step_x + cos(alpha)*cos(beta)*displacement
  93.             y = step_y + sin(alpha)*cos(beta)*displacement
  94.             z = 0      + sin(beta)*star_vert_spread(r(th)/r(th_max))*displacement
  95.  
  96.             # Render two spheres, one on each arm
  97.             t.sphere((x,y,z),random()*star_size_max, 'star')
  98.             t.sphere((-x,-y,-z),random()*star_size_max, 'star')
  99.     old_px = path_x
  100.     old_py = path_y
  101.  
  102.     th += th_delta
  103.  
  104. t.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement