Advertisement
here2share

# b_sphere_rotation.py

Dec 2nd, 2019
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. # b_sphere_rotation.py
  2.  
  3. # Rotating Sphere, by Al Sweigart al@inventwithpython.com
  4.  
  5. import math, time, sys, os
  6.  
  7. NUM_LAT_POINTS = 9
  8. NUM_LON_POINTS = 9
  9.  
  10. if len(sys.argv) == 3:
  11.     # Set size based on command line arguments:
  12.     WIDTH = int(sys.argv[1])
  13.     HEIGHT = int(sys.argv[2])
  14. else:
  15.     WIDTH, HEIGHT = 80, 50
  16. DEFAULT_SCALEX = (WIDTH - 4) // 4
  17. DEFAULT_SCALEY = (HEIGHT - 4) // 2 # Text cells are twice as tall as they are wide, so set scaley accordingly.
  18. DEFAULT_TRANSLATEX = (WIDTH - 4) // 2
  19. DEFAULT_TRANSLATEY = (HEIGHT - 4) // 2
  20.  
  21.  
  22. def rotateXYZ(xyz, ax, ay, az):
  23.     # NOTE: Rotates around the origin (0, 0, 0)
  24.  
  25.     # Rotate along x axis:
  26.     x, y, z = xyz
  27.     rotatedX = x
  28.     rotatedY = (y * math.cos(ax)) - (z * math.sin(ax))
  29.     rotatedZ = (y * math.sin(ax)) + (z * math.cos(ax))
  30.     x, y, z = rotatedX, rotatedY, rotatedZ
  31.  
  32.     # Rotate along y axis:
  33.     rotatedX = (z * math.sin(ay)) + (x * math.cos(ay))
  34.     rotatedY = y
  35.     rotatedZ = (z * math.cos(ay)) - (x * math.sin(ay))
  36.     x, y, z = rotatedX, rotatedY, rotatedZ
  37.  
  38.     # Rotate along z axis:
  39.     rotatedX = (x * math.cos(az)) - (y * math.sin(az))
  40.     rotatedY = (x * math.sin(az)) + (y * math.cos(az))
  41.     rotatedZ = z
  42.  
  43.     return (rotatedX, rotatedY, rotatedZ)
  44.  
  45.  
  46. def transformPoint(point, scalex=None, scaley=None, translatex=None, translatey=None):
  47.     if scalex == None:
  48.         scalex = DEFAULT_SCALEX
  49.     if scaley == None:
  50.         scaley = DEFAULT_SCALEY
  51.     if translatex == None:
  52.         translatex = DEFAULT_TRANSLATEX
  53.     if translatey == None:
  54.         translatey = DEFAULT_TRANSLATEY
  55.  
  56.     return (int(point[0] * scalex + translatex),
  57.             int(point[1] * scaley + translatey))
  58.  
  59.  
  60. # Directions of each axis:
  61. #  -y
  62. #   |
  63. #   +-- +x
  64. #  /
  65. # +z
  66.  
  67. # Set up the points of the sphere:
  68. points = [(0, 0, 1.0), (0, 0, -1.0)]
  69. for latitude in range(NUM_LAT_POINTS):
  70.     for longitude in range(NUM_LON_POINTS):
  71.         lat = math.acos(2 * (latitude / float(NUM_LAT_POINTS)) - 1) - (math.pi / 2)
  72.         lon = 2 * math.pi * (longitude / float(NUM_LON_POINTS))
  73.  
  74.         x = math.cos(lat) * math.cos(lon)
  75.         y = math.cos(lat) * math.sin(lon)
  76.         z = math.sin(lat)
  77.         points.append((x, y, z))
  78.  
  79.  
  80. rotatedPoints = [None] * len(points)
  81. rx = ry = rz = 0.0 # Rotation amounts for each axis.
  82.  
  83. try:
  84.     while True:
  85.         # Rotate the cube:
  86.         rx += 0.01# + random.randint(1, 20) / 100
  87.         ry += 0.05# + random.randint(1, 20) / 100
  88.         #rz += 0.05# + random.randint(1, 20) / 100
  89.         for i, point in enumerate(points):
  90.             rotatedPoints[i] = rotateXYZ(point, rx, ry, rz)
  91.  
  92.         # Get the points of the cube lines:
  93.         spherePoints = []
  94.         for point in rotatedPoints:
  95.             spherePoints.append(transformPoint(point))
  96.         spherePoints = tuple(frozenset(spherePoints)) # Get rid of duplicate points.
  97.  
  98.         # Draw the cube:
  99.         print
  100.         for y in range(0, HEIGHT, 2):
  101.             s = ''
  102.             for x in range(WIDTH):
  103.                 if (x, y) in spherePoints and (x, y + 1) in spherePoints:
  104.                     s += 'O' # Draw full block.
  105.                 elif (x, y) in spherePoints and (x, y + 1) not in spherePoints:
  106.                     s += '*' # Draw top half of block.
  107.                 elif not (x, y) in spherePoints and (x, y + 1) in spherePoints:
  108.                     s += '.' # Draw bottom half of block.
  109.                 else:
  110.                     s += ' ' # Draw empty space.
  111.             print s
  112.         print ''
  113.  
  114.         time.sleep(0.05) # Pause for a bit.
  115.  
  116.         # Erase the screen:
  117.         if sys.platform == 'win32':
  118.             os.system('cls')
  119.         else:
  120.             os.system('clear')
  121.  
  122. except KeyboardInterrupt:
  123.     sys.exit() # When Ctrl-C is pressed, end the program.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement