home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / prog / graphics / clock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  4.9 KB  |  269 lines

  1. #include <LEDA/window.h>
  2. #include <time.h>
  3. #include <math.h>
  4.  
  5. const char* month[] =
  6. { "Januar",
  7.   "Februar",
  8.   "Maerz",
  9.   "April",
  10.   "Mai",
  11.   "Juni",
  12.   "Juli",
  13.   "August",
  14.   "September",
  15.   "Oktober",
  16.   "November",
  17.   "Dezember"
  18.  };
  19.  
  20.  
  21. float radius;
  22. float mx,my;
  23. int   h;
  24. int   m;
  25. int   s;
  26.  
  27. string  datum;
  28.  
  29. window* Wp;
  30.  
  31. color face_color   = white;
  32. color text_color   = black;
  33. color min_color    = white;
  34. color hour_color   = white;
  35. bool  seconds      = false;
  36.  
  37. list<point> make_hand(float rad, float l1, float l2,float pos)
  38. { circle c(mx,my,rad);
  39.   float  phi = 2*M_PI*pos/60;
  40.   point  p(mx+l1*sin(phi),my+l1*cos(phi));
  41.   double l = c.left_tangent(p).direction();
  42.   double r = c.right_tangent(p).direction();
  43.  
  44.   list<point> L;
  45.   L.append(p);
  46.   L.append(p.translate(l,l2));
  47.   L.append(p.translate(r,l2));
  48.  
  49.   return L;
  50. }
  51.  
  52.  
  53. void hour_hand(float pos,int mode)
  54.   float  length = 0.65 * radius;
  55.   color      bg = (hour_color == black) ? white : black;
  56.   list<point> P = make_hand(radius/11,length,1.3*length,pos);
  57.  
  58.   if (mode==0) // erase
  59.     { Wp->draw_filled_polygon(P,face_color);
  60.       Wp->draw_polygon(P,face_color);
  61.      }
  62.   else         // draw
  63.     { Wp->draw_filled_polygon(P,hour_color);
  64.       Wp->draw_polygon(P,bg);
  65.      }
  66. }
  67.  
  68. void min_hand(float pos,int mode)
  69.   float  length = 0.95*radius;
  70.   color      bg = (min_color == black) ? white : black;
  71.   list<point> P = make_hand(radius/13,length,1.20*length,pos);
  72.  
  73.   if (mode==0) // erase
  74.     { Wp->draw_filled_polygon(P,face_color);
  75.       Wp->draw_polygon(P,face_color);
  76.      }
  77.   else         // draw
  78.     { Wp->draw_filled_polygon(P,min_color);
  79.       Wp->draw_polygon(P,bg);
  80.       Wp->draw_disc(mx,my,radius/35,bg);
  81.      }
  82. }
  83.  
  84. void sec_hand(float pos)
  85.   float  length = 0.95*radius;
  86.   color      bg = (min_color == black) ? white : black;
  87.   list<point> P = make_hand(radius/60,length,1.15*length,pos);
  88.  
  89.   Wp->set_mode(xor_mode);
  90.   Wp->draw_filled_polygon(P,orange);
  91.   Wp->set_mode(src_mode);
  92.   Wp->draw_disc(mx,my,radius/35,bg);
  93. }
  94.  
  95.  
  96. void draw_clock()
  97.   int i;
  98.   float x,y;
  99.  
  100.   mx = (Wp->xmax()-Wp->xmin())/2;
  101.   my = (Wp->ymax()-Wp->ymin())/2;
  102.  
  103.   radius = (mx < my) ? 0.9*mx : 0.9*my;
  104.  
  105.   Wp->clear();
  106.  
  107.   Wp->draw_disc(mx,my,1.1*radius,face_color);
  108.   Wp->draw_circle(mx,my,1.1*radius,black);
  109.  
  110.   for(i = 1; i<=12; i++)
  111.   { x = mx + radius*sin(2*M_PI*i/12);
  112.     y = my + radius*cos(2*M_PI*i/12);
  113.     Wp->draw_disc(x,y,radius/30,text_color);
  114.    }
  115.  
  116.   long clock; 
  117.   time(&clock);
  118.   tm* T = localtime(&clock);
  119.  
  120.   s = T->tm_sec;
  121.   m = T->tm_min;
  122.   h = T->tm_hour;
  123.  
  124.   datum = string("%d. %s 19%0d",T->tm_mday,month[T->tm_mon], T->tm_year%100);
  125.  
  126.   Wp->set_frame_label(datum);
  127.  
  128.   hour_hand(5*(h+m/60.0),1);
  129.   min_hand(m,1);
  130.  
  131.   if (seconds) sec_hand(s);
  132.  
  133. }
  134.  
  135.  
  136. void move_hands(int hour, int min, int sec)
  137.   if (seconds && sec != s) sec_hand(s);
  138.  
  139.   if (min != m || hour != h)
  140.   { min_hand(m,0);
  141.     hour_hand(5*(h+m/60.0),0);
  142.     hour_hand(5*(hour+min/60.0),1);
  143.     min_hand(min,1);
  144.    }
  145.  
  146.   if (seconds && sec != s) sec_hand(sec);
  147.  
  148.   s = sec;
  149.   m = min;
  150.   h = hour;
  151.  }
  152.  
  153.  
  154. void sync_time()
  155. { long clock; 
  156.   time(&clock);
  157.   tm* T = localtime(&clock); 
  158.  
  159.   while (T->tm_sec > 50)
  160.   { sleep(1);
  161.     time(&clock);
  162.     T = localtime(&clock); 
  163.    } 
  164.  
  165.   move_hands(T->tm_hour,T->tm_min,T->tm_sec);
  166.  
  167.   if (h+m+s == 0) 
  168.   { datum = string("%d. %s 19%0d",T->tm_mday,month[T->tm_mon], T->tm_year%100);
  169.     Wp->set_frame_label(datum);
  170.    }
  171.  
  172.  }
  173.  
  174.  
  175. void demo()
  176. { for(int h = 0; h < 12; h++)
  177.      for(int m = 0; m < 60; m++)
  178.        for(int s = 0; s < 60; s++)
  179.           move_hands(h,m,s);
  180.  }
  181.  
  182.  
  183. #if defined(__MSDOS__)
  184. int wait_a_second()
  185. { long clock; 
  186.   time(&clock);
  187.   long clock1 = clock; 
  188.   while (clock1 == clock && Wp->get_button() == 0) time(&clock1);
  189.   return (clock1 == clock);
  190. }
  191. #else
  192. int wait_a_second()
  193. { usleep(950000);
  194.   //sleep(1);
  195.   return Wp->get_button();
  196. }
  197. #endif
  198.  
  199.  
  200. main(int argc, char** argv)
  201. {  
  202.   int height = window::screen_height()/2;
  203.   int width  = height;
  204.   int xpos   = 0;
  205.   int ypos   = 0;
  206.  
  207.   if (argc > 1)
  208.   { if (argc == 3)
  209.       { width  = atoi(argv[1]);
  210.         height = atoi(argv[2]);
  211.        }
  212.     else
  213.       if (argc == 5)
  214.         { width  = atoi(argv[1]);
  215.           height = atoi(argv[2]);
  216.           xpos   = atoi(argv[3]);
  217.           ypos   = atoi(argv[4]);
  218.          }
  219.       else 
  220.         { cout << "usage: clock [ width height [ xpos ypos ]]\n";
  221.           exit(1);
  222.          }
  223.    }
  224.  
  225.   window W(width,height,xpos,ypos);
  226.  
  227.   Wp = &W;
  228.  
  229.   W.set_show_coordinates(false);
  230.   W.set_redraw(draw_clock);
  231.  
  232.   if (!W.mono())
  233.   { face_color   = violet;
  234.     text_color   = black;
  235.     min_color    = red;
  236.     hour_color   = red;
  237.    }
  238.  
  239.   panel P("clock panel");
  240.  
  241.   P.bool_item("seconds", seconds);
  242.   P.color_item("face   color", face_color);
  243.   P.color_item("hour   color", hour_color);
  244.   P.color_item("minute color", min_color);
  245.   P.color_item("text   color", text_color);
  246.  
  247.   P.button("ok");
  248.   P.button("quit");
  249.  
  250.   draw_clock();
  251.  
  252.   for(;;)
  253.   { if (wait_a_second())
  254.     { if(P.open() == 1) break;
  255.       draw_clock();
  256.       continue;
  257.      }
  258.     move_hands(h,m,s+1);
  259.     if (s == 59) sync_time();
  260.    }
  261.  
  262.  return 0;
  263. }
  264.