18

I am trying to draw a clock similar to http://www.ebay.com.au/itm/KUMON-Study-Wall-Clock-Learning-how-to-tell-time-easily-/252645374477 to learn how to use tikz on a full page diagram

I can draw the outer part of clock and labels, however I cannot figure out how to draw and colour the inner hours in the pink and yellow in the sample clock.

How do I create the 12 inner hour nodes so I can have each hour coloured yellow and pink?

I have completed the follwing code that attempts to draw the inner hours. But I am unsure how to complete it so I can colour each node seperately

\documentclass{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\newcommand\clock{%
\begin{tikzpicture}[overlay,remember picture, cap=round,line width=3pt]
\filldraw [fill=white] (0,0) circle (10cm);
\foreach \angle [count=\label] in {60,30,...,-270} %Draw 10 minute ticks
{
\draw[line width=1pt] (\angle:9.5cm) -- (\angle:10cm); 
\draw (\angle:9cm) node{\label};
}
%
% Replace the following bit to draw inners hours in node
%
\foreach \angle [count=\label] in {60,30,...,-270} %Try to draw inner hours
{
\draw[line width=1pt] (\angle:6.25cm) -- (\angle:7.75cm);  %Draws part of inner hour
}
\draw (0,0) circle (6.25cm);
\draw (0,0) circle (7.75cm);
\foreach \angle [count=\label] in {45,15,...,-285} %Draw inner hour
{
\draw (\angle:7.cm) node{\label};
}
%
% Replace the proceeding bit
%
\foreach \angle [count=\label] in {84,78,...,-270} % Draw Second Ticks
{
\draw[line width=1pt] (\angle:9.75cm) -- (\angle:10cm);
\draw (\angle:9.5cm) node{\label};
}
\foreach \angle in {0,90,180,270}
\draw[line width=4pt] (\angle:9cm) -- (\angle:10cm);
\path [fill=red] (0,0) circle (2pt);
%
\end{tikzpicture}%
}
\clock
\end{document}

Any suggestions would be most helpful as I am not sure how to do this?

Thanks

23

Just for fun, here is a solution using contour package...

enter image description here

\documentclass[tikz]{standalone}
\usepackage{lmodern}
\usepackage[outline]{contour}
\newcommand\clock[2]{%
  \begin{tikzpicture}[cap=round,rotate=90]
    % colors
    \colorlet{minutes color}{blue!50!cyan!70!black}
    \colorlet{bg hours 0}{yellow}
    \colorlet{bg hours 1}{red!50}
    \colorlet{hours color}{red!80!black}
    % styles
    \tikzset{
      minutes/.style={circle,inner sep=0,text width=5mm,align=center,font=\bfseries},
      minutes 0/.style={fill=minutes color,text=white,minutes},
      minutes 1/.style={text=minutes color,fill=white,minutes},
      minutes font/.style={font=\normalsize},
      hours/.style={font=\fontsize{60}{66}\selectfont\bfseries,text=hours color,align=center},
      mini hours font/.style={font=\fontsize{40}{46}\selectfont\bfseries},
    }
    % radii
    \def\bigradius{80mm}
    \def\minuteradius{75mm}
    \def\hourradius{60mm}
    \def\minihourminradius{25mm}
    \def\minihourmaxradius{45mm}
    \pgfmathsetmacro\minihourradius{(\minihourmaxradius + \minihourminradius)*.5}
    \def\hourwidth{2mm}
    \def\minutewidth{1mm}

    % big circle
    \filldraw [fill=white,draw=minutes color] (0,0) circle (\bigradius);

    % minutes marks
    \foreach \angle[count=\c from 0,evaluate={\c as \hourmark using notequal(int(mod(\c,5)),0)}]
    in {0,6,...,354}{ \path (-\angle:\minuteradius) node[minutes \hourmark]{\c}; }

    % hours marks
    \foreach \angle[count=\c from 1,evaluate={\c as \col using int(mod(\c,2))}] in {30,60,...,360}{

      \path (-\angle:\hourradius) node[hours]{\c};

      \path[fill=bg hours \col]
      (-\angle:\minihourminradius) -- (-\angle:\minihourmaxradius)
      arc(-\angle:-\angle-30:\minihourmaxradius) -- (-\angle-30:\minihourminradius)
      arc(-\angle-30:-\angle:\minihourminradius) -- cycle;

      \path (-\angle-15:\minihourradius pt) node[mini hours font]{\textcolor{white}{\contour{hours color}{\c}}};
    }

    % hands
    \pgfmathsetmacro\hourangle{-#1*30-#2*.5}
    \pgfmathsetmacro\minuteangle{-#2*6}
    \fill[rotate=\hourangle,fill=hours color] ++(0,\hourwidth) arc(90:270:\hourwidth) -- ++(50mm,0)
    -- ++(\hourwidth,\hourwidth) -- ++(-\hourwidth,\hourwidth) -- ++(-50mm,0) -- cycle ;
    \fill[rotate=\minuteangle,fill=minutes color] ++(0,\minutewidth) arc(90:270:\minutewidth) -- ++(70mm,0)
    -- ++(\minutewidth,\minutewidth) -- ++(-\minutewidth,\minutewidth) -- ++(-70mm,0) -- cycle ;

  \end{tikzpicture}%
}
\begin{document}
\clock{19}{45}
\end{document}

Edit (specially for Thruston)

To add the cat and the mouse, add the following lines...

... in the preamble:

\usepackage{marvosym}
\usepackage{tikzsymbols}

... at the end of the tikzpicture:

\path (0:15mm) node[fill=cyan,circle,inner sep=0]{\Cat[3]};
\path (180:15mm) node[fill=lime,circle,inner sep=0,font=\fontsize{45}{50}\selectfont]{\ComputerMouse};

enter image description here

  • Damn, that's some neat code. I started trying to do the whole clock but it got a Little complicated with the filled blue Minute nodes... You handled it perfectly! – Guilherme Zanotelli Dec 12 '16 at 11:38
  • Wow. That is an amazing example. How long did that take to do? – Stuart Norris Dec 12 '16 at 20:09
  • Amazing! but where are the cat and the mouse?! (they are clearly visible on the OP graphic :-) – Thruston Dec 12 '16 at 23:21
  • @Thruston Done... :-) – Paul Gaborit Dec 13 '16 at 0:48
  • @StuartNorris about 15 minutes (+ 5 minutes to find the cat and the mouse). – Paul Gaborit Dec 13 '16 at 6:26
6

You can draw a thick circle

\draw[line width=1.5cm, red!30] (0,0) circle (7.00cm);

and later on fill some sectors:

\foreach \i in {60,0,...,-240}
    \fill[yellow!30] (\i:6.25cm) -- (\i:7.75cm) arc (\i:\i-30:7.75cm)--(\i-30:6.25cm) arc (\i-30:\i:6.25cm)--cycle;

enter image description here

Complete code:

\documentclass{standalone}
\usepackage{tikz}
\newcommand\clock{%
}

\begin{document}
\begin{tikzpicture}[line cap=round,line width=3pt]
\filldraw [fill=white] (0,0) circle (10cm);
\foreach \angle [count=\label] in {60,30,...,-270} %Draw 10 minute ticks
{
\draw[line width=1pt] (\angle:9.5cm) -- (\angle:10cm); 
\draw (\angle:9cm) node{\label};
}
%
% Replace the following bit to draw inners hours in node
%
\draw[line width=1.5cm, red!30] (0,0) circle (7.00cm);
\foreach \angle [count=\label, remember=\angle as \last (initially -270)] in {60,30,...,-270} %Try to draw inner hours
{
\draw[line width=1pt] (\angle:6.25cm) -- (\angle:7.75cm);  %Draws part of inner hour
%\draw[line width=1.5cm] (\last:6.25cm) arc(\last:\angle:6.25cm);  %Draws part of inner hour
}
\foreach \i in {60,0,...,-240}
    \fill[yellow!30] (\i:6.25cm) -- (\i:7.75cm) arc (\i:\i-30:7.75cm)--(\i-30:6.25cm) arc (\i-30:\i:6.25cm)--cycle;

\draw (0,0) circle (6.25cm);
\draw (0,0) circle (7.75cm);
\foreach \angle [count=\label] in {45,15,...,-285} %Draw inner hour
{
\draw (\angle:7.cm) node{\label};
}
%
% Replace the proceeding bit
%
\foreach \angle [count=\label] in {84,78,...,-270} % Draw Second Ticks
{
\draw[line width=1pt] (\angle:9.75cm) -- (\angle:10cm);
\draw (\angle:9.5cm) node{\label};
}
\foreach \angle in {0,90,180,270}
\draw[line width=4pt] (\angle:9cm) -- (\angle:10cm);
\path [fill=red] (0,0) circle (2pt);
%
\end{tikzpicture}%
\end{document}
4

One other way is to test if \label is even or odd and do the filling accordingly:

\foreach \angle [count=\label] in {60,30,...,-270} %Try to draw inner hours
{
\pgfmathparse{isodd(\label)}
\ifnum\pgfmathresult=1%
\draw[yellow, line width=1.5cm, line cap=butt] (\angle:7cm) arc[radius=7cm, start angle=\angle, delta angle=-30] node[midway, black]{\label};
\else%
\draw[pink, line width=1.5cm, line cap=butt] (\angle:7cm) arc[radius=7cm, start angle=\angle, delta angle=-30] node[midway, black]{\label};
\fi
}

That code also draws the hour labels, avoinding one extra \foreach you were using.

Here's an improved MWE, that uses the most outer circle as reference for the ticks, so if you chagen ist size almost the whole drawing adapts (only the colored circle doesn't.

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}[%overlay, remember picture,
                    line cap=round, line width=3pt]
\node[circle, draw, minimum size=20cm, outer sep=-2pt] (Clock) {};
\foreach \angle [count=\label] in {60,30,...,-270}{%Draw 10 minute ticks
  \draw[line width=1pt] (Clock.\angle) -- ++(\angle:-.5cm) node[shift={(\angle:-2em)}]{\label};
}

% Replace the following bit to draw inners hours in node
\foreach \angle [count=\label] in {60,30,...,-270}{%Try to draw inner hours
  \pgfmathparse{isodd(\label)}
  \ifnum\pgfmathresult=0%
    \draw[double=yellow!30, double distance=1.5cm, line cap=butt] (\angle:7cm) arc[radius=7cm, start angle=\angle, delta angle=-30] node[midway, black]{\label};
  \else%
    \draw[double=pink, double distance=1.5cm, line cap=butt] (\angle:7cm) arc[radius=7cm, start angle=\angle, delta angle=-30] node[midway, black]{\label};
  \fi
}

% Replace the proceeding bit
\foreach \angle [count=\label] in {84,78,...,-270}{% Draw Second Ticks
  \draw[line width=1pt] (Clock.\angle) -- ++(\angle:-.25cm) node[shift={(\angle:-1em)}]{\label};
}
\foreach \angle in {0,90,180,270} \draw[line width=4pt] (Clock.\angle) -- ++(\angle:-1cm);
\fill[red] circle (2pt);
%
\end{tikzpicture}%
\end{document}

enter image description here

  • Thank's. Now I understand what line cap=butt means. I was unsuccessfully trying with rect. That's the reason I used fill and not draw. – Ignasi Dec 12 '16 at 10:55
  • Your very welcome! There's an example in the manual that shows the difference. Another useful thing to know is that there's an arrow tip Round Cap, sometimes we want the round cap but don't want to add the extra .5\pgflinewidth at the end of the path... :) – Guilherme Zanotelli Dec 12 '16 at 11:32

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.