honey_the_codewitch

analog clock hands w/ gfx

Apr 30th, 2022 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. // draw analog clock
  2. // does the hands only
  3. template<typename Destination>
  4. void draw_clock(Destination& dst, tm& time,const srect16& bounds) {
  5.   srect16 b = bounds.normalize();
  6.   uint16_t w = min(b.width(),b.height());
  7.  
  8.   // using a viewport to do our rotation
  9.   // but we're not going to draw to it
  10.   // instead we just use its rotate
  11.   // and translate features.
  12.   using view_t = viewport<Destination>;
  13.   view_t view(dst);
  14.   view.center(spoint16(w/2,w/2));
  15.  
  16.   // create the second hand
  17.   srect16 sr(0,0,w/16,w/2);
  18.   sr.center_horizontal_inplace(b);
  19.   // rotate the second hand
  20.   view.rotation((time.tm_sec/60.0)*360);
  21.   // here we translate each of the
  22.   // rectangle points using the view
  23.   spoint16 second_points[] = {
  24.     view.translate(spoint16(sr.x1,sr.y1)),
  25.     view.translate(spoint16(sr.x2,sr.y1)),
  26.     view.translate(spoint16(sr.x2,sr.y2)),
  27.     view.translate(spoint16(sr.x1,sr.y2))
  28.   };
  29.   spath16 second_path(4,second_points);
  30.  
  31.   // create the minute hand
  32.   view.rotation((time.tm_min/60.0)*360);
  33.   spoint16 minute_points[] = {
  34.     view.translate(spoint16(sr.x1,sr.y1)),
  35.     view.translate(spoint16(sr.x2,sr.y1)),
  36.     view.translate(spoint16(sr.x2,sr.y2)),
  37.     view.translate(spoint16(sr.x1,sr.y2))
  38.   };
  39.   spath16 minute_path(4,minute_points);
  40.  
  41.   // create the hour hand
  42.   sr.y1 += w/8; // shorter
  43.   view.rotation((time.tm_hour/24.0)*360);
  44.   spoint16 hour_points[] = {
  45.     view.translate(spoint16(sr.x1,sr.y1)),
  46.     view.translate(spoint16(sr.x2,sr.y1)),
  47.     view.translate(spoint16(sr.x2,sr.y2)),
  48.     view.translate(spoint16(sr.x1,sr.y2))
  49.   };
  50.   spath16 hour_path(4,hour_points);
  51.  
  52.   // draw them
  53.  
  54.   draw::filled_polygon(dst,minute_path,color<typename Destination::pixel_type>::black);
  55.  
  56.   draw::filled_polygon(dst,hour_path,color<typename Destination::pixel_type>::black);
  57.  
  58.   draw::filled_polygon(dst,second_path,color<typename Destination::pixel_type>::red);
  59.  
  60. }
Add Comment
Please, Sign In to add comment