Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # b_sphere_rotation.py
- # Rotating Sphere, by Al Sweigart al@inventwithpython.com
- import math, time, sys, os
- NUM_LAT_POINTS = 9
- NUM_LON_POINTS = 9
- if len(sys.argv) == 3:
- # Set size based on command line arguments:
- WIDTH = int(sys.argv[1])
- HEIGHT = int(sys.argv[2])
- else:
- WIDTH, HEIGHT = 80, 50
- DEFAULT_SCALEX = (WIDTH - 4) // 4
- DEFAULT_SCALEY = (HEIGHT - 4) // 2 # Text cells are twice as tall as they are wide, so set scaley accordingly.
- DEFAULT_TRANSLATEX = (WIDTH - 4) // 2
- DEFAULT_TRANSLATEY = (HEIGHT - 4) // 2
- def rotateXYZ(xyz, ax, ay, az):
- # NOTE: Rotates around the origin (0, 0, 0)
- # Rotate along x axis:
- x, y, z = xyz
- rotatedX = x
- rotatedY = (y * math.cos(ax)) - (z * math.sin(ax))
- rotatedZ = (y * math.sin(ax)) + (z * math.cos(ax))
- x, y, z = rotatedX, rotatedY, rotatedZ
- # Rotate along y axis:
- rotatedX = (z * math.sin(ay)) + (x * math.cos(ay))
- rotatedY = y
- rotatedZ = (z * math.cos(ay)) - (x * math.sin(ay))
- x, y, z = rotatedX, rotatedY, rotatedZ
- # Rotate along z axis:
- rotatedX = (x * math.cos(az)) - (y * math.sin(az))
- rotatedY = (x * math.sin(az)) + (y * math.cos(az))
- rotatedZ = z
- return (rotatedX, rotatedY, rotatedZ)
- def transformPoint(point, scalex=None, scaley=None, translatex=None, translatey=None):
- if scalex == None:
- scalex = DEFAULT_SCALEX
- if scaley == None:
- scaley = DEFAULT_SCALEY
- if translatex == None:
- translatex = DEFAULT_TRANSLATEX
- if translatey == None:
- translatey = DEFAULT_TRANSLATEY
- return (int(point[0] * scalex + translatex),
- int(point[1] * scaley + translatey))
- # Directions of each axis:
- # -y
- # |
- # +-- +x
- # /
- # +z
- # Set up the points of the sphere:
- points = [(0, 0, 1.0), (0, 0, -1.0)]
- for latitude in range(NUM_LAT_POINTS):
- for longitude in range(NUM_LON_POINTS):
- lat = math.acos(2 * (latitude / float(NUM_LAT_POINTS)) - 1) - (math.pi / 2)
- lon = 2 * math.pi * (longitude / float(NUM_LON_POINTS))
- x = math.cos(lat) * math.cos(lon)
- y = math.cos(lat) * math.sin(lon)
- z = math.sin(lat)
- points.append((x, y, z))
- rotatedPoints = [None] * len(points)
- rx = ry = rz = 0.0 # Rotation amounts for each axis.
- try:
- while True:
- # Rotate the cube:
- rx += 0.01# + random.randint(1, 20) / 100
- ry += 0.05# + random.randint(1, 20) / 100
- #rz += 0.05# + random.randint(1, 20) / 100
- for i, point in enumerate(points):
- rotatedPoints[i] = rotateXYZ(point, rx, ry, rz)
- # Get the points of the cube lines:
- spherePoints = []
- for point in rotatedPoints:
- spherePoints.append(transformPoint(point))
- spherePoints = tuple(frozenset(spherePoints)) # Get rid of duplicate points.
- # Draw the cube:
- print
- for y in range(0, HEIGHT, 2):
- s = ''
- for x in range(WIDTH):
- if (x, y) in spherePoints and (x, y + 1) in spherePoints:
- s += 'O' # Draw full block.
- elif (x, y) in spherePoints and (x, y + 1) not in spherePoints:
- s += '*' # Draw top half of block.
- elif not (x, y) in spherePoints and (x, y + 1) in spherePoints:
- s += '.' # Draw bottom half of block.
- else:
- s += ' ' # Draw empty space.
- print s
- print ''
- time.sleep(0.05) # Pause for a bit.
- # Erase the screen:
- if sys.platform == 'win32':
- os.system('cls')
- else:
- os.system('clear')
- except KeyboardInterrupt:
- sys.exit() # When Ctrl-C is pressed, end the program.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement