]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tclock.c
ncurses 4.1
[ncurses.git] / test / tclock.c
1 #include "test.priv.h"
2
3 #include <float.h>
4 #include <math.h>
5 #include <time.h>
6
7 /*
8   tclock - analog/digital clock for curses.
9   If it gives you joy, then
10   (a) I'm glad
11   (b) you need to get out more :-)
12
13   This program is copyright Howard Jones, September 1994
14   (ha.jones@ic.ac.uk). It may be freely distributed as
15   long as this copyright message remains intact, and any
16   modifications are clearly marked as such. [In fact, if
17   you modify it, I wouldn't mind the modifications back,
18   especially if they add any nice features. A good one
19   would be a precalc table for the 60 hand positions, so
20   that the floating point stuff can be ditched. As I said,
21   it was a 20 hackup minute job.]
22   
23   COMING SOON: tfishtank. Be the envy of your mac-owning
24   colleagues.  
25 */
26
27 /* To compile: cc -o tclock tclock.c -lcurses -lm */
28
29 #ifndef PI
30 #define PI 3.141592654
31 #endif
32
33 #define sign(_x) (_x<0?-1:1)
34
35 /* Plot a point */
36 static void
37 plot(int x,int y,char col)
38 {
39   mvaddch(y,x,(chtype)col);
40 }
41  
42
43 /* Draw a diagonal(arbitrary) line using Bresenham's alogrithm. */
44 static void
45 dline(int from_x, int from_y, int x2, int y2, char ch)
46 {
47         int dx,dy;
48         int ax,ay;
49         int sx,sy;
50         int x,y;
51         int d;
52         
53         dx=x2-from_x;
54         dy=y2-from_y;
55         
56         ax=abs(dx*2);
57         ay=abs(dy*2);
58
59         sx=sign(dx);
60         sy=sign(dy);
61
62         x=from_x;
63         y=from_y;
64                 
65         if(ax>ay)
66         {
67                 d=ay-(ax/2);
68                 
69                 while(1)
70                 {
71                         plot(x,y,ch);
72                         if(x==x2) return;
73                         
74                         if(d>=0)
75                         {
76                                 y+=sy;
77                                 d-=ax;
78                         }
79                         x+=sx;
80                         d+=ay;                  
81                 }
82         }
83         else
84         {
85                 d=ax-(ay/2);
86                 
87                 while(1)
88                 {
89                         plot(x,y,ch);
90                         if(y==y2) return;
91                         
92                         if(d>=0)
93                         {
94                                 x+=sx;
95                                 d-=ay;
96                         }
97                         y+=sy;
98                         d+=ax;                  
99                 }       
100         }
101 }
102
103 int
104 main(
105         int argc GCC_UNUSED,
106         char *argv[] GCC_UNUSED)
107 {
108         int i,cx,cy;
109         double mradius, hradius, mangle, hangle;
110         double sangle, sradius, hours;
111         int hdx, hdy;
112         int mdx, mdy;
113         int sdx, sdy;
114         time_t tim;
115         struct tm *t;
116         char szChar[10];
117         
118         initscr();
119         noecho();
120
121         cx=39;
122         cy=12;
123         mradius=9;
124         hradius=6;
125         sradius=8;
126
127         for(i=0;i<12;i++)
128           {
129             sangle=(i+1)*(2.0*PI)/12.0;
130             sradius=10;
131             sdx=2.0*sradius*sin(sangle);
132             sdy=sradius*cos(sangle);
133             sprintf(szChar,"%d",i+1);
134
135             mvaddstr((int)(cy-sdy),(int)(cx+sdx),szChar);
136           }
137
138         mvaddstr(0,0,"ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
139
140         sradius=8;
141         while(1)
142           {
143             sleep(1);
144
145             tim=time(0);
146             t=localtime(&tim);
147
148             hours=(t->tm_hour + (t->tm_min/60.0));
149             if(hours>12.0) hours-=12.0;
150
151             mangle=(t->tm_min)*(2*PI)/60.0;
152             mdx=2.0*mradius*sin(mangle);
153             mdy=mradius*cos(mangle);
154             
155             hangle=(hours)*(2.0*PI)/12.0;
156             hdx=2.0*hradius*sin(hangle);
157             hdy=hradius*cos(hangle);
158        
159             sangle=(t->tm_sec%60)*(2.0*PI)/60.0;
160             sdx=2.0*sradius*sin(sangle);
161             sdy=sradius*cos(sangle);
162
163             plot(cx+sdx,cy-sdy,'O');
164             dline(cx,cy,cx+hdx,cy-hdy,'.');
165             dline(cx,cy,cx+mdx,cy-mdy,'#');
166
167             mvaddstr(23,0,ctime(&tim));
168             
169             refresh();
170             plot(cx+sdx,cy-sdy,' ');
171             dline(cx,cy,cx+hdx,cy-hdy,' ');
172             dline(cx,cy,cx+mdx,cy-mdy,' ');
173             
174           }
175
176         return 0;
177 }