Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // draw analog clock
- // does the hands only
- template<typename Destination>
- void draw_clock(Destination& dst, tm& time,const srect16& bounds) {
- srect16 b = bounds.normalize();
- uint16_t w = min(b.width(),b.height());
- // using a viewport to do our rotation
- // but we're not going to draw to it
- // instead we just use its rotate
- // and translate features.
- using view_t = viewport<Destination>;
- view_t view(dst);
- view.center(spoint16(w/2,w/2));
- // create the second hand
- srect16 sr(0,0,w/16,w/2);
- sr.center_horizontal_inplace(b);
- // rotate the second hand
- view.rotation((time.tm_sec/60.0)*360);
- // here we translate each of the
- // rectangle points using the view
- spoint16 second_points[] = {
- view.translate(spoint16(sr.x1,sr.y1)),
- view.translate(spoint16(sr.x2,sr.y1)),
- view.translate(spoint16(sr.x2,sr.y2)),
- view.translate(spoint16(sr.x1,sr.y2))
- };
- spath16 second_path(4,second_points);
- // create the minute hand
- view.rotation((time.tm_min/60.0)*360);
- spoint16 minute_points[] = {
- view.translate(spoint16(sr.x1,sr.y1)),
- view.translate(spoint16(sr.x2,sr.y1)),
- view.translate(spoint16(sr.x2,sr.y2)),
- view.translate(spoint16(sr.x1,sr.y2))
- };
- spath16 minute_path(4,minute_points);
- // create the hour hand
- sr.y1 += w/8; // shorter
- view.rotation((time.tm_hour/24.0)*360);
- spoint16 hour_points[] = {
- view.translate(spoint16(sr.x1,sr.y1)),
- view.translate(spoint16(sr.x2,sr.y1)),
- view.translate(spoint16(sr.x2,sr.y2)),
- view.translate(spoint16(sr.x1,sr.y2))
- };
- spath16 hour_path(4,hour_points);
- // draw them
- draw::filled_polygon(dst,minute_path,color<typename Destination::pixel_type>::black);
- draw::filled_polygon(dst,hour_path,color<typename Destination::pixel_type>::black);
- draw::filled_polygon(dst,second_path,color<typename Destination::pixel_type>::red);
- }
Add Comment
Please, Sign In to add comment