7
$\begingroup$

I am trying to program a clock, I have the face I want and the hands but I can not seem to put them together so they work.....

Dynamic@Module[{hour, min, sec, hourhand, minutehand, secondhand}, 
  Clock[]; {hour, min, sec} = Take[DateList[], -3]; (* 
  The Dynamic Time *)
  
  hourhand = Pi/2 - 2 Pi hour/12 - 2 Pi min/720; 
  minutehand = Pi/2 - 2 Pi min/60;
  secondhand = Pi/2 - 2 Pi Floor[sec]/60; (* 
  The Clock Hands Position *)
  
  
  
  clock = 
   Graphics[{Thickness[0.013], Style[Circle[], Blue], 
     Thickness[0.003], Point[{0, 0}], (* 
     Circular Rim and center Point*)
     
     Table[
      Line[{0.9 {Cos[a], Sin[a]}, 0.95 {Cos[a], Sin[a]}}], {a, 0, 
       2 Pi, 2 Pi/60}], Thickness[0.018],(* Little Ticks *)
     
     Table[
      Line[{0.9 {Cos[a], Sin[a]}, 0.95 {Cos[a], Sin[a]}}], {a, 0, 
       2 Pi, 2 Pi/12}],(* Big Ticks *)
     
     Table[
      Rotate[Style[
        Text[IntegerString[i, "Roman"], 
         1.1 {Cos[-i Pi/6 + Pi/2], Sin[-i Pi/6 + Pi/2]}], Bold, Thick,
         35, FontFamily -> "Helvetica"], i*-30 Degree], {i, 1, 
       12}]}]](* Roman Numeral's *)


[Graphics[{Arrowheads[0.1], 
  Arrow[{{0, 0}, 0.5 {Cos[hourhand], Sin[hourhand]}}], 
  Arrow[{{0, 0}, 0.70 {Cos[minutehand], Sin[minutehand]}}], 
  Line[{{0, 0}, 0.85 {Cos[secondhand], Sin[secondhand]}}], 
  PointSize[Medium], Point[{0, 0}], 
  Circle[]}]](* Graphics for the clock Hands *)

The output is:

enter image description here

However it does not display the hands... Separately both pieces of code work correctly.

Part 1 The clock face:

    clock = Graphics[{Thickness[0.013], Style[Circle[], Blue], 
   Thickness[0.003], Point[{0, 0}], (* 
   Circular Rim and center Point*)
   
   Table[Line[{0.9 {Cos[a], Sin[a]}, 0.95 {Cos[a], Sin[a]}}], {a, 0, 
     2 Pi, 2 Pi/60}], Thickness[0.018],(* Little Ticks *)
   
   Table[Line[{0.9 {Cos[a], Sin[a]}, 0.95 {Cos[a], Sin[a]}}], {a, 0, 
     2 Pi, 2 Pi/12}],(* Big Ticks *)
   
   Table[Rotate[
     Style[Text[IntegerString[i, "Roman"], 
       1.1 {Cos[-i Pi/6 + Pi/2], Sin[-i Pi/6 + Pi/2]}], Bold, Thick, 
      35, FontFamily -> "Helvetica"], i*-30 Degree], {i, 1, 
     12}]}](* Roman Numeral's *)

Output: The same as above.

Part 2 The analog time:

  Dynamic@Module[{hour, min, sec, hourhand, minutehand, secondhand}, 
  Clock[]; {hour, min, sec} = Take[DateList[], -3];
  
  hourhand = Pi/2 - 2 Pi hour/12 - 2 Pi min/720; 
  minutehand = Pi/2 - 2 Pi min/60;
  secondhand = Pi/2 - 2 Pi Floor[sec]/60;
  Graphics[{Arrowheads[0.1], 
    Arrow[{{0, 0}, 0.5 {Cos[hourhand], Sin[hourhand]}}], 
    Arrow[{{0, 0}, 0.70 {Cos[minutehand], Sin[minutehand]}}], 
    Line[{{0, 0}, 0.85 {Cos[secondhand], Sin[secondhand]}}], 
    PointSize[Medium], Circle[]}]]

Output:

enter image description here

And the arms move with the correct system time. However I can not get them to work together.....

$\endgroup$
1
  • 1
    $\begingroup$ The first example in the "Neat Examples" section of the documentation for Graphics is to "Display an analog clock with current system time" $\endgroup$
    – Bob Hanlon
    Dec 1, 2020 at 21:04

2 Answers 2

7
$\begingroup$

You have to combine the clock face and clock hands graphics. I am using Show since that changes your code the least:

Dynamic@Module[{hour, min, sec, hourhand, minutehand, secondhand, 
   grClock, grHands},
  Clock[];
  {hour, min, sec} = Take[DateList[], -3];(*The Dynamic Time*)
  hourhand = Pi/2 - 2 Pi hour/12 - 2 Pi min/720;
  minutehand = Pi/2 - 2 Pi min/60;
  secondhand = Pi/2 - 2 Pi Floor[sec]/60;(*The Clock Hands Position*)
  grClock = 
   Graphics[{Thickness[0.013], Style[Circle[], Blue], 
     Thickness[0.003], Point[{0, 0}],(*Circular Rim and center Point*)
     Table[Line[{0.9 {Cos[a], Sin[a]}, 0.95 {Cos[a], Sin[a]}}], {a, 0,
        2 Pi, 2 Pi/60}], Thickness[0.018],(*Little Ticks*)
     Table[Line[{0.9 {Cos[a], Sin[a]}, 0.95 {Cos[a], Sin[a]}}], {a, 0,
        2 Pi, 2 Pi/12}],(*Big Ticks*)
     Table[Rotate[
       Style[Text[IntegerString[i, "Roman"], 
         1.1 {Cos[-i Pi/6 + Pi/2], Sin[-i Pi/6 + Pi/2]}], Bold, Thick,
         35, FontFamily -> "Helvetica"], i*-30 Degree], {i, 1, 
       12}]}];
  
  grHands = 
   Graphics[{Arrowheads[0.1], 
     Arrow[{{0, 0}, 0.5 {Cos[hourhand], Sin[hourhand]}}], 
     Arrow[{{0, 0}, 0.70 {Cos[minutehand], Sin[minutehand]}}], 
     Line[{{0, 0}, 0.85 {Cos[secondhand], Sin[secondhand]}}], 
     PointSize[Medium], Circle[]}];
  Show[{grClock, grHands}]
  ]

enter image description here

$\endgroup$
3
  • $\begingroup$ Sir thank you so much! I have been struggling with this for hours. I did not even now about "Show". $\endgroup$ Dec 1, 2020 at 19:56
  • 2
    $\begingroup$ Ok, good luck! We can also use Graphics[{grClock[[1]], grHands[[1]]}] instead of Show[{grClock, grHands}]. $\endgroup$ Dec 1, 2020 at 20:01
  • $\begingroup$ OK, good to know. Thanks! $\endgroup$ Dec 1, 2020 at 22:50
4
$\begingroup$

You can also use the built-in ClockGauge:

Dynamic @ Refresh[ClockGauge[Round@AbsoluteTime[], 
  Charting`LabelSide -> Left, 
  GaugeFrameSize -> None, ImageSize -> Large, 
  LabelStyle -> Directive[Black, Bold, 35], ScaleOrigin -> .75, 
  "TickLength" -> {20, 15}, 
  LabelingFunction -> (Pane[#3, ImageMargins -> 15] &), 
  TickLabels -> RotateRight[IntegerString[Range@12, "Roman"]], 
  TicksStyle -> Directive[CapForm["Butt"], Black], 
  GaugeStyle -> Black, 
  Epilog -> {Directive[Blue, AbsoluteThickness[5]], 
    Circle[{0, 0}, .8]}], UpdateInterval -> 1]

enter image description here

Could not find a convenient way to rotate and align tick labels using LabelingFunction. Below, I construct the labels separately as as an Arrow with custom Arrowheads and add it as Epilog:

arrowheads = {.1, #/12, Graphics[{Text[Style[IntegerString[#, "Roman"], 35, Bold], 
  {0, 0}, {0, -1}, N@{Cos[-Pi/12], Sin[-Pi/12]}]}]} & /@ Range[12];

hourminutesecondhands = {Graphics[{Thick, Black, Arrowheads[Large], 
     Arrow[{{0, 0}, {3, 0}}]}], 
   Graphics[{Thick, Black, Arrowheads[Large], Arrow[{{0, 0}, {3/2, 0}}]}], 
   Graphics[{Thin, Black, Line[{{0, 0}, {3/2, 0}}]}]};

Dynamic @ Refresh[ClockGauge[Round @ AbsoluteTime[], 
   GaugeFaceStyle -> None, 
   GaugeFrameSize -> None, 
   "TickLength" -> {20, 15}, 
   LabelStyle -> None, TicksStyle -> Black, 
   GaugeMarkers -> hourminutesecondhands, 
   Epilog -> {AbsoluteThickness[10], Blue, Circle[{0, 0}, .98],
     Black, "LineOpacity" -> 0, Arrowheads[arrowheads],
     Arrow[Prepend[Reverse@#, First@#] & @ CirclePoints[{1, Pi/2}, 12]], 
     Disk[{0, 0}, .03]}, 
   PlotRange -> {{-3/2, 3/2}, {-3/2, 3/2}}, ImageSize -> Large], 
  UpdateInterval -> 1]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.