Here's Python code that simulates a rotating cube in the terminal:
import math
import time
import os
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def rotate_point(x, y, z, angle_x, angle_y, angle_z):
# Rotate around X axis
cos_x, sin_x = math.cos(angle_x), math.sin(angle_x)
y, z = y * cos_x - z * sin_x, y * sin_x + z * cos_x
# Rotate around Y axis
cos_y, sin_y = math.cos(angle_y), math.sin(angle_y)
x, z = x * cos_y + z * sin_y, -x * sin_y + z * cos_y
# Rotate around Z axis
cos_z, sin_z = math.cos(angle_z), math.sin(angle_z)
x, y = x * cos_z - y * sin_z, x * sin_z + y * cos_z
return x, y, z
def project_3d_to_2d(x, y, z, distance=5):
# Simple perspective projection
if z + distance == 0:
z = 0.001 # Avoid division by zero
factor = distance / (z + distance)
return int(x * factor * 20 + 40), int(y * factor * 10 + 12)
def draw_cube():
# Define cube vertices
vertices = [
[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1], # Back face
[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1] # Front face
]
# Define cube edges (connecting vertices)
edges = [
(0, 1), (1, 2), (2, 3), (3, 0), # Back face
(4, 5), (5, 6), (6, 7), (7, 4), # Front face
(0, 4), (1, 5), (2, 6), (3, 7) # Connecting edges
]
angle_x = angle_y = angle_z = 0
try:
while True:
clear_screen()
# Create screen buffer
screen = [[' ' for _ in range(80)] for _ in range(24)]
# Rotate and project vertices
projected_vertices = []
for vertex in vertices:
x, y, z = rotate_point(vertex[0], vertex[1], vertex[2],
angle_x, angle_y, angle_z)
screen_x, screen_y = project_3d_to_2d(x, y, z)
projected_vertices.append((screen_x, screen_y))
# Draw edges
for edge in edges:
start_vertex = projected_vertices[edge[0]]
end_vertex = projected_vertices[edge[1]]
draw_line(screen, start_vertex[0], start_vertex[1],
end_vertex[0], end_vertex[1])
# Draw vertices as points
for vertex in projected_vertices:
x, y = vertex
if 0 <= x < 80 and 0 <= y < 24:
screen[y][x] = '*'
# Print screen
for row in screen:
print(''.join(row))
# Update rotation angles
angle_x += 0.05
angle_y += 0.03
angle_z += 0.02
time.sleep(0.1)
except KeyboardInterrupt:
print("\nAnimation stopped.")
def draw_line(screen, x1, y1, x2, y2):
# Simple line drawing using Bresenham's algorithm
dx = abs(x2 - x1)
dy = abs(y2 - y1)
sx = 1 if x1 < x2 else -1
sy = 1 if y1 < y2 else -1
err = dx - dy
x, y = x1, y1
while True:
if 0 <= x < 80 and 0 <= y < 24:
screen[y][x] = '#'
if x == x2 and y == y2:
break
e2 = 2 * err
if e2 > -dy:
err -= dy
x += sx
if e2 < dx:
err += dx
y += sy
if __name__ == "__main__":
print("Rotating Cube Animation")
print("Press Ctrl+C to stop")
time.sleep(2)
draw_cube()
This code creates a 3D rotating cube animation in your terminal. Here's what it does:
The rotate_point function applies 3D rotations around all three axes using rotation matrices. The project_3d_to_2d function converts 3D coordinates to 2D screen coordinates using perspective projection. The cube is defined by 8 vertices and 12 edges connecting them.
The animation continuously rotates the cube around all three axes at different speeds, clears the screen, redraws the cube in its new position, and repeats. The cube edges are drawn with '#' characters and vertices with '*' characters.
To run it, save the code to a file (like rotating_cube.py) and run it with python rotating_cube.py. Press Ctrl+C to stop the animation. The animation works best in a terminal window that's at least 80 characters wide and 24 lines tall.