]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tclock.c
ncurses 4.2
[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 #define ASPECT 2.2
36 #define ROUND(value) ((int)((value) + 0.5))
37
38 #define A2X(angle,radius) ROUND(ASPECT * radius * sin(angle))
39 #define A2Y(angle,radius) ROUND(radius * cos(angle))
40
41 /* Plot a point */
42 static void
43 plot(int x,int y,char col)
44 {
45   mvaddch(y,x,(chtype)col);
46 }
47  
48
49 /* Draw a diagonal(arbitrary) line using Bresenham's alogrithm. */
50 static void
51 dline(int from_x, int from_y, int x2, int y2, char ch)
52 {
53         int dx,dy;
54         int ax,ay;
55         int sx,sy;
56         int x,y;
57         int d;
58         
59         dx=x2-from_x;
60         dy=y2-from_y;
61         
62         ax=abs(dx*2);
63         ay=abs(dy*2);
64
65         sx=sign(dx);
66         sy=sign(dy);
67
68         x=from_x;
69         y=from_y;
70                 
71         if(ax>ay)
72         {
73                 d=ay-(ax/2);
74                 
75                 while(1)
76                 {
77                         plot(x,y,ch);
78                         if(x==x2) return;
79                         
80                         if(d>=0)
81                         {
82                                 y+=sy;
83                                 d-=ax;
84                         }
85                         x+=sx;
86                         d+=ay;                  
87                 }
88         }
89         else
90         {
91                 d=ax-(ay/2);
92                 
93                 while(1)
94                 {
95                         plot(x,y,ch);
96                         if(y==y2) return;
97                         
98                         if(d>=0)
99                         {
100                                 x+=sx;
101                                 d-=ay;
102                         }
103                         y+=sy;
104                         d+=ax;                  
105                 }       
106         }
107 }
108
109 int
110 main(
111         int argc GCC_UNUSED,
112         char *argv[] GCC_UNUSED)
113 {
114         int i,cx,cy;
115         double mradius, hradius, mangle, hangle;
116         double sangle, sradius, hours;
117         int hdx, hdy;
118         int mdx, mdy;
119         int sdx, sdy;
120         time_t tim;
121         struct tm *t;
122         char szChar[10];
123         
124         initscr();
125         noecho();
126
127         cx=39;
128         cy=12;
129         mradius=9;
130         hradius=6;
131         sradius=8;
132
133         for(i=0;i<12;i++)
134           {
135             sangle=(i+1)*(2.0*PI)/12.0;
136             sradius=10;
137             sdx = A2X(sangle, sradius);
138             sdy = A2Y(sangle, sradius);
139             sprintf(szChar,"%d",i+1);
140
141             mvaddstr((int)(cy-sdy),(int)(cx+sdx),szChar);
142           }
143
144         mvaddstr(0,0,"ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
145
146         sradius=8;
147         for(;;)
148           {
149             napms(1000);
150
151             tim = time(0);
152             t = localtime(&tim);
153
154             hours = (t->tm_hour + (t->tm_min/60.0));
155             if(hours>12.0) hours -= 12.0;
156
157             mangle = ((t->tm_min) * (2 * PI)/60.0);
158             mdx    = A2X(mangle, mradius);
159             mdy    = A2Y(mangle, mradius);
160             
161             hangle = ((hours) * (2.0 * PI)/12.0);
162             hdx    = A2X(hangle, hradius);
163             hdy    = A2Y(hangle, hradius);
164        
165             sangle = ((t->tm_sec) * (2.0 * PI)/60.0);
166             sdx    = A2X(sangle, sradius);
167             sdy    = A2Y(sangle, sradius);
168
169             plot(cx+sdx,cy-sdy,'O');
170             dline(cx,cy,cx+hdx,cy-hdy,'.');
171             dline(cx,cy,cx+mdx,cy-mdy,'#');
172
173             mvaddstr(23,0,ctime(&tim));
174             refresh();
175             if ((t->tm_sec % 5) == 0) beep();
176
177             plot(cx+sdx,cy-sdy,' ');
178             dline(cx,cy,cx+hdx,cy-hdy,' ');
179             dline(cx,cy,cx+mdx,cy-mdy,' ');
180             
181           }
182
183         return 0;
184 }