docs
[mirrors/Programs.git] / c / spiral.c
CommitLineData
21c4e167
H
1/*
2 * Compile with:
3 * gcc -lm aclox.c -o aclox; ./aclox
4 * or
5 * CFLAGS='-lm' make aclox; ./aclox
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <math.h>
12#include <time.h>
13
14//config
15#define HOUR_NUMBERS 1
16#define HI_PRECISION 1
17#define SHOW_DIGITS 1
18#define MINUTE_DOTS 1
19#define SMOOTH_MOTION 1
20#define zoom 2
21
22//useful macros
23#define max(a,b) ((a)>=(b)?(a):(b))
24#define min(a,b) ((a)<=(b)?(a):(b))
25#define PI 3.141592654
26#define deg2rad(a) (((a)/180)*PI)
27//phi = angle, r = length
28#define polar2x(phi, r) ((r) * cos(deg2rad(phi)))
29#define polar2y(phi, r) ((r) * sin(deg2rad(phi)))
30#define cls() printf("\033[2J")
31#define top() printf("\033[0;0H")
32
33int llines = -1, lcols = -1;
34void render_clock(double a) {
35 //colors
36 char color_empty = ' ';
37 char color_s = '*';
38 char color_center = 'o';
39
40 //get size of terminal
41 static int lines = -1;
42 static int cols = -1;
43 if(lines < 7 || cols < 7) {
44 FILE *p;
45 p = popen("stty size", "r");
46 fscanf(p, "%d %d", &lines, &cols);
47 close((int)p);
48 }
49
50 //calculate size
51 int size = 40;
52 size = min(lines,cols/zoom);
53 size = ((size%2)==0?size-1:size); //make size odd
54 //printf("%d\n", size);
55
56 //empty canvas
57 char cifernik[size*zoom][size];
58 int i, x, y;
59 double phi, r;
60 for(y=0;y<size;y++) for(x=0;x<(size*zoom);x++) cifernik[x][y] = color_empty;
61
62 //draw seconds hand
63 //phi = -90+(s*(360/60));
64 //int i;
65 for(i=0;i<360;i+=28) {
66 double inc=0;
67 for(r=0;r<(size/2);r+=inc) {
68 x = round((size/2+polar2x(a+i,r))*zoom);
69 y = round(size/2+polar2y(a+i,r));
70 cifernik[x][y] = color_s;
71 a-=5;
72 inc+=0.009;
73 }
74 }
75
76 //draw center dot
77 cifernik[(size/2)*zoom][size/2] = color_center;
78
79 //cls when terminal size changes
80 if(lines != llines || cols != lcols) {
81 cls();
82 llines=lines;
83 lcols=cols;
84 }
85
86 //print to terminal
87 printf("\033[0;0H");
88 for(y=0;y<size;y++) {
89 for(r=0;r<=((cols-(size*zoom))/2);r++) putchar(' ');
90 for(x=0;x<((size*zoom)-(zoom-1));x++) {
91 putchar(cifernik[x][y]);
92 }
93 if(y!=(size-1)) putchar('\n');
94 }
95}
96
97int main(void) {
98 int a = 0;
99
100 while(1) {
101 render_clock(a=(a+5)%360);
102 usleep(100000);
103 }
104}
This page took 0.264198 seconds and 4 git commands to generate.