home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ncurses-1.9.9e-src.tgz / tar.out / fsf / ncurses / test / tclock.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  178 lines

  1. #include <stdio.h>
  2. #include <float.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <curses.h>
  8.  
  9. /*
  10.   tclock - analog/digital clock for curses.
  11.   If it gives you joy, then
  12.   (a) I'm glad
  13.   (b) you need to get out more :-)
  14.  
  15.   This program is copyright Howard Jones, September 1994
  16.   (ha.jones@ic.ac.uk). It may be freely distributed as
  17.   long as this copyright message remains intact, and any
  18.   modifications are clearly marked as such. [In fact, if
  19.   you modify it, I wouldn't mind the modifications back,
  20.   especially if they add any nice features. A good one
  21.   would be a precalc table for the 60 hand positions, so
  22.   that the floating point stuff can be ditched. As I said,
  23.   it was a 20 hackup minute job.]
  24.   
  25.   COMING SOON: tfishtank. Be the envy of your mac-owning
  26.   colleagues.  
  27. */
  28.  
  29. /* To compile: cc -o tclock tclock.c -lcurses -lm */
  30.  
  31. #ifndef PI
  32. #define PI 3.141592654
  33. #endif
  34.  
  35. #define sign(_x) (_x<0?-1:1)
  36.  
  37. /* Plot a point */
  38. static void
  39. plot(int x,int y,char col)
  40. {
  41.   mvaddch(y,x,(chtype)col);
  42. }
  43.  
  44.  
  45. /* Draw a diagonal(arbitrary) line using Bresenham's alogrithm. */
  46. static void
  47. dline(int from_x, int from_y, int x2, int y2, char ch)
  48. {
  49.     int dx,dy;
  50.     int ax,ay;
  51.     int sx,sy;
  52.     int x,y;
  53.     int d;
  54.     
  55.     dx=x2-from_x;
  56.     dy=y2-from_y;
  57.     
  58.     ax=abs(dx*2);
  59.     ay=abs(dy*2);
  60.  
  61.     sx=sign(dx);
  62.     sy=sign(dy);
  63.  
  64.     x=from_x;
  65.     y=from_y;
  66.         
  67.     if(ax>ay)
  68.     {
  69.         d=ay-(ax/2);
  70.         
  71.         while(1)
  72.         {
  73.             plot(x,y,ch);
  74.             if(x==x2) return;
  75.             
  76.             if(d>=0)
  77.             {
  78.                 y+=sy;
  79.                 d-=ax;
  80.             }
  81.             x+=sx;
  82.             d+=ay;            
  83.         }
  84.     }
  85.     else
  86.     {
  87.         d=ax-(ay/2);
  88.         
  89.         while(1)
  90.         {
  91.             plot(x,y,ch);
  92.             if(y==y2) return;
  93.             
  94.             if(d>=0)
  95.             {
  96.                 x+=sx;
  97.                 d-=ay;
  98.             }
  99.             y+=sy;
  100.             d+=ax;            
  101.         }    
  102.     }
  103. }
  104.  
  105. int
  106. main(int argc, char **argv)
  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. }
  178.