Advertisement
ivnikkk

Untitled

Jul 13th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define debug(tl) cerr<<#tl<<' '<<tl<<'\n';
  3. #include <iostream>
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <math.h>
  7. using namespace std;
  8. #define all(d) d.begin(), d.end()
  9. typedef long double ld;
  10. namespace d2 {
  11.     struct Vec {
  12.         ld x, y;
  13.         Vec(ld c) :x(c), y(c) {};
  14.         Vec(ld _x, ld _y) : x(_x), y(_y) {};
  15.         Vec operator+(const Vec& other) { return Vec(x + other.x, y + other.y); }
  16.         Vec operator-(const Vec& other) { return Vec(x - other.x, y - other.y); }
  17.         Vec operator*(const Vec& other) { return Vec(x * other.x, y * other.y); }
  18.         Vec operator/(const Vec& other) { return Vec(x / other.x, y / other.y); }
  19.         ld len() {
  20.             return sqrtl(x * x + y * y);
  21.         }
  22.         ld len2() {
  23.             return (x * x + y * y);
  24.         }
  25.         Vec norm() {
  26.             ld d = len();
  27.             return Vec(x / d, y / d);
  28.         }
  29.     };
  30. }
  31. namespace d3 {
  32.     struct Vec {
  33.         ld x, y, z;
  34.         Vec(ld c) : x(c), y(c), z(c) {};
  35.         Vec(ld _x, d2::Vec const& v) : x(_x), y(v.x), z(v.y) {};
  36.         Vec(ld _x, ld _y, ld _z) : x(_x), y(_y), z(_z) {};
  37.         Vec operator+(Vec const& other) { return Vec(x + other.x, y + other.y, z + other.z); }
  38.         Vec operator-(Vec const& other) { return Vec(x - other.x, y - other.y, z - other.z); }
  39.         Vec operator*(Vec const& other) { return Vec(x * other.x, y * other.y, z * other.z); }
  40.         Vec operator/(Vec const& other) { return Vec(x / other.x, y / other.y, z / other.z); }
  41.         Vec operator-() { return Vec(-x, -y, -z); }
  42.         ld len() {
  43.             return sqrtl(x * x + y * y + z * z);
  44.         }
  45.         ld len2() {
  46.             return (x * x + y * y + z * z);
  47.         }
  48.         Vec norm() {
  49.             ld d = len();
  50.             return Vec(x / d, y / d, z / d);
  51.         }
  52.     };
  53. }
  54. ld get(const d3::Vec& a, const d3::Vec& b) {
  55.     return a.x * b.x + a.y * b.y + a.z * b.z;
  56. }
  57. d2::Vec intersection_shere(const d3::Vec& ro, const d3::Vec rd, ld r) {
  58.     ld a = get(ro, rd), b = get(ro, ro) - r * r;
  59.     ld c = a * a - b;
  60.     if (c < 0.000)return d2::Vec(-1.0);
  61.     c = sqrtl(c);
  62.     return d2::Vec(-a - c, -a + c);
  63. }
  64. signed main() {
  65. /*
  66. #ifdef _DEBUG
  67.     freopen("input.txt", "r", stdin);
  68.     freopen("output.txt", "w", stdout);
  69. #endif
  70. */
  71.     ios_base::sync_with_stdio(false);
  72.     cin.tie(nullptr);
  73.     cout.tie(nullptr);
  74.     int w = 120, h = 30;
  75.     ld k1 = (ld)w / h, k2 = 11.00/24.00;
  76.     char* scr = new char[w * h + 1];
  77.     vector<char> gr = { ' ','.', ':', '!','/', 'r','(', 'l','1','Z','4','H','9','W','8','$','@' };
  78.     scr[w * h] = '\0';
  79.     for (int d = 0; d < 1e5; d++) {
  80.         d3::Vec light(sin(d * 0.003), cos(d * 0.003), -1.0);
  81.         light.norm();
  82.         for (int i = 0; i < w; i++) {
  83.             for (int j = 0; j < h; j++) {
  84.                 d2::Vec p(i, j), sc(w, h);
  85.                 p = p / sc * 2.0 - 1.0;
  86.                 p.x *= k1 * k2;
  87.                 d3::Vec camera(-2, 0, 0), to(1, p);
  88.                 to = to.norm();
  89.                 int ind = 0;
  90.                 d2::Vec ins = intersection_shere(camera, to, 1);
  91.                 if (ins.x > 0) {
  92.                     d3::Vec it = camera + to * ins.x;
  93.                     it = it.norm();
  94.                     ind = (int)(get(it, light) * 20);
  95.                 }
  96.                 scr[i + j * w] = gr[max(min(ind, (int)gr.size() - 1), 0)];
  97.             }
  98.         }
  99.         printf(scr);
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement