https://github.com/peterajhgraham/rotating-ascii-cube
A fun C++ program that generates a rotating 3D cube using ASCII characters, and you can run it right inside your computer terminal!
https://github.com/peterajhgraham/rotating-ascii-cube
ascii-art cpp terminal
Last synced: 18 days ago
JSON representation
A fun C++ program that generates a rotating 3D cube using ASCII characters, and you can run it right inside your computer terminal!
- Host: GitHub
- URL: https://github.com/peterajhgraham/rotating-ascii-cube
- Owner: peterajhgraham
- License: mit
- Created: 2024-08-21T14:57:30.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2026-05-23T19:22:54.000Z (about 2 months ago)
- Last Synced: 2026-05-23T21:19:50.728Z (about 2 months ago)
- Topics: ascii-art, cpp, terminal
- Language: C++
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rotating ASCII Cube
Real-time 3D ASCII renderer in C++ with rotation matrix math, z-buffering, and projection in ~150 lines.
A terminal program that spins a cube by composing X and Y rotation matrices each frame - `(y, z) -> (y*cosA - z*sinA, y*sinA + z*cosA)` and the Y equivalent - then projects the rotated vertices onto the screen with a perspective divide: `x' = K*x/(z+d)`, `y' = K*y/(z+d)`. Edges are rasterized with Bresenham's line algorithm. A per-pixel z-buffer keyed on `1/(z+d)` ensures nearer edges overwrite farther ones, giving correct occlusion.
https://github.com/user-attachments/assets/06601427-6c2b-435b-ba5b-ded2195fc7b9
## Directory Structure
```
ASCII_Rotating_Cube/
├── rotating_cube.cpp
├── LICENSE
├── README.md
└── .gitignore
```
## Prerequisites
* **C++ Compiler**
* **Terminal** (Unix-like systems recommended)
## Usage
1. **Clone**:
```
git clone https://github.com/peterajhgraham/Rotating_ASCII_Cube.git
cd Rotating_ASCII_Cube
```
3. **Compile**:
```
g++ -o rotating_cube rotating_cube.cpp -lm
```
4. **Run**:
```
./rotating_cube
```
## Customization
* **Rotation Speed:** You can modify the rotation speed by changing the values of `A += 0.02f;` and `B += 0.015f;` in the `rotating_cube.cpp` file
* Increase/decrease the values of A & B proportionately to avoid an uneven and distorted cube
* **Cube Size:** Adjust the cube size by modifying the `cubeSize` variable
## Notes
* Best viewed in a terminal with at least 160x44 characters
* The code uses the Bresenham's line algorithm for drawing lines between the cube's vertices