Advertisement
Guest User

Linux version (possibly cross-platform) of circle illusion

a guest
Jul 23rd, 2014
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. // Original by https://codegolf.stackexchange.com/users/21158/fabricio
  2. // https://codegolf.stackexchange.com/questions/34887/make-a-circle-illusion-animation?newreg=6661e4328c904030a57b9f7646a65278
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <cmath>
  7.  
  8. // thanks to http://www.cplusplus.com/forum/unices/10491/
  9. #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__WINDOWS__) || defined(__TOS_WIN__)
  10.  
  11.   #include <windows.h>
  12.  
  13.   inline void delay( unsigned long ms )
  14.     {
  15.     Sleep( ms );
  16.     }
  17.  
  18. #else  /* presume POSIX */
  19.  
  20.   #include <unistd.h>
  21.  
  22.   inline void delay( unsigned long ms )
  23.     {
  24.     usleep( ms * 1000 );
  25.     }
  26.  
  27. #endif
  28.  
  29. int round_unique (double r) { return (r > 0.0) ? (r + 0.5) : (r - 0.5); }
  30. void print (char * buffer, int x, int y, int w, int h, char c) {
  31.     if(x < w && y < h)
  32.         buffer[y*w+x] = c;
  33. }
  34.  
  35. void flush(char * buffer, int w, int h) {
  36.     for(int y = 0; y < h; y++) {
  37.         for(int x = 0; x < w; x++) {
  38.             printf("%c", buffer[y*w+x]);
  39.         }
  40.        
  41.         printf("\n");
  42.     }
  43. }
  44.  
  45. void clear(char * buffer, int len) {
  46.     for(int i = 0; i < len; i++) {
  47.         buffer[i] = ' ';
  48.     }
  49. }
  50.  
  51. int main ()
  52. {
  53.     float pi = 3.14159265358979323846;
  54.     float circle = pi * 2;
  55.     int len = 12;
  56.     int hlen = len / 2;
  57.     int cx = 13;
  58.     int cy = 8;
  59.     float w = 11.0;
  60.     float h =  8.0;
  61.     float step = 0.0;
  62.    
  63.     int W = 30;
  64.     int H = 20;
  65.    
  66.     char buffer[W*H];
  67.    
  68.    
  69.  
  70.     while (1)
  71.     {
  72.         clear(buffer, W*H);
  73.         for (int i = 0; i < len; i++)
  74.         {
  75.            
  76.             float a = (i / (float)len) * circle;
  77.             int x = cx + round_unique(cos(a) * w);
  78.             int y = cy + round_unique(sin(a) * h);
  79.             print(buffer, x, y, W, H, 'O');
  80.  
  81.             if (i < hlen) continue;
  82.  
  83.             step -= 0.05;
  84.             float range = cos(a + step);
  85.             x = cx + round_unique(cos(a) * (w - 1) * range);
  86.             y = cy + round_unique(sin(a) * (h - 1) * range);
  87.             print(buffer, x, y, W, H, 'O');
  88.         }
  89.        
  90.         flush(buffer, W, H);
  91.         delay(100);
  92.     }
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement