Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define PI 3.14159265359
  6.  
  7. float sx, sy;
  8.  
  9. float sdCircle(float px, float py, float r) {
  10.     float dx = px - sx, dy = py - sy;
  11.     return sqrtf(dx * dx + dy * dy) - r;
  12. }
  13.  
  14. float opUnion(float d1, float d2) {
  15.     return d1 < d2 ? d1 : d2;
  16. }
  17.  
  18. #define T px + scale * r * cosf(theta), py + scale * r * sin(theta)
  19.  
  20. float f(float px, float py, float theta, float scale, int n) {
  21.     float d = 0.0f;
  22.     for (float r = 0.0f; r < 0.8f; r += 0.02f)
  23.         d = opUnion(d, sdCircle(T, 0.05f * scale * (0.95f - r)));
  24.  
  25.     if (n > 0)
  26.         for (int t = -1; t <= 1; t += 2) {
  27.             float tt = theta + t * 1.8f;
  28.             float ss = scale * 0.9f;
  29.             for (float r = 0.2f; r < 0.8f; r += 0.1f) {
  30.                 d = opUnion(d, f(T, tt, ss * 0.5f, n - 1));
  31.                 ss *= 0.8f;
  32.             }
  33.         }
  34.  
  35.     return d;
  36. }
  37.  
  38. int ribbon() {
  39.     float x = (fmodf(sy, 0.1f) / 0.1f - 0.5f) * 0.5f;
  40.     return sx >= x - 0.05f && sx <= x + 0.05f;
  41. }
  42.  
  43. int main(int argc, char* argv[]) {
  44.     int n = argc > 1 ? atoi(argv[1]) : 3;
  45.     float zoom = argc > 2 ? atof(argv[2]) : 1.0f;
  46.     for (sy = 0.8f; sy > 0.0f; sy -= 0.02f / zoom, putchar('\n'))
  47.         for (sx = -0.35f; sx < 0.35f; sx += 0.01f / zoom) {
  48.             if (f(0, 0, PI * 0.5f, 1.0f, n) < 0.0f) {
  49.                 if (sy < 0.1f)
  50.                     putchar('.');
  51.                 else {
  52.                     if (ribbon())
  53.                         putchar('=');
  54.                     else
  55.                         putchar("............................#j&o"[rand() % 32]);
  56.                 }
  57.             }
  58.             else
  59.                 putchar(' ');
  60.         }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement