-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.cpp
More file actions
134 lines (114 loc) · 3.78 KB
/
Copy pathcube.cpp
File metadata and controls
134 lines (114 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <cmath>
#include <cstring>
#include <thread>
#include <chrono>
#include <vector>
#include <algorithm>
using namespace std;
const int width = 80; // Console width
const int height = 40; // Console height
const float zDistance = 5.0f; // Depth factor (for perspective)
const float scale = 20.0f; // Scale for projection
struct Vertex {
float x, y, z;
};
struct Edge {
int a, b;
};
// Draws a line between two points (using DDA algorithm)
void drawLine(int x0, int y0, int x1, int y1, char ch, char* buffer) {
int dx = x1 - x0;
int dy = y1 - y0;
int steps = max(abs(dx), abs(dy));
if (steps == 0) {
if (x0 >= 0 && x0 < width && y0 >= 0 && y0 < height)
buffer[y0 * width + x0] = ch;
return;
}
float xIncrement = dx / static_cast<float>(steps);
float yIncrement = dy / static_cast<float>(steps);
float x = x0, y = y0;
for (int i = 0; i <= steps; i++) {
int xi = round(x);
int yi = round(y);
if (xi >= 0 && xi < width && yi >= 0 && yi < height)
buffer[yi * width + xi] = ch;
x += xIncrement;
y += yIncrement;
}
}
void drawCube(float angleX, float angleY, float angleZ, float floatOffset) {
char buffer[width * height];
memset(buffer, ' ', sizeof(buffer));
// Define the cube's 8 vertices
Vertex cube[8] = {
{-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 edges (pairs of vertex indices)
vector<Edge> edges = {
{0,1}, {1,2}, {2,3}, {3,0}, // Front face
{4,5}, {5,6}, {6,7}, {7,4}, // Back face
{0,4}, {1,5}, {2,6}, {3,7} // Connecting edges
};
// Array to hold projected 2D coordinates
pair<int, int> proj[8];
// Apply rotations and projection for each vertex
for (int i = 0; i < 8; i++) {
float x = cube[i].x;
float y = cube[i].y;
float z = cube[i].z;
// Rotate around X axis
float tempY = y * cos(angleX) - z * sin(angleX);
float tempZ = y * sin(angleX) + z * cos(angleX);
y = tempY;
z = tempZ;
// Rotate around Y axis
float tempX = x * cos(angleY) + z * sin(angleY);
tempZ = -x * sin(angleY) + z * cos(angleY);
x = tempX;
z = tempZ;
// Rotate around Z axis
tempX = x * cos(angleZ) - y * sin(angleZ);
tempY = x * sin(angleZ) + y * cos(angleZ);
x = tempX;
y = tempY;
// Apply vertical (floating) offset
y += floatOffset;
// Perspective projection
float zFactor = 1.0f / (z + zDistance);
int screenX = static_cast<int>(width / 2 + scale * x * zFactor);
int screenY = static_cast<int>(height / 2 - scale * y * zFactor);
proj[i] = {screenX, screenY};
}
// Draw the cube edges
for (const Edge &edge : edges) {
drawLine(proj[edge.a].first, proj[edge.a].second,
proj[edge.b].first, proj[edge.b].second, '#', buffer);
}
// Output the buffer to the console
cout << "\x1b[H"; // Move cursor to top-left
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++)
cout << buffer[i * width + j];
cout << "\n";
}
}
int main() {
float angleX = 0.0f, angleY = 0.0f, angleZ = 0.0f;
float floatOffset = 0.0f;
int floatDirection = 1;
cout << "\x1b[2J"; // Clear screen
while (true) {
drawCube(angleX, angleY, angleZ, floatOffset);
angleX += 0.05f;
angleY += 0.03f;
angleZ += 0.02f;
floatOffset += floatDirection * 0.05f;
if (floatOffset > 1.0f || floatOffset < -1.0f)
floatDirection *= -1;
this_thread::sleep_for(chrono::milliseconds(50));
}
return 0;
}