-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube.cpp
107 lines (93 loc) · 2.77 KB
/
Cube.cpp
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
#include <iostream>
#include <cstring>
#include <cmath>
#include <chrono>
#include <thread>
#include <limits>
using namespace std;
float A, B, C;
const int width = 84, height = 24, cubeSize = 10, vSet = 57, hSet = 13;
char backGround[height * width];
float zBuffer;
float buffer[height * width];
float roll(int i, int j, int k) {
return j * sin(A) * sin(B) * cos(C)
- k * cos(A) * sin(B) * cos(C)
+ j * cos(A) * sin(C)
+ k * sin(A) * sin(C)
+ i * cos(B) * cos(C);
}
float pitch(int i, int j, int k) {
return j * cos(A) * cos(C)
+ k * sin(A) * cos(C)
- j * sin(A) * sin(B) * sin(C)
+ k * cos(A) * sin(B) * sin(C)
- i * cos(B) * sin(C);
}
float yaw(int i, int j, int k) {
return k * cos(A) * cos(B)
- j * sin(A) * cos(B)
+ i * sin(B);
}
int cubePos(int x, int y, int z) {
float X, Y, Z;
X = roll(x, y, z);
Y = pitch(x, y, z);
Z = yaw(x, y, z) + 1000;
X = (int)X + vSet;
Y = (int)Y / 2 + hSet;
zBuffer = 1 / Z;
return X + Y * width;
}
void insertSymbol(int index, char symbol) {
if (index >= 0 && index < height * width) {
if (zBuffer > buffer[index]) {
buffer[index] = zBuffer;
backGround[index] = symbol;
}
}
}
int main() {
int countEndLine = 0;
float velocity = 0.6;
int index;
while (1) {
printf("\x1b[2J\x1b[H");
memset(backGround, ' ', sizeof(backGround));
for (int i = 0; i < height * width; ++i) {
buffer[i] = -std::numeric_limits<float>::max();
}
for (float iX = -cubeSize; iX <= cubeSize; iX += velocity) {
for (float iY = -cubeSize; iY <= cubeSize; iY += velocity) {
index = cubePos(iX, iY, -cubeSize);
insertSymbol(index, '#');
index = cubePos(-cubeSize, iY, iX);
insertSymbol(index, '@');
index = cubePos(cubeSize, iY, iX);
insertSymbol(index, '%');
index = cubePos(-iX, iY, cubeSize);
insertSymbol(index, '*');
index = cubePos(iX, -cubeSize, iY);
insertSymbol(index, '+');
index = cubePos(iX, cubeSize, iY);
insertSymbol(index, '-');
}
}
string output;
output.reserve(sizeof(backGround) + height);
for (int i = 0; i < sizeof(backGround); i++) {
output += backGround[i];
countEndLine++;
if (countEndLine == width) {
output += '\n';
countEndLine = 0;
}
}
cout << output;
A += 0.06;
B += 0.02;
C += 0.08;
std::this_thread::sleep_for(std::chrono::milliseconds(70));
}
return 0;
}