\documentclass{article}

\title{TikZ Tutorial Solutions}
\author{John Tanner}
\date{27 March 2025}

\usepackage{tikz} % To use \begin{tikzpicture}
\usepackage{float} % To use [H] on \begin{figure} and force figure placement
\usepackage{graphicx} % To use \includegraphics[]{}
\usepackage{hyperref} % To use \href

\begin{document}

\maketitle

\newpage

% Exercise 1
% Exercise 1
% Exercise 1
% Exercise 1
% Exercise 1

\section*{Exercise 1: Drawing a happy face}

\begin{figure}[H]
\centering
\begin{tikzpicture}
    \draw[help lines, dotted] (-4,-4) grid (4,4);
    % Draw the outline of the happy face and colour it yellow
    \draw[fill=yellow] (0,0) circle (3cm);
    % Draw the eyes
    \draw[fill=white] (-1,1) circle (0.5cm);
    \draw[fill=white] (1,1) circle (0.5cm);
    % Draw the smile
    \draw[ultra thick] ({2*cos(210)},{2*sin(210)}) arc[start angle=210,end angle=330,radius=2cm] ;
\end{tikzpicture}
\end{figure} \newpage



% Exercise 2
% Exercise 2
% Exercise 2
% Exercise 2
% Exercise 2

\section*{Exercise 2: Drawing a colourful envelope}

\begin{figure}[H]
\centering
\begin{tikzpicture}
    \draw[help lines, dotted] (-3,-2) grid (3,3);
    % Draw the envelope outline
    \draw[thick, fill=blue] (-2,-1.5) -| (2,1.5) -| cycle;
    \draw[thick, rounded corners, fill=red] (2,1.5) -- (0,3) -- (-2,1.5);
    % Draw the sine wave
    \draw[line width=1mm, red] plot[domain=-180:180, smooth, samples=200] ({\x/90}, {0.5*cos((\x*2)});
    % Draw the yellow circles
    \draw[fill=yellow] (-1,0.25) circle (2mm);
    \draw[fill=yellow] (0,-0.25) circle (2mm);
    \draw[fill=yellow] (1,0.25) circle (2mm);
    % Draw the white label saying "Hello world"
    \draw[fill=white] (-1,0.75) rectangle (1,1.25);
    \node at (0,1) {Hello world!};
\end{tikzpicture}
\end{figure} \newpage



% Exercise 3
% Exercise 3
% Exercise 3
% Exercise 3
% Exercise 3

\section*{Exercise 3: Turn a 1-star rating into a 5-star rating.}

\begin{figure}[H]
\centering
\begin{tikzpicture}
    \draw[help lines, dotted] (-5,-2) grid (5,2);
    \node at (-4,0) {\includegraphics[trim=0.1cm 0cm 13.65cm 0cm, clip, width=2cm]{Star_rating.png}};
    \node at (-2,0) {\includegraphics[trim=0.1cm 0cm 13.65cm 0cm, clip, width=2cm]{Star_rating.png}};
    \node at (0,0) {\includegraphics[trim=0.1cm 0cm 13.65cm 0cm, clip, width=2cm]{Star_rating.png}};
    \node at (2,0) {\includegraphics[trim=0.1cm 0cm 13.65cm 0cm, clip, width=2cm]{Star_rating.png}};
    \node at (4,0) {\includegraphics[trim=0.1cm 0cm 13.65cm 0cm, clip, width=2cm]{Star_rating.png}};
\end{tikzpicture}
\end{figure} \newpage



% Exercise 4
% Exercise 4
% Exercise 4
% Exercise 4
% Exercise 4

\section*{Exercise 4: Drawing a flowchart}

\begin{figure}[H]
\centering
\begin{tikzpicture}
    \draw[help lines, dotted] (-5,0) grid (5,13);
    % Draw the "Input" node
    \node (Input) at (0,0.25) {Input};
    % Draw the "Embeddings" node
    \draw[thick, rounded corners, fill=red!25] (-3,1) rectangle (3,2.5);
    % Draw the "Word" + "Position" + "Type" nodes
    \draw[thick, rounded corners, fill=red!50] (-2.75,1.25) rectangle ++(1.5,0.5);
    \draw[thick, rounded corners, fill=red!50] (-0.75,1.25) rectangle ++(1.5,0.5);
    \draw[thick, rounded corners, fill=red!50] (1.25,1.25) rectangle ++(1.5,0.5);
    \node at (0,2.125) {Embeddings};
    \node at (-2,1.5) {Word};
    \node at (0,1.5) {Position};
    \node at (2,1.5) {Type};
    \node at (-1,1.5) {\textbf{+}};
    \node at (1,1.5) {\textbf{+}};
    % Draw the big grey box before its contents
    \draw[thick, fill=black!5, rounded corners] (-3,3) rectangle (3,9);
    \node at (-4,6) {N $\times$};
    % Draw the nodes inside the grey box
    \draw[thick, fill=orange!25, rounded corners] (-2.5,3.25) rectangle ++(5,1.25);
    \node at (0,3.875) {Fourier};
    \draw[thick, fill=yellow!25, rounded corners] (-2.5,4.75) rectangle ++(5,0.75);
    \node at (0,5.125) {Add \& Normalize};
    \draw[thick, fill=cyan!25, rounded corners] (-2.5,6.25) rectangle ++(5,1.25);
    \node at (0,6.875) {Feed Forward};
    \draw[thick, fill=yellow!25, rounded corners] (-2.5,7.75) rectangle ++(5,0.75);
    \node at (0,8.125) {Add \& Normalize};
    % Draw the "Dense" and "Output projection" nodes
    \draw[thick, fill=pink!75!purple, rounded corners] (-3,9.5) rectangle ++(6,0.75);
    \node at (0,9.875) {Dense};
    \draw[thick, fill=green!50, rounded corners] (-3,10.75) rectangle ++(6,0.75);
    \node at (0,11.125) {Output Projection};
    % Draw the "Output" node
    \node at (0,12.25) {Output};
    % Draw the arrows for the graph
    \draw[thick, ->] (Input) -- ++(0,0.75);
    \draw[thick, ->] (0,2.5) -- ++(0,0.75);
    \draw[thick, ->] (0,2.75) -- ++(-2.75,0) -- ++(0,2.375) -- ++(0.25,0);
    \draw[thick, ->] (0,4.5) -- ++(0,0.25);
    \draw[thick, ->] (0,5.5) -- ++(0,0.75);
    \draw[thick, ->] (0,5.75) -- ++(-2.75,0) -- ++(0,2.375) -- ++(0.25,0);
    \draw[thick, ->] (0,7.5) -- ++(0,0.25);
    \draw[thick, ->] (0,9) -- ++(0,0.5);
    \draw[thick, ->] (0,10.25) -- ++(0,0.5);
    \draw[thick, ->] (0,11.5) -- ++(0,0.5);
\end{tikzpicture}
\end{figure} \newpage



% Exercise 5
% Exercise 5
% Exercise 5
% Exercise 5
% Exercise 5

\section*{Exercise 5: Drawing a convolutional neural network}

\begin{figure}[H]
\centering
\hspace*{-3cm}
\begin{tikzpicture}
    \draw[help lines, dotted] (-7,-3) grid (11,3);
    % Put the handwritten 6 on the left side
    \node at (-4,0.25) {\includegraphics[trim=5.175cm 1.2cm 4.5cm 0.625cm, clip, width=2cm]{MNIST_sample.png}};
    \draw[thick, black!25] (-3.625,0.75) rectangle ++(0.5,0.5);
    % Draw the convolution layer
    \foreach \x in {0,1,2,3,4,5,6}{
        \draw[thick, fill=blue!50] ({-1+\x/6},{1.75-\x/6}) rectangle ++(2,-2);
    }
    \draw[thick, black!25] (1.25,0) rectangle ++(0.5,0.5);
    % Draw the pooling layer
    \foreach \x in {0,1,2,3,4}{
        \draw[thick, fill=red!50] ({3.5+\x/4},{1-\x/4}) rectangle ++(1,-1);
    }
    % Draw the input layer for the fully connected layer
    \node[draw, circle, inner sep=0.15cm, fill=green!50] (In1) at (7.5,0.9) {};
    \node[draw, circle, inner sep=0.15cm, fill=green!50] (In2) at (7.5,0.3) {};
    \node[draw, circle, inner sep=0.15cm, fill=green!50] (In3) at (7.5,-0.3) {};
    \node[draw, circle, inner sep=0.15cm, fill=green!50] (In4) at (7.5,-0.9) {};
    % Draw the output layer for the fully connected layer
    \node[draw, circle, inner sep=0.15cm, fill=yellow!50] (Out1) at (9,0.3) {};
    \node[draw, circle, inner sep=0.15cm, fill=yellow!50] (Out2) at (9,-0.3) {};
    % Draw lines from input to conv layer
    \draw[thick, dashed, black!25] (-3.125,1.25) -- (0.5,-0.5);
    \draw[thick, dashed, black!25] (-3.625,0.75) -- (0.5,-0.5);
    % Draw lines from conv layer to pool layer
    \draw[thick, dashed, black!25] (1.25,0) -- (5,-0.5);
    \draw[thick, dashed, black!25] (1.75,0.5) -- (5,-0.5);
    % Draw lines from pooling to fully connected layer
    \draw[thick, dashed, black!25] (4.5,1) -- (7.5,1.05);
    \draw[thick, dashed, black!25] (5.5,-1) -- (7.5,-1.05);
    % Draw lines for the fully connected layer
    \draw[thick, ->] (In1) -- (Out1);
    \draw[thick, ->] (In2) -- (Out1);
    \draw[thick, ->] (In3) -- (Out1);
    \draw[thick, ->] (In4) -- (Out1);
    \draw[thick, ->] (In1) -- (Out2);
    \draw[thick, ->] (In2) -- (Out2);
    \draw[thick, ->] (In3) -- (Out2);
    \draw[thick, ->] (In4) -- (Out2);
    % Label the various layers
    \node at (-5.9,0) {\large Input};
    \node[draw] at (-2,2.25) {\large Convolution};
    \node[draw] at (2.75,2.25) {\large Pooling};
    \node[draw] at (6.25,2.25) {\large Flatten};
    \node at (10.1,0) {\large Output};
    % Label the feature extraction and classification portions of the diagram
    \draw[thick, rounded corners] (-5.75,-1.25) -- ++(0,-0.5) -- ++(12,0) -- ++(0,0.5);
    \node at (0.25,-2) {Feature extraction};
    \draw[thick, rounded corners] (7,-1.25) -- ++(0,-0.5) -- ++(2.5,0) -- ++(0,0.5);
    \node at (8.25,-2) {Classification};
\end{tikzpicture}
\end{figure} \newpage

\end{document}
