# Recreating a guilloche with TikZ

One of my last questions talked about certificates and I got great answers. Now my questions is, can one recreate a guilloche in TikZ like the one shown below:

or may be a border like

-
Maybe this will help? tex.stackexchange.com/questions/41159/… – mbork Mar 2 '12 at 15:19
– Ben Lerner Mar 2 '12 at 15:50
I know this is an old question, but for reference I thought I'd add this, since I stumbled across it. It might be possible to design a desirable guilloche with this subblue.com/projects/guilloche and then utilising their provided source code (extract the equations), plug parameters from your drawing to re-create the image with TikZ – EricR Jun 4 '12 at 19:02

## 2 Answers

Guilloché (Guilloche) is a decorative engraving technique in which a very precise intricate repetitive pattern or design is mechanically engraved into an underlying material with fine detail. Specifically, it involves a technique of engine turning, called guilloché in French after the French engineer “Guillot”, who invented a machine “that could scratch fine patterns and designs on metallic surfaces

The problem is to draw a curve with a fixed point inside a circle

A method : I used tkz-fct because it's my package and I know it but it's easy to create the same thing with only tikz. (here you need gnuplot)

\documentclass[11pt]{scrartcl}
\usepackage[dvipsnames]{xcolor}
\usepackage{tkz-fct}

\begin{document}

\def\spirographlike#1{%
\def\repeatno{#1}
\foreach \i in {1,...,\repeatno}
{%
\begin{scope}[rotate=360/\repeatno*\i]
\mycloedcurve
\end{scope}}%
}

\def\mycloedcurve{\tkzFctPolar[color=MidnightBlue,thick,domain=0:2*pi,samples=400]{ 1+cos(3*t)+(sin(3*t))**2}}

\noindent\begin{tikzpicture}
\mycloedcurve
\end{tikzpicture}
\begin{tikzpicture}[scale=2]
\tkzInit [xmin=-5,xmax=5,ymin=-5,ymax=5]
\spirographlike{40}
\end{tikzpicture}

\end{document}


update

This is better but you need some time to compile

\documentclass[11pt]{scrartcl}
\usepackage[dvipsnames]{xcolor}
\usepackage{tkz-fct}

\begin{document}

\def\spirographlike#1{%
\def\repeatno{#1}
\foreach \i in {1,...,\repeatno}
{%
\begin{scope}[rotate around={360/\repeatno*\i:(.5,0)}]
\mycloedcurve
\end{scope}}%
}

\def\mycloedcurve{\tkzFctPolar[color=MidnightBlue,thick,domain=0:2*pi,samples=400]{ 1+cos(3*t)+(sin(3*t))**2}}

\begin{tikzpicture}
\mycloedcurve
\end{tikzpicture}
\begin{tikzpicture}[scale=2]
\tkzInit [xmin=-5,xmax=5,ymin=-5,ymax=5]
\spirographlike{20}
\end{tikzpicture}

\end{document}

-
Sweet sweet sweet.....love TikZ.....love it <3. – azetina Mar 2 '12 at 16:53
Adding \noindent before \begin{tikzpicture} eliminates the overfull \hbox warning. – Peter Grill Mar 2 '12 at 17:12
Thanks Peter I forget always these kind of problem, because I'm obsessed with the main question. Possible \usepackage{fullpage} – Alain Matthes Mar 2 '12 at 17:31
Very nice. This is getting closer and closer to the Spirograph question. – percusse Mar 2 '12 at 17:43
@percusse Can you provide any insights in generating the border like guilloche? Or may be with your knowledge on your post quoted above can the image sample I provided be recreated? – azetina Mar 2 '12 at 17:49

So here's an approach to draw the 'straight' parts. (Might be a bit crude to more seasoned people...)

The compile time is certain to put the most patient of us to test, but that's to be expected with TikZ doing such things... :)

I only plotted one such sample from the ones I found at Mathworld. So there are some samples ready to be downloaded. They are (as you can see below) a sum of lots of sines and the essential trick to get them to mesh together beautifully is to control the 'frequency' and the 'initial phase'.

So here's the code:

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[very thin]

\foreach \n in {0,...,19}
{
\foreach \x [remember=\x as \lastx (initially 0)] in {0.01,0.02,...,6.28}
{
\draw [red] (\lastx*2,{(4+sin(5*(180*\lastx/pi)))+((7+sin(7*(180*\lastx/pi)))-(4+sin(5*(180*\lastx/pi))))*(1+sin(5*(180*\lastx/pi)+\n*20))/2}) -- (\x*2,{(4+sin(5*(180*\x/pi)))+((7+sin(7*(180*\x/pi)))-(4+sin(5*(180*\x/pi))))*(1+sin(5*(180*\x/pi)+\n*20))/2});
}
}

\end{tikzpicture}

\end{document}


As you can see, I had to rescale a bit the coordinates, primarily because the source formulas used radians and TikZ likes degrees... Also I stretched a bit the thing along the x axis for looks.

And this is what you get after - well - a while:

## Faster Code:

The idea is to reuse the previous coordinate instead of recomputing it with the use of \lastx. Further this means that there will only be {0,...,19} draw commands instead of 20\times6.29/0.01=12580.
This feature is used by doing an inline for-loop.

So instead you would do:

\begin{tikzpicture}[very thin]

\foreach \n in {0,...,19} {
\draw [red] (0,{(4+sin(5*(0)))+((7+sin(7*(0)))-(4+sin(5*(0))))*(1+sin(5*(0)+\n*20))/2})
\foreach \x in {0.01,0.02,...,6.28} {
-- (\x*2,{(4+sin(5*(180*\x/pi)))+((7+sin(7*(180*\x/pi)))-(4+sin(5*(180*\x/pi))))*(1+sin(5*(180*\x/pi)+\n*20))/2})
}; % <- Here the \draw ends
}
\end{tikzpicture}


For further speed the computation of the angles could be made more explicit, and shorter expression could be made:

\begin{tikzpicture}[very thin]
\foreach \n in {0,...,19} {
\draw [red] (0,{4+(7-4)*(1+sin(\n*20))/2})
\foreach \x in {0.5,1,...,360} {
-- ({6.28318*\x/180},{4+sin(5*\x)+(7+sin(7*\x)-(4+sin(5*\x)))*(1+sin(5*\x+\n*20))/2})
};
}
\end{tikzpicture}


As a last optimization all variables that can be reduced should be, this will not give as much, as it is simple multiplication and division:

\begin{tikzpicture}[very thin]
\foreach \n [evaluate={\n*20} as \ntwenty] in {0,...,19} {
\draw [red] (0,{5.5+1.5*sin(\ntwenty))})
\foreach \x [evaluate={sin(5*\x)} as \sfx] in {0.5,1,...,360} {
-- ({0.034906585039886591*\x},{4+\sfx+(3+sin(7*\x)-\sfx)*(1+sin(5*\x+\ntwenty))/2})
};
}
\end{tikzpicture}


And actually it does compiler twice as fast, and faster for the last, when changing from evaluate to \pgfextra\pgfmathparse{sin(5*\x)}\edef\sfx{\pgfmathresult}\endpgfextra there is no gain, so is not showed:

1. 1m59.699s
2. 0m59.100s
3. 0m43.627s
4. 0m38.714s
-
Excellent results. I suppose one can make it into border like with the above. Though the compilation may take long one can export it as a pdf and then import it as a background. – azetina Mar 3 '12 at 1:02
Very nice, the compilation time will be speed up a lot by not calculating every point twice, so instead do: \draw [red] (\lastx*2,{(4+sin(5*(180*\lastx/pi)))+((7+sin(7*(180*\lastx/pi)))-(4+sin(5*(180*‌​\lastx/pi))))*(1+sin(5*(180*\lastx/pi)+\n*20))/2}) \foreach \x in {0.01,0.02,...,6.28} { -- (\x*2,{(4+sin(5*(180*\x/pi)))+((7+sin(7*(180*\x/pi)))-(4+sin(5*(180*\x/pi))))*(1‌​+sin(5*(180*\x/pi)+\n*20))/2}) }; also the 180*\x/pi could be automated in the for loop instead! – zeroth Mar 5 '12 at 8:33
@zeroth: I'm sorry, I don't really get it... Where do I insert that line of code? What should I do with \lastx? I'd rather you edited my answer (if it's too much for a comment). – Count Zero Mar 5 '12 at 22:52
@CountZero I have edited your answer with two speedups! :) – zeroth Mar 6 '12 at 7:14
@CountZero and one more, which only is to illustrate that the fewest repetitive computations is the best! :) – zeroth Mar 6 '12 at 11:53