42

Looking at the excellent answers here, I can't help wondering how I can accomplish two apparently simple modifications:

  1. I wanted to be able to generate a traditional Roman-number clock;
  2. It would be very helpful to add seconds to the labels, so that it would be easy to help children learn how to tell time with an analog clock.

MWE from Harish Kumar's answer to the previous question:

\documentclass[dvipsnames]{article}
\usepackage{tikz}

\begin{document}
\newcommand\clock[4][2]{%
\begin{tikzpicture}[scale=#1,line cap=round,line width=#1*3pt]
\filldraw [fill=Goldenrod!20] (0,0) circle (2cm);
\foreach \angle / \label in
{0/3, 30/2, 60/1, 90/12, 120/11, 150/10, 180/9,
210/8, 240/7, 270/6, 300/5, 330/4}
{
\draw[line width=#1*1pt] (\angle:1.8cm) -- (\angle:2cm);
\draw (\angle:1.4cm) node[scale=#1]{\textsf{\label}};
}
\foreach \angle in {0,90,180,270}
\draw[line width=#1*2pt] (\angle:1.6cm) -- (\angle:2cm);
\node[draw=none,font=\tiny,text=red,scale=#1] at (0,.9cm) {TICK-TOCK};
\draw[rotate=90,line width=#1*2pt] (0,0) -- (-#2*30-#3*30/60:0.7cm); % hours
\draw[rotate=90,line width=#1*1.5pt] (0,0) -- (-#3*6:1cm); % minutes
\draw[rotate=90,line width=#1*.6pt,red] (0,0) -- (-#4*6:1.2cm); % seconds
\path [fill=black] (0,0) circle (3pt);
\path [fill=red] (0,0) circle (1.5pt);
%
\end{tikzpicture}%
}
%%\syntax
%% \clock[<optional scaling dim>]{<hour>}{<minute>}{<seconds>}
\noindent\clock{9}{35}{55}\clock[1.2]{1}{15}{30} \clock[.9]{2}{25}{15}
\clock[.7]{12}{58}{10} \clock[.5]{6}{10}{55}
\clock[.3]{12}{20}{22} \clock[.2]{8}{0}{5}


\end{document}

I'm guessing it's something similar to:

\foreach \angle / \label in
{0/3, 30/2, 60/1, 90/12, 120/11, 150/10, 180/9,
210/8, 240/7, 270/6, 300/5, 330/4}
{
\draw[line width=#1*1pt] (\angle:1.8cm) -- (\angle:2cm);
\draw (\angle:1.4cm) node[scale=#1]{\textsf{\label}};
}

But I cannot figure out where to go from here.

  • Can you please add a full minimal working example so that people can see what packages etc you need to get your code to work (but don't add code that is not needed). This makes it much easier for people to help you. – Andrew Apr 5 '15 at 12:08
  • Both Heiko's and Harish's answers are beautiful; I cannot decide between them... I guess that's because I asked two questions in one. – Joseph Apr 5 '15 at 16:24
  • I'm a bit late to the party but... – Alenanno Oct 22 '15 at 14:45
23

I like to use styles and the key system of PGF as much as I can, so I made this little solution.

I added the PGFmath functions croman and cRoman that take an integer number and output a string (i.e. it can't be used as a mathematical input to another function) in Roman numerals. The c denotes the fact that "IIII" is used instead of "IV".

As PGFmath functions cannot be redeclared with the declare function key I'm using .code keys with a helper macro \clockF. Not the best solution but it'll work here.

The last example redefines these functions for the second (minutes) and the third (seconds) level.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\makeatletter
\pgfmathdeclarefunction{cRoman}{1}{%
  \pgfmathcroman@{#1}\edef\pgfmathresult{\uppercase{\pgfmathresult}}}
\pgfmathdeclarefunction{croman}{1}{%
  \begingroup
    \pgfmathint@{#1}%
    \ifnum\pgfmathresult<0\relax\let\pgfmathresult\pgfutil@empty
    \else\ifnum\pgfmathresult=4\def\pgfmathresult{iiii}%
    \else\edef\pgfmathresult{\romannumeral\pgfmathresult}\fi\fi
  \pgfmath@smuggleone\pgfmathresult\endgroup}
\makeatother
\newcommand*\clockset[1]{\pgfqkeys{/clock}{#1}}
\pgfset{/pgf/declare function={angle(\i,\t)=90-360/\t*\i;
                               hide(\i,\n)=ifthenelse(mod(\i,\n)==0,"","\i");}}
\newcommand*\clockF[2]{\pgfkeysvalueof{/clock/f-#1/.@cmd}#2\pgfeov}
\clockset{
  clock/.style={
    name=c, ultra thick, circle, draw, minimum size=+6cm, outer sep=-.5\pgflinewidth},
  f-1/.code={angle(#1,\pgfkeysvalueof{/clock/l-1})},
  f-2/.code={angle(#1,\pgfkeysvalueof{/clock/l-2})},
  f-3/.code={angle(#1,\pgfkeysvalueof{/clock/l-3})},
  v-1/.initial=0, v-2/.initial=0, v-3/.initial=0, % 0:00:00
  every hand/.style={draw, line cap=round},
  levels/.initial=3, l-1/.initial=12, l-2/.initial=60, l-3/.initial=60,
  line path/.style n args={3}{
    insert path={(c.{\clockF{#2}{#1}}) -- ++({180+\clockF{#2}{#1}}:#3)}},
  hand path/.style n args={3}{insert path={(c.center) -- ++ ({\clockF{#2}{#1}}:#3)}},
  l-1-path/.style={draw, /clock/line path={#1}{1}{.4cm}},
  l-2-path/.style={draw, /clock/line path={#1}{2}{.1cm}},
  l-1-node/.style={font=\large, pos=1, anchor={\clockF{1}{#1}},
                   node contents={\pgfmathprint{cRoman(#1)}}},
  l-2-node/.style={font=\sffamily\tiny,  pos=0, inner sep=+3pt, anchor=south,
                   rotate={-90+\clockF{2}{#1}}, node contents={#1}},
  l-3-node/.style={shape=coordinate},
  hand-1-path/.style={/clock/every hand, /clock/hand path={#1}{1}{1.0cm}, ultra thick},
  hand-2-path/.style={/clock/every hand, /clock/hand path={#1}{2}{1.4cm}, thick},
  hand-3-path/.style={/clock/every hand, /clock/hand path={#1}{3}{1.8cm}},
  time/.style args={#1:#2:#3}{
    /clock/v-1={#1+(#2+(#3)/60)/60}, /clock/v-2={#2+(#3)/60}, /clock/v-3={#3}}}
\newcommand*\clock[1][]{%
\begin{tikzpicture}\clockset{#1}
\node[/clock/clock] {};
\foreach \level in {\pgfkeysvalueof{/clock/levels},...,1}{
  \foreach \ll in {1,...,\pgfkeysvalueof{/clock/l-\level}}
    \path[/clock/l-\level-path/.try=\ll] node[/clock/l-\level-node/.try=\ll] {};
  \path[/clock/hand-\level-path/.try=\pgfkeysvalueof{/clock/v-\level}];}
\end{tikzpicture}}
\begin{document}
\clock[time=10:10:25]
\clock[l-2-node/.append style={pos=1, node contents=\pgfmathprint{hide(#1,5)},
anchor={ifthenelse(and(#1<45,#1>15),270,90)},rotate={ifthenelse(and(#1<45,#1>15),180,0)}}]
\clock[time=19:05:20, l-1=24, clock/.append style={rectangle},
  l-1-node/.append style={font=\small, node contents={#1}},
  f-2/.code={angle(#1,120)}, f-3/.code={180-angle(#1,120)}, l-2-path/.append style={red},
  l-2-node/.style={pos=1, red, font=\sffamily\tiny, sloped, anchor=east, scale=.75,
                   inner sep=+1pt, node contents={\pgfmathprint{hide(#1,5)}}},
  hand-2-path/.append style=red, hand-3-path/.append style=blue,
  l-3-path/.style={draw=blue, /clock/line path={#1}{3}{.1cm}},
  /tikz/execute at end picture={\sffamily\tikzset{nodes={inner sep=+2pt}}
    \draw[red, -latex] (90:+1.45cm) arc[start angle=90, delta angle=-180, radius=+1.45cm]
      node[midway, sloped, above]{\tiny Minutes};
    \draw[blue, -latex] (90:+1.85cm) arc[start angle=90, delta angle=+180, radius=+1.85cm]
      node[midway, sloped, above, rotate=180]{\tiny Seconds};}]
\end{document}

Output

enter image description here enter image description here enter image description here

  • 8
    Nice use of proper clock Roman numerals, especially the use of IIII for four. – dotancohen Apr 6 '15 at 13:13
  • Welcome back :-) – user11232 Apr 6 '15 at 23:23
  • +1 for the 24-hour clock. I have a (real) fully mechanical Russian clock ("Raketa", I think) that my father bought from a sailor more than 40 years ago and then gave to me... a really cherished memory (but well, minutes and seconds are traditional as in the 12-hour clock, only the hour hand was 24-hours). – Rmano Apr 9 '15 at 20:04
  • @Rmano Thank you. The arrangement of the minutes and seconds is just something different. I don't think there's a watch that displays that. I just wanted to show how easy it can be to change such trivial aspects without re-writing the whole document with the use of styles and no hard-coded values. Using a shape for the whole clock (here a rectangle) and the use of the angular borders make it even easier (and style-able). (And with execute at end picture you can basically cheat everything in there.) – Qrrbrbirlbel Apr 9 '15 at 21:55
41

There is a much simpler sans-seconds solution given in Reproducing an analog clock similar to the one provided by the ifsym package

But without bling what does a TikZ answer do good for anyways?

\documentclass[border=5mm,tikz]{standalone}
\usepackage{PTSansNarrow}
\usepackage[T1]{fontenc}
\usetikzlibrary{shadows,arrows.meta}
\begin{document}
\def\hourhand{1}
\def\minutehand{47}
\def\secondhand{32}
\begin{tikzpicture}[r/.style={font=\sffamily,inner sep=0,rotate=-30*\x,anchor=north},
s/.style={even odd rule,top color=black,bottom color=black,middle color=white}]

\shade[s,shading angle=-10] circle (21mm) circle (22mm);
\shade[s,shading angle=15]  circle (22mm) circle (25mm);

\foreach\x[evaluate={\xf=int(5*\x);}] in{1,...,12}{
\draw ({30*(3-\x)}:19mm) node[r,anchor=south,scale=0.5,inner sep=1pt] {\xf}
node[r,scale=1.5,xscale=0.85] at ({30*(3-\x)}:1.8cm) 
                               {\uppercase\expandafter{\romannumeral\x}};
\ifodd\x\else\draw (30*\x:18mm-2.5ex)--++(30*\x:-1mm);\fi
}

\foreach\x in{1,...,60}{\draw (6*\x:18mm)--++(6*\x:1mm);}
\draw circle(18mm) circle(19mm) circle(18mm-2.5ex) circle(17mm-2.5ex);

\begin{scope}[rotate around={-6*(\minutehand):(0,0)}]%\hourhand+
\fill(0.5mm,0) to[out=90,in=-100] (80:1.2cm) to[in=-80,out=120] (90:1.75cm)
to[out=-100,in=60] (100:1.2cm) to[in=90,out=-80](-0.5mm,0);
\end{scope}

\begin{scope}[rotate around={-30*(\hourhand+\minutehand/60):(0,0)}]
\fill(0.5mm,0) to[out=90,in=-150] (70:6mm) to[in=-80,out=120] (90:1.4cm)
to[out=-100,in=60] (110:6mm) to[in=90,out=-30](-0.5mm,0);
\end{scope}
\draw[thick,-{Kite[scale=0.8]}] (0,0) -- (90-6*\secondhand:1.8cm); %Seconds
\filldraw[fill=white]circle (1mm);
\end{tikzpicture}
\end{document}

enter image description here

As you can see I suddenly got bored with the clock hands :)

  • 2
    Wow!!! That definitely adds some "bling". I'd keep it to myself instead of using this to teach children... However, I'm still at a loss trying to add some functionality -- like easily defining hours, minutes and seconds like \clock{1}{15}{30} above. – Joseph Apr 5 '15 at 16:54
  • @Joseph Play around with \hourhand,\minutehand etc. values at the top. Or put everything inside a macro like the other answers :) – percusse Apr 5 '15 at 17:00
  • 1
    That is sexy.__ – John Apr 7 '15 at 21:19
34

Not claiming to add much in terms of functionality, but just more of an "icon" style clock:

\documentclass[tikz, border=5]{standalone}
\usetikzlibrary{shadows}
\tikzset{shaded/.style args={#1:#2:#3 @ #4}{
  left color=#1, right color=#3, middle color=#2, shading angle=#4
}}
\tikzset{pics/.cd, clock/.style args={#1:#2:#3}{code={
\tikzset{x=1ex, y=1ex, every path/.style={line cap=round}} 
\shade [shaded=black!75:black!50:black!25 @ 225] circle [radius=13];
\shade [shaded=black!75:black!50:black!25 @ 45]  circle [radius=12.5];
\fill [black!90] circle [radius=12];
\foreach \i [evaluate={\j=90-\i*30; \k=mod(\i,3)==0;\m=int(\i*5);}] in {1,...,12}
  \draw [white, line width=\k ? .4ex : .2ex] (\j:11.5) -- (\j:11-\k) 
    (\j:10) node [anchor=\j, text=black!20, font=\sffamily]  {\expandafter\uppercase\expandafter{\romannumeral\i}};
\shade [inner color=white, outer color=black, opacity=0.25] circle [radius=12];
\fill [gray!50, rotate=90-#1*30-#2/2-#3/120, 
  rounded corners=.5ex, drop shadow={fill=black, opacity=0.5}]
  (-3/2,3/4) -- (-3/2,-3/4) -- (7,0) -- cycle;
\fill  [gray!50, rotate=90-#2*6-#3/60, 
  rounded corners=0.5ex, drop shadow={fill=black, opacity=0.5}]
  (-3/2,3/4) -- (-3/2,-3/4) -- (11,0) -- cycle;
\fill [red!75!black, drop shadow={fill=black, opacity=0.5}, rotate=90-#3*6] (0,-.1ex) rectangle (11,.1ex);
\shade [shaded=black!50:black!25:black!10 @ 225] circle [radius=1];
\shade [shaded=black!50:black!25:black!10 @ 45] circle [radius=3/4];
\fill [white, opacity=1/16] (0:11) arc (0:180:11) 
.. controls ++(-45:10) and ++(135:10) .. cycle;
}}}

\begin{document}
\begin{tikzpicture}
\pic at (0,0) {clock={9:35:15}};
\pic at (0,5) {clock={3:15:55}};
\pic at (5,5) {clock={12:05:30}};
\pic at (5,0) {clock={6:20:43}};
\end{tikzpicture}
\end{document}

enter image description here

33

The hour values are written inside a loop:

\foreach \angle / \label in {
  0/3, 30/2, 60/1, 90/12, 120/11, 150/10,
  180/9, 210/8, 240/7, 270/6, 300/5, 330/4
}{
  % ...
  \draw (\angle:1.4cm) node[scale=#1]{%
    \textsf{\label}%
  };
}

Macro \label contains the hour number, it can be converted via \romannumeral to lowercase roman numbers and with \uppercase to uppercase roman numbers:

    \textsf{\uppercase\expandafter{\romannumeral\label}}%

The seconds are already provided in Harish Kumar's answer. This version adds the seconds outside as small red numbers, for learning purposes.

\documentclass[dvipsnames]{article}
\usepackage{tikz}

\begin{document}
\newcommand\clock[4][2]{%
  \begin{tikzpicture}[scale=#1,line cap=round,line width=#1*3pt]
    \filldraw [fill=Goldenrod!20] (0,0) circle (2cm);
    \foreach \angle / \label in {
      0/3, 30/2, 60/1, 90/12, 120/11, 150/10,
      180/9, 210/8, 240/7, 270/6, 300/5, 330/4
    }{
      \draw[line width=#1*1pt] (\angle:1.8cm) -- (\angle:2cm);
      \draw (\angle:1.4cm) node[scale=#1]{%
        \textsf{\uppercase\expandafter{\romannumeral\label}}%
      };
    }   
    \foreach \angle in {0,90,180,270}
      \draw[line width=#1*2pt] (\angle:1.6cm) -- (\angle:2cm);
    \foreach \i in {0, 1, ..., 11} {
      \node[text=red, font=\tiny, scale=#1] at (\i*30:2.2cm) {%
        \pgfmathsetmacro\seconds{int(mod(75 - \i*5, 60))}%
        \textsf{\seconds}%
      };
    }   
    \node[draw=none,font=\tiny,text=red,scale=#1] at (0,.9cm) {TICK-TOCK};
    \draw[rotate=90,line width=#1*2pt] (0,0) -- (-#2*30-#3*30/60:0.7cm); % hour
    \draw[rotate=90,line width=#1*1.5pt] (0,0) -- (-#3*6:1cm); % minutes
    \draw[rotate=90,line width=#1*.6pt,red] (0,0) -- (-#4*6:1.2cm); % seconds
    \path [fill=black] (0,0) circle (3pt);
    \path [fill=red] (0,0) circle (1.5pt);
    %
  \end{tikzpicture}%
}
%%\syntax
%% \clock[<optional scaling dim>]{<hour>}{<minute>}{<seconds>}
\noindent\clock{9}{35}{55}

\end{document}

Result

  • Ah! You nailed roman issue. +1 – user11232 Apr 5 '15 at 12:54
20

Add these:

\foreach \angle [count=\label] in {84,78,...,-270}
{
\draw[line width=1pt] (\angle:1.9cm) -- (\angle:2cm);
\draw (\angle:1.7cm) node[scale=0.25]{\textsf{\label}};
}

Full code:

\documentclass[dvipsnames]{article}
\usepackage{tikz}

\begin{document}
\newcommand\clock[3]{%
\begin{tikzpicture}[line cap=round,line width=3pt]
\filldraw [fill=Goldenrod!30] (0,0) circle (2cm);
\foreach \angle [count=\label] in {60,30,...,-270}
{
\draw[line width=1pt] (\angle:1.8cm) -- (\angle:2cm);
\draw (\angle:1.4cm) node{\textsf{\label}};
}
\foreach \angle [count=\label] in {84,78,...,-270}
{
\draw[line width=1pt] (\angle:1.9cm) -- (\angle:2cm);
\draw (\angle:1.7cm) node[scale=0.25]{\textsf{\label}};
}
\foreach \angle in {0,90,180,270}
\draw[line width=2.3pt] (\angle:1.6cm) -- (\angle:2cm);
\node[draw=none,font=\tiny,text=red] at (0,.9cm) {TICK-TOCK};
\draw[rotate=90,line width=2pt] (0,0) -- (-#1*30-#2*30/60:0.7cm); % hours
\draw[rotate=90,line width=1.5pt] (0,0) -- (-#2*6:1cm); % minutes
\draw[rotate=90,thin,red] (0,0) -- (-#3*6:1.2cm); % seconds
\path [fill=red] (0,0) circle (2pt);
%
\end{tikzpicture}%
}
%%\syntax
%% \clock{<hour>}{<minute>}{<seconds>}
\noindent\clock{1}{15}{30}

\end{document}

enter image description here

  • Use Heiko's techniques for roman numbers. – user11232 Apr 5 '15 at 12:54
17

A try with MetaPost, without much frills, but the result indicates the hour (in roman numerics), minute and second as required :-).

Hour, minute and second are given by the user as the three parameters of the clock macro, which draws the analog clock and a rudimentary numeric clock below it. This macro makes use of the freelabel instruction from the Metafun format, which allows correct placement of labels around a circle, and the numprint package for the numeric clock.

EDIT Now the last example gives the current hour, minute, and second. Current hour and minute are given by MetaPost internal variables hour and minute (see also Thruston's answer in this topic), but MetaPost has no variable for the current second. However, as Dohyun Kim (maintainer of the luamplib package) told me in this comment on my attempt in the preceeding related subject, Lua can produce it. So I've included Dohyun Kim's code in the last example:

second := \directlua{tex.sprint(os.date("*t").sec)};

The complete program:

\documentclass[border=5mm, multi=mplibcode]{standalone}
\usepackage{numprint}
  \nplpadding{2}
\usepackage{luamplib}
  \mplibsetformat{metafun}
  \mplibtextextlabel{enable}
  \everymplib{
    verbatimtex \leavevmode etex;
    r := 4cm; len := 10bp; min_thickness := 1.75bp; hour_thickness := 3bp;

    def clock(expr hour, minute, second) =
      path cadran; cadran = fullcircle scaled (2r); 
      fill cadran scaled 1.025; fill cadran withcolor .9white; 

      for i = 1 upto 60:
        if i mod 5 = 0: 
          j := i div 5;
          angl := 90-30j; 
          freelabel("\large\sffamily\uppercase\expandafter{\romannumeral" & decimal j & "}", 
            (r-len)*dir angl, r*dir angl);
          draw ((r, 0) -- (r - len, 0)) rotated angl withpen pencircle scaled min_thickness;
        else:
          angl := 90 - 6i;
          draw ((r, 0) -- (r - .5len, 0)) rotated angl;
          freelabel("\tiny\sffamily" & decimal i, (r-.5len)*dir angl, r*dir angl);
        fi
      endfor;

      drawarrow (origin -- r*dir(90-second*6)) cutends (0, 1.5len) withcolor red;
      pickup pencircle scaled min_thickness;
      drawarrow origin -- r*dir(90-(minute+second/60)*6) cutends (0, 3.5len);
      pickup pencircle scaled hour_thickness;
      drawarrow origin -- r*dir (90-(hour+minute/60+second/3600)*30) cutends(0, 6len);

      fill fullcircle scaled len;
      pickup pencircle;
      labeloffset := .6cm;
      picture num_watch; 
      num_watch = thelabel.bot("\huge\ttfamily" & decimal hour 
        & ":\numprint{" & decimal minute & "}:\numprint{" & decimal second & "}", r*down); 
      fill bbox num_watch withcolor .9white; 
      draw bbox num_watch withpen pencircle; 
      draw num_watch;
    enddef;

    beginfig(1);
  }
  \everyendmplib{endfig;}

\begin{document}
  \begin{mplibcode} clock(0, 0, 0); \end{mplibcode}
  \begin{mplibcode} clock(2, 7, 3); \end{mplibcode}
  \begin{mplibcode} clock(3, 27, 43); \end{mplibcode}
  \begin{mplibcode}
    second := \directlua{tex.sprint(os.date("*t").sec)}; % Courtesy of Dohyun Kim 
    clock(hour, minute, second); 
  \end{mplibcode}
\end{document}

To be typeset with LuaLaTeX. Here are the outputs of the four different examples.

enter image description here

enter image description here

enter image description here

The last is thus the current time at the moment of the compilation:

enter image description here

13

Here's one I made earlier in Metapost. (This was originally written in raw PostScript years ago when my children were learning to tell the time). I've updated it with optional roman numbers.

Use it like this to get a stamp of the current time:

beginfig(1)
draw clock(hour,minute)
endfig;

enter image description here

or like this to get a page of random clocks for your learner to practise.

beginfig(2);
  roman_numerals_please := 1;
  for i=0 upto 2:
    for j=0 upto 3:
       draw clock(floor(uniformdeviate 12), 5 * floor(uniformdeviate 12)) 
            shifted (1.414i*clock_diameter, 1.414j*clock_diameter);
    endfor 
  endfor
endfig;

enter image description here

Complete program:

prologues:=3;outputtemplate:="%j%c.eps";

vardef roman primary h = 
   if     h<5: ""  for i=1 upto h: & "I" endfor
   elseif h<9: "V" for i=6 upto h: & "I" endfor
   elseif h=9: "IX"
   else:       "X" for i=11 upto h: & "I" endfor
   fi
enddef;

newinternal clock_diameter; clock_diameter := 4cm; 
path hand[]; 
hand1 = origin .. (.257,1/50) .. (.377,1/60) & (.377,1/60) {up} .. (.40,3/50) .. (.60, 1/40) .. {right} (.75,0);
hand1 := (hand1 .. reverse hand1 reflectedabout(left,right) .. cycle) scaled 1/2 clock_diameter;

hand2 = origin  --- (.60, 1/40) .. {right} (.95,0);
hand2 := (hand2 .. reverse hand2 reflectedabout(left,right) .. cycle) scaled 1/2 clock_diameter;

% hour of the day to degrees
vardef htod(expr hrs)  = save h; h := hrs mod 12; h := (15-h) mod 12; h * 30 enddef;
vardef mtod(expr mins) = 6*((75-mins) mod 60) enddef;

vardef clock(expr hh, mm) = image(
  interim linecap := butt;

  fill fullcircle scaled clock_diameter withcolor (240/255, 240/255, 230/255);
  draw fullcircle scaled .9876 clock_diameter withcolor .8 white;
  draw fullcircle scaled clock_diameter withpen pencircle scaled 1;
  for t=0 step  6 until 359: draw (right scaled .475 clock_diameter -- right scaled .49 clock_diameter) rotated t; endfor
  for t=0 step 30 until 359: draw (right scaled .46  clock_diameter -- right scaled .49 clock_diameter) rotated t withpen pencircle scaled 1; endfor
  for h=1 upto 12:
    label(
      if known roman_numerals_please: 
         roman h infont "bchr8r" rotated ((12-h)*30)
      else: 
         decimal h infont "bchr8r" 
      fi,
    right scaled .41 clock_diameter rotated (((15-h) mod 12)*30)); 
  endfor
  filldraw hand1 rotated htod(hh+mm/60) withpen pencircle scaled 1;
  filldraw hand2 rotated mtod(mm)       withpen pencircle scaled 1;

  fill fullcircle scaled 1/12 clock_diameter;
  fill fullcircle scaled 1/20 clock_diameter withcolor .27 white;
  fill fullcircle scaled 1/40 clock_diameter withcolor .47 white;

) enddef;

beginfig(1)
draw clock(hour,minute)
endfig;
beginfig(2);
  roman_numerals_please := 1;
  for i=0 upto 2:
    for j=0 upto 3:
       draw clock(floor(uniformdeviate 12), 5 * floor(uniformdeviate 12)) 
            shifted (1.414i*clock_diameter, 1.414j*clock_diameter);
    endfor 
  endfor
endfig;
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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