Version 2 of An analogue countdown clock-face

Updated 2003-03-26 12:56:41

This is adapted from KBK's "An analog clock in Tk".

   proc countdown {seconds} {
       init
       countdown_kernel $seconds
   }


   proc countdown_kernel seconds {
       hands $seconds
       if !$seconds return
       after 1000 [list countdown_kernel [incr seconds -1]]
   }


   proc draw_hand {angle decorations} {
       eval .c create line $::size $::size [get_xy $angle] $decorations
   }


   proc end_coordinate difference {
       set hand_length [expr $::size * .9]
       return [expr $::size + $hand_length * $difference]
   }


   proc get_xy angle {
       return [list [end_coordinate [expr sin($angle)]] \
                    [end_coordinate [expr -cos($angle)]]]
   }


   proc hands seconds {
       catch {.c delete withtag hands}

       set seconds_angle [expr $seconds * $::twopi / 60.]
       draw_hand $seconds_angle "-width 1 -tags hands"
       set minutes_angle [expr $seconds_angle / 60.]
       draw_hand $minutes_angle \
                    "-width 3 -capstyle projecting -tags hands"
   }


   proc init {} {
       catch {destroy .c}
       set ::size 30
       set full_diameter [expr 2 * $::size]
       pack [canvas .c -width $full_diameter -height $full_diameter]
       set ::twopi 6.283185
       set border 2
       set diameter [expr 2 * $::size - $border]
       .c create oval $border $border \
                      $diameter $diameter \
                      -fill white -outline black
   }

       # Example usage:  count down three minutes.
   countdown 180



  Should the clock hands advance clockwise, or counterclockwise?