Rotating Cube Simulation in Python
Below is a Python script that simulates rotating a cube in the terminal. This script uses basic math to project 3D points onto a 2D plane and then rotates the cube around its center.
import os
import time
import math
# Define the vertices of the cube
vertices = [
[-1, -1, -1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, 1],
[1, -1, 1],
[1, 1, 1],
[-1, 1, 1]
]
# Define the edges of the cube
edges = [
[0, 1], [1, 2], [2, 3], [3, 0],
[4, 5], [5, 6], [6, 7], [7, 4],
[0, 4], [1, 5], [2, 6], [3, 7]
]
# Function to rotate a point around the X axis
def rotate_x(point, angle):
y = point[1] * math.cos(angle) - point[2] * math.sin(angle)
z = point[1] * math.sin(angle) + point[2] * math.cos(angle)
return [point[0], y, z]
# Function to rotate a point around the Y axis
def rotate_y(point, angle):
x = point[0] * math.cos(angle) + point[2] * math.sin(angle)
z = -point[0] * math.sin(angle) + point[2] * math.cos(angle)
return [x, point[1], z]
# Function to rotate a point around the Z axis
def rotate_z(point, angle):
x = point[0] * math.cos(angle) - point[1] * math.sin(angle)
y = point[0] * math.sin(angle) + point[1] * math.cos(angle)
return [x, y, point[2]]
# Function to project a 3D point onto a 2D plane
def project(point):
factor = 200 / (4 - point[2])
return [int(point[0] * factor + 20), int(-point[1] * factor + 10)]
# Main function to draw the cube
def draw_cube(angle_x, angle_y):
os.system('cls' if os.name == 'nt' else 'clear') # Clear the terminal
# Rotate and project vertices
projected_vertices = []
for vertex in vertices:
rotated_x = rotate_x(vertex, angle_x)
rotated_xy = rotate_y(rotated_x, angle_y)
projected_vertices.append(project(rotated_xy))
# Draw edges
for edge in edges:
p1 = projected_vertices[edge[0]]
p2 = projected_vertices[edge[1]]
dx, dy = p2[0] - p1[0], p2[1] - p1[1]
steps = max(abs(dx), abs(dy))
for i in range(steps + 1):
x = p1[0] + dx * i // steps
y = p1[1] + dy * i // steps
if 0 <= x < 40 and 0 <= y < 20: # Terminal size is 40x20
print("\033[%d;%dH*" % (y + 1, x + 1), end='', flush=True)
# Print axis labels (optional)
print("\033[1;1H^ Y", end='', flush=True)
print("\033[21;41HX>", end='', flush=True)
# Main loop to animate the rotation
angle_x, angle_y = 0, 0
while True:
draw_cube(angle_x, angle_y)
time.sleep(0.05) # Adjust the speed of rotation
angle_x += 0.05
angle_y += 0.03
How it works:
- Vertices and Edges: The cube is defined by its 8 vertices and 12 edges connecting them.
- Rotation Functions:
rotate_x, rotate_y, and rotate_z apply rotation transformations around the respective axes using basic trigonometry.
- Projection Function:
project takes a 3D point and projects it onto a 2D plane using a simple perspective projection. The factor 200 / (4 - point[2]) simulates depth; points farther away (negative Z) appear smaller.
- Drawing Function:
draw_cube clears the terminal, applies rotations and projection to all vertices, and then draws the edges by connecting the projected points. It uses ANSI escape codes to position the cursor and print asterisks (*) at the calculated positions.
- Main Loop: Continuously updates the rotation angles, redraws the cube, and waits a short period to create the animation effect.
Tips for Running:
- This script is designed for terminals with a size of at least 40 columns by 20 rows. Adjust the projection and print statements if your terminal size differs significantly.
- The rotation speed and smoothness can be adjusted by changing the
time.sleep value and the increments of angle_x and angle_y.
- For the best experience, run this script in a local environment. Online compilers might not support ANSI escape codes or the required terminal size.
Enjoy watching your rotating cube in the terminal!