Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Original by https://codegolf.stackexchange.com/users/21158/fabricio
- // https://codegolf.stackexchange.com/questions/34887/make-a-circle-illusion-animation?newreg=6661e4328c904030a57b9f7646a65278
- #include <stdio.h>
- #include <stdlib.h>
- #include <cmath>
- // thanks to http://www.cplusplus.com/forum/unices/10491/
- #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__WINDOWS__) || defined(__TOS_WIN__)
- #include <windows.h>
- inline void delay( unsigned long ms )
- {
- Sleep( ms );
- }
- #else /* presume POSIX */
- #include <unistd.h>
- inline void delay( unsigned long ms )
- {
- usleep( ms * 1000 );
- }
- #endif
- int round_unique (double r) { return (r > 0.0) ? (r + 0.5) : (r - 0.5); }
- void print (char * buffer, int x, int y, int w, int h, char c) {
- if(x < w && y < h)
- buffer[y*w+x] = c;
- }
- void flush(char * buffer, int w, int h) {
- for(int y = 0; y < h; y++) {
- for(int x = 0; x < w; x++) {
- printf("%c", buffer[y*w+x]);
- }
- printf("\n");
- }
- }
- void clear(char * buffer, int len) {
- for(int i = 0; i < len; i++) {
- buffer[i] = ' ';
- }
- }
- int main ()
- {
- float pi = 3.14159265358979323846;
- float circle = pi * 2;
- int len = 12;
- int hlen = len / 2;
- int cx = 13;
- int cy = 8;
- float w = 11.0;
- float h = 8.0;
- float step = 0.0;
- int W = 30;
- int H = 20;
- char buffer[W*H];
- while (1)
- {
- clear(buffer, W*H);
- for (int i = 0; i < len; i++)
- {
- float a = (i / (float)len) * circle;
- int x = cx + round_unique(cos(a) * w);
- int y = cy + round_unique(sin(a) * h);
- print(buffer, x, y, W, H, 'O');
- if (i < hlen) continue;
- step -= 0.05;
- float range = cos(a + step);
- x = cx + round_unique(cos(a) * (w - 1) * range);
- y = cy + round_unique(sin(a) * (h - 1) * range);
- print(buffer, x, y, W, H, 'O');
- }
- flush(buffer, W, H);
- delay(100);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement