-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
74 lines (59 loc) · 2.24 KB
/
main.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
#include <iostream>
#include <cmath>
#include <cstring>
#include <windows.h>
constexpr int height = 50, width = 50;
constexpr double theta_spacing = 0.07, phi_spacing = 0.02;
constexpr double R1 = 1;
constexpr double R2 = 2;
constexpr double K2 = 5;
constexpr double K1 = width * K2 * 3 / (8 * (R1 + R2));
double A = 1, B = 1;
char render(double A, double B) {
double cosA = cos(A), sinA = sin(A);
double cosB = cos(B), sinB = sin(B);
char output[width][height];
double zbuffer[width][height];
memset(output, ' ', sizeof(output));
memset(zbuffer, 0, sizeof(zbuffer));
for (double theta = 0; theta < 2 * M_PI; theta += theta_spacing) {
double costheta = cos(theta), sintheta = sin(theta);
for (double phi = 0; phi < 2 * M_PI; phi += phi_spacing) {
double cosphi = cos(phi), sinphi = sin(phi);
double circlex = R2 + R1 * costheta;
double circley = R1 * sintheta;
double x = circlex * (cosB * cosphi + sinA * sinB * sinphi) - circley * cosA * sinB;
double y = circlex * (sinB * cosphi - sinA * cosB * sinphi) + circley * cosA * cosB;
double z = K2 + cosA * circlex * sinphi + circley * sinA;
double ooz = 1 / z;
int x_projection = static_cast<int>(width / 2 + K1 * ooz * x);
int y_projection = static_cast<int>(height / 2 + K1 * ooz * y);
double luminance = cosphi * costheta * sinB - cosA * costheta * sinphi - sinA * sintheta + cosB * (
cosA * sintheta - costheta * sinA * sinphi);
if (luminance > 0) {
if (ooz > zbuffer[x_projection][y_projection]) {
zbuffer[x_projection][y_projection] = ooz;
int luminance_index = luminance * 8;
output[x_projection][y_projection] = ".,-~:;=!*#$@"[luminance_index];
}
}
}
}
printf("\x1b[H");
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
putchar(output[i][j]);
}
putchar('\n');
}
return 0;
}
int main() {
printf("\x1b[2J");
while (true) {
render(A, B);
A += theta_spacing;
B += phi_spacing;
}
return 0;
}