Repository navigation
Expand file tree
/
Copy pathcode.cpp
More file actions
66 lines (62 loc) · 2.57 KB
/
Copy pathcode.cpp
File metadata and controls
66 lines (62 loc) · 2.57 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
#include <chrono>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <array>
#include <thread>
typedef int32_t i32;
typedef float f32;
typedef double f64;
constexpr i32 WIDTH = 82;
constexpr i32 HEIGHT = 25;
constexpr i32 BUFFER_SIZE = WIDTH * HEIGHT;
using namespace std;
int main() {
f32 rotationA = 0.0f;
f32 rotationB = 0.0f;
array<char, BUFFER_SIZE> screen;
array<f32, BUFFER_SIZE> depthBuffer;
for (;;) {
screen.fill(' ');
depthBuffer.fill(0.0f);
// cos, sin(theta) for getting the right transformation, please take a look at the README notes for a better understanding -> 6.28 ~ 2pi (90° rotation!). Phi for torsus
for (f32 theta = 0.0f; theta < 6.28f; theta += 0.07f)
for (f32 phi = 0.0f; phi < 6.28f; phi += 0.02f) {
f32 sinTheta = sin(theta);
f32 cosTheta = cos(theta);
f32 sinPhi = sin(phi);
f32 cosPhi = cos(phi);
f32 sinA = sin(rotationA);
f32 cosA = cos(rotationA);
f32 sinB = sin(rotationB);
f32 cosB = cos(rotationB); // -> now comes the torus geo (https://en.wikipedia.org/wiki/Torus)
f32 circleX = cosPhi * 2.0f;
f32 inverseDepth = 1.0f / (sinTheta * circleX * sinA + sinPhi * cosA + 5.0f);
f32 projX = sinTheta * circleX * cosA - sinPhi * sinA;
i32 x = static_cast<int>( WIDTH / 2.0f + 30 * inverseDepth * (cosTheta * circleX * cosB - projX * sinB));
i32 y = static_cast<int>( HEIGHT / 2.0f + 15 * inverseDepth * (cosTheta * circleX * sinB +projX * cosB));
if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) continue;
i32 idx = x + WIDTH * y;
int luminance = static_cast<i32>(8 * ((sinPhi * sinA - sinTheta * cosPhi * cosA) * cosB -
sinTheta * cosPhi * sinA - sinPhi * cosA - cosTheta * cosPhi * sinB));
constexpr char shades[] = ".,-~:;=!*#@";
if (inverseDepth > depthBuffer[idx]) {
depthBuffer[idx] = inverseDepth;
if (luminance < 0) luminance = 0;
if (luminance > 11) luminance = 11;
screen[idx] = shades[luminance];
}
}
cout << "\x1b[H";
for (int i = 0; i < BUFFER_SIZE; ++i) {
if (i % WIDTH == 0) cout << '\n';
cout << screen[i];
}
cout.flush();
rotationA += 0.04f;
rotationB += 0.02f;
this_thread::sleep_for(chrono::milliseconds(30));
}
}