#include <windows.h>
#include <cstdio>
#include <cstring>
#include <cmath>

int main() {
    float A = 0, B = 0;
    float i, j;
    int k;
    float z[1760];
    char b[1760];

    // Clear screen
    std::printf("\x1b[2J");

    for (;;) {
        std::memset(b, 32, 1760);  // fill with spaces
        std::memset(z, 0, 1760 * sizeof(float));

        for (j = 0; j < 6.28; j += 0.07) {
            for (i = 0; i < 6.28; i += 0.02) {
                float c = std::sin(i);
                float d = std::cos(j);
                float e = std::sin(A);
                float f = std::sin(j);
                float g = std::cos(A);
                float h = d + 2;
                float D = 1 / (c * h * e + f * g + 5);
                float l = std::cos(i);
                float m = std::cos(B);
                float n = std::sin(B);
                float t = c * h * g - f * e;
                int x = int(40 + 30 * D * (l * h * m - t * n));
                int y = int(12 + 15 * D * (l * h * n + t * m));
                int o = x + 80 * y;
                int N = int(8 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n));

                if (y >= 0 && y < 22 && x >= 0 && x < 80 && D > z[o]) {
                    z[o] = D;
                    b[o] = ".,-~:;=!*#$@"[N > 0 ? N : 0];
                }
            }
        }

        std::printf("\x1b[H");
        for (k = 0; k < 1761; k++) {
            putchar(k % 80 ? b[k] : 10);
        }

        A += 0.04f;
        B += 0.02f;

        Sleep(10);  // milliseconds
    }

    return 0;
}
