home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / tcl / 2186 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  9.9 KB

  1. Path: sparky!uunet!cs.utexas.edu!news-is-not-mail
  2. From: capo@cs.utexas.edu (Charles Read)
  3. Newsgroups: comp.lang.tcl
  4. Subject: two more clocks
  5. Date: 21 Dec 1992 11:24:18 -0600
  6. Organization: CS Dept, University of Texas at Austin
  7. Lines: 395
  8. Message-ID: <1h4uk2INN4no@im4u.cs.utexas.edu>
  9. NNTP-Posting-Host: im4u.cs.utexas.edu
  10. Keywords: Tk2.3, tclX6.4, clock
  11.  
  12. Here are a couple more clocks.
  13. They are poorly sync'ed
  14. with the actual time, but still neat.
  15.  
  16. -------------------------------first clock-----------------------------------
  17. #!/usr/local/bin/wish -f
  18. #
  19. # c. read, 11-20-92
  20. #
  21. # Usage: clock [-width <number>] [-height <number>] \
  22. #              [-second <color>] [-minute <color>] [-hour <color>]
  23. #
  24. # Required: tk2.3, tclX6.4
  25.  
  26. # useful global variables
  27. set width 150.0
  28. set height 150.0
  29. set secondCol yellow
  30. set minuteCol green
  31. set hourCol white
  32. set backGround ""
  33.  
  34. # Make arcs for second, minute, and hour hands.
  35. proc layoutClock {c cwidth cheight} {
  36.   global backGround
  37.  
  38.   canvas $c.bg -width $cwidth -height $cheight -relief raised
  39.   bind $c.bg <Double-1> exit
  40.   bind $c.bg <q> exit
  41.   
  42.   # Get background of canvas.
  43.   set backGround [lindex [$c.bg configure -bg] 4]
  44.  
  45.   # layout stuff
  46.   pack append $c $c.bg {expand fill}
  47.  
  48.   # make arc items for hours.
  49.   set szX [expr {.98 * $cwidth}]
  50.   set szY [expr {.98 * $cheight}]
  51.  
  52.   set startX [expr {.01 * $cwidth}]
  53.   set startY [expr {.01 * $cheight}]
  54.   set extentX [expr {$startX + $szX}]
  55.   set extentY [expr {$startY + $szY}]
  56.   for {set i 1} {$i <= 12} {incr i} {
  57.     set startDeg [expr {90 - 30 * $i}]
  58.     $c.bg create arc $startX $startY $extentX $extentY -start $startDeg \
  59.       -extent 30 -fill $backGround -tags hour_$i -outline ""
  60.   }
  61.  
  62.   # bounding box of minute arcs
  63.   set szX [expr {.62 * $cwidth}]
  64.   set szY [expr {.62 * $cheight}]
  65.   set startMinX [expr {.19 * $cwidth}]
  66.   set startMinY [expr {.19 * $cheight}]
  67.   set extentMinX [expr {$startMinX + $szX}]
  68.   set extentMinY [expr {$startMinY + $szY}]
  69.  
  70.   # bounding box of second arcs
  71.   set szX [expr {.32 * $cwidth}]
  72.   set szY [expr {.32 * $cheight}]
  73.   set startSecX [expr {.34 * $cwidth}]
  74.   set startSecY [expr {.34 * $cheight}]
  75.   set extentSecX [expr {$startSecX + $szX}]
  76.   set extentSecY [expr {$startSecY + $szY}]
  77.  
  78.   for {set i 1} {$i <= 60} {incr i} {
  79.     set startDeg [expr {90 - 6 * $i}]
  80.     $c.bg create arc $startMinX $startMinY $extentMinX \
  81.       $extentMinY -start $startDeg -extent 6 -fill $backGround \
  82.       -tags minute_$i -outline ""
  83.     $c.bg create arc $startSecX $startSecY $extentSecX \
  84.       $extentSecY -start $startDeg -extent 6 -fill $backGround \
  85.       -tags second_$i -outline ""
  86.   }
  87. }
  88.  
  89. proc startTicking {c} {
  90.   global secondCol minuteCol hourCol
  91.  
  92.   # initialize hour, minute, and second hands.
  93.   set rightNow [fmtclock [getclock] {%I %M %S}]
  94.   set hourHand [expr {[concat "1[lvarpop rightNow 0]"] - 100}]
  95.   set minuteHand [expr {[concat "1[lvarpop rightNow 0]"] - 100}]
  96.   set secondHand [expr {[concat "1[lvarpop rightNow 0]"] - 100}]
  97.   for {set i 1} {$i <= $hourHand} {incr i} {
  98.     $c.bg itemconfigure hour_$i -fill $hourCol
  99.   }
  100.   for {set i 1} {$i <= $minuteHand} {incr i} {
  101.     $c.bg itemconfigure minute_$i -fill $minuteCol
  102.   }
  103.   for {set i 1} {$i <= $secondHand} {incr i} {
  104.     $c.bg itemconfigure second_$i -fill $secondCol
  105.   }
  106.   update
  107.  
  108.   # start the ticking.
  109.   while {1} {
  110.     sleep 1
  111.     incr secondHand
  112.     if {$secondHand == 61} then {
  113.       set secondHand 1
  114.       restartSecondClock $c
  115.     }
  116.     $c.bg itemconfigure second_$secondHand -fill $secondCol
  117.     if {$secondHand == 60} then {
  118.       incr minuteHand
  119.       if {$minuteHand == 61} then {
  120.     set minuteHand 1
  121.     restartMinuteClock $c
  122.       }
  123.       $c.bg itemconfigure minute_$minuteHand -fill $minuteCol
  124.       if {$minuteHand == 60} then {
  125.     incr hourHand
  126.     if {$hourHand == 13} then {
  127.       set hourHand 1
  128.       restartHourClock $c
  129.     }
  130.     $c.bg itemconfigure hour_$hourHand -fill $hourCol
  131.       } 
  132.      }
  133.     update
  134.   }
  135. }
  136.  
  137. proc restartSecondClock {c} {
  138.   global backGround
  139.   for {set i 1} {$i <= 60} {incr i} {
  140.     $c.bg itemconfigure second_$i -fill $backGround
  141.   }
  142. }
  143.  
  144. proc restartMinuteClock {c} {
  145.   global backGround
  146.   for {set i 1} {$i <= 60} {incr i} {
  147.     $c.bg itemconfigure minute_$i -fill $backGround
  148.   }
  149. }
  150.  
  151. proc restartHourClock {c} {
  152.   global backGround
  153.   for {set i 1} {$i <= 12} {incr i} {
  154.     $c.bg itemconfigure hour_$i -fill $backGround
  155.   }
  156. }
  157.  
  158. proc parseArgs {argc argv} {
  159.   global width height secondCol minuteCol hourCol
  160.   while {[llength $argv] > 0} {
  161.     set arg [lvarpop argv 0]
  162.     case $arg {
  163.       -width {
  164.     set width [lvarpop argv 0]
  165.       }
  166.       -height {
  167.     set height [lvarpop argv 0]
  168.       }
  169.       -second {
  170.     set secondCol [lvarpop argv 0]
  171.       }
  172.       -minute {
  173.     set minuteCol [lvarpop argv 0]
  174.       }
  175.       -hour {
  176.     set hourCol [lvarpop argv 0]
  177.       }
  178.     }
  179.   }
  180. }
  181.  
  182. parseArgs $argc $argv
  183.  
  184. wm withdraw .
  185. toplevel .clock
  186. wm title .clock "a clock for mae"
  187. layoutClock .clock $width $height
  188.  
  189. # exit on interrupts.
  190. signal trap SIGINT exit
  191.  
  192. startTicking .clock
  193.  
  194. -------------------------------second clock-------------------------
  195. #!/usr/local/bin/wish -f
  196. #
  197. # c. read, 11-20-92
  198. #
  199. # henke's world.
  200. #
  201. # Usage: clock [-width <number>] [-height <number>] \
  202. #              [-second <color>] [-minute <color>] [-hour <color>]
  203. #
  204. # Required: tk2.3, tclX6.4
  205.  
  206. # radii of circles
  207. set secondRad ""
  208. set minuteRad ""
  209. set hourRad ""
  210.  
  211. # position of hour and minute circles.
  212. set hourX ""
  213. set hourY ""
  214. set minuteX ""
  215. set minuteY ""
  216.  
  217. # color of circles
  218. set secondCol yellow
  219. set minuteCol green
  220. set hourCol white
  221.  
  222. # useful globals
  223. set width 150.0
  224. set height 150.0
  225. set pi 3.1415926
  226.  
  227. # Make hour oval; compute radius of minor axis.
  228. proc initLayout {c cwidth cheight} {
  229.   global secondRad minuteRad hourRad
  230.   
  231.   canvas $c.bg -width $cwidth -height $cheight -relief raised
  232.   bind $c.bg <Double-1> exit
  233.   bind $c.bg <q> exit
  234.  
  235.   # set radii
  236.   if {$cwidth <= $cheight} then {
  237.     set hourRad [expr {$cwidth / 4.0}]
  238.   } else {
  239.     set hourRad [$cheight {$cheight / 4.0}]
  240.   }
  241.   set minuteRad [expr {$hourRad / 4.0}]
  242.   set secondRad [expr {$minuteRad / 4.0}]
  243.  
  244.   # layout canvas
  245.   pack append $c $c.bg {expand fill}
  246.  
  247.   # make hour oval
  248.   $c.bg create oval 0 0 $cwidth $cheight
  249. }
  250.  
  251. proc startRolling {c} {
  252.   global pi width height
  253.   global hourX hourY minuteX minuteY
  254.  
  255.   # center of canvas
  256.   set cx [expr {$width / 2.0}]
  257.   set cy [expr {$height / 2.0}]
  258.  
  259.   # current time
  260.   set rightNow [fmtclock [getclock] {%I %M %S}]
  261.   set hourHand [expr {[concat "1[lvarpop rightNow 0]"] - 100}]
  262.   set minuteHand [expr {[concat "1[lvarpop rightNow 0]"] - 100}]
  263.   set secondHand [expr {[concat "1[lvarpop rightNow 0]"] - 100}]
  264.  
  265.   # initial time
  266.   moveToHour $c $cx $cy $hourHand
  267.   moveToMinute $c $minuteHand
  268.   moveToSecond $c $secondHand
  269.   update
  270.  
  271.   while {1} {
  272.     sleep 1
  273.     incr secondHand
  274.     if {$secondHand == 60} then {
  275.       set secondHand 0
  276.       incr minuteHand
  277.       if {$minuteHand == 60} then {
  278.     set minuteHand 0
  279.     incr hourHand
  280.     if {$hourHand == 13} then {
  281.       set hourHand 1
  282.     }
  283.     $c.bg delete hour
  284.     moveToHour $c $cx $cy $hourHand
  285.       }
  286.       $c.bg delete minute
  287.       moveToMinute $c $minuteHand
  288.     }
  289.     $c.bg delete second
  290.     moveToSecond $c $secondHand
  291.     update
  292.   }
  293. }
  294.  
  295. proc moveToHour {c cx cy hourHand} {
  296.   global pi hourRad hourX hourY hourCol
  297.  
  298.   # angle of hour hand.
  299.   set angle [expr {$pi/2.0 - $hourHand * $pi/6}]
  300.  
  301.   # center of hour circle.
  302.   set hourY [expr {[sin $angle] * ($cy - $hourRad)}]
  303.   set hourX [expr {[cos $angle] * ($cx - $hourRad)}]
  304.  
  305.   # translate into canvas coordinate-system.
  306.   set hourY [expr {$cy - $hourY}]
  307.   set hourX [expr {$cx + $hourX}]
  308.  
  309.   # make bbox for hour circle.
  310.   set topleftX [expr {$hourX - $hourRad}]
  311.   set topleftY [expr {$hourY - $hourRad}]
  312.   set botrightX [expr {$hourX + $hourRad}]
  313.   set botrightY [expr {$hourY + $hourRad}]
  314.  
  315.   # create the hour circle
  316.   $c.bg create oval $topleftX $topleftY $botrightX $botrightY \
  317.     -fill $hourCol -tags hour
  318. }
  319.  
  320. proc moveToMinute {c minuteHand} {
  321.   global pi hourRad minuteRad minuteCol
  322.   global minuteX minuteY hourX hourY
  323.  
  324.   # angle of minute hand.
  325.   set angle [expr {$pi/2.0 - $minuteHand * $pi/30}]
  326.   
  327.   # center of minute circle.
  328.   set minuteY [expr {[sin $angle] * ($hourRad - $minuteRad)}]
  329.   set minuteX [expr {[cos $angle] * ($hourRad - $minuteRad)}]
  330.  
  331.   # translate into hour circle
  332.   set minuteY [expr {$hourY - $minuteY}]
  333.   set minuteX [expr {$hourX + $minuteX}]
  334.  
  335.   # make bbox for minute circle.
  336.   set topleftX [expr {$minuteX - $minuteRad}]
  337.   set topleftY [expr {$minuteY - $minuteRad}]
  338.   set botrightX [expr {$minuteX + $minuteRad}]
  339.   set botrightY [expr {$minuteY + $minuteRad}]
  340.  
  341.   #create the minute circle.
  342.   $c.bg create oval $topleftX $topleftY $botrightX $botrightY \
  343.     -fill $minuteCol -tags minute
  344. }
  345.  
  346. proc moveToSecond {c secondHand} {
  347.   global pi minuteRad secondRad minuteX minuteY secondCol
  348.   
  349.   # angle of second hand.
  350.   set angle [expr {$pi/2.0 - $secondHand * $pi/30}]
  351.   
  352.   # center of second circle.
  353.   set secondY [expr {[sin $angle] * ($minuteRad - $secondRad)}]
  354.   set secondX [expr {[cos $angle] * ($minuteRad - $secondRad)}]
  355.  
  356.   # translate into minute circle
  357.   set secondY [expr {$minuteY - $secondY}]
  358.   set secondX [expr {$minuteX + $secondX}]
  359.   
  360.   # make bbox for second circle.
  361.   set topleftX [expr {$secondX - $secondRad}]
  362.   set topleftY [expr {$secondY - $secondRad}]
  363.   set botrightX [expr {$secondX + $secondRad}]
  364.   set botrightY [expr {$secondY + $secondRad}]
  365.  
  366.   #create the second circle.
  367.   $c.bg create oval $topleftX $topleftY $botrightX $botrightY \
  368.     -fill $secondCol -tags second
  369. }
  370.  
  371. # FIXME -- should allow relative lengths of radii of circles 
  372. # to be settable.  
  373. proc parseArgs {argc argv} {
  374.   global width height secondCol minuteCol hourCol
  375.   while {[llength $argv] > 0} {
  376.     set arg [lvarpop argv 0]
  377.     case $arg {
  378.       -width {
  379.     set width [lvarpop argv 0]
  380.       }
  381.       -height {
  382.     set height [lvarpop argv 0]
  383.       }
  384.       -second {
  385.     set secondCol [lvarpop argv 0]
  386.       }
  387.       -minute {
  388.     set minuteCol [lvarpop argv 0]
  389.       }
  390.       -hour {
  391.     set hourCol [lvarpop argv 0]
  392.       }
  393.     }
  394.   }
  395. }
  396.  
  397. parseArgs $argc $argv
  398.  
  399. wm withdraw .
  400. toplevel .clock
  401. initLayout .clock $width $height
  402.  
  403. # exit on interrupts
  404. signal trap SIGINT exit
  405.  
  406. startRolling .clock
  407.