2

From the answers to these questions 1, 2, and 3:

Is it possible to combine the 3 versions of the Spirograph codes:

the simple one including controlling nRotations, colours, and scales;

the one controlling segmenting the drawing (with the added \pv{dx});

and the one controlling the filling of the drawing;

into a one unified code that could be used to control all these variables, when needed, in all these different situations.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{verbatim}
\begin{document}
% ====================== begin spiro setting ======================
\tikzset{pics/spiro/.style={code={
\tikzset{spiro/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}} 
\draw[trig format=rad,pic actions]
plot[variable=\t,domain=0:2*pi*\pv{nRotations}, samples=90*\pv{nRotations}+1, smooth cycle]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spiro/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1,nRotations/.initial=1}
% ====================== end spiro setting ======================
% ============== begin spirofill setting ===================
\tikzset{pics/spirofill/.style={code={
\tikzset{spirofill/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spirofill/##1}} 
\draw[trig format=rad,pic actions]
 plot[variable=\t,domain=pi/2:3*pi/2, samples=31, smooth]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spirofill path/.code={\def\pv##1{\pgfkeysvalueof{/tikz/spirofill/##1}}
\tikzset{insert path={
plot[trig format=rad,variable=\t,domain=pi/2:3*pi/2, samples=31, smooth, domain=pi:pi/2]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
) 
|- (\pv{R}+\pv{r}+\pv{p},-\pv{R}-\pv{r}-\pv{p})
-- 
(\pv{R}+\pv{r}+\pv{p},\pv{R}+\pv{r}+\pv{p})  --
(-\pv{R}-\pv{r}-\pv{p},\pv{R}+\pv{r}+\pv{p})
--  cycle
}}},
spirofill/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1}
% ============== end spirofill setting ===================
% ====================== begin spirosegment setting ======================
\tikzset{pics/spirosegment/.style={code={
\tikzset{spirosegment/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spirosegment/##1}} 
\pgfmathparse{(int(1/\pv{dx}+1)}
\tikzset{spirosegment/samples=\pgfmathresult}
\draw[trig format=rad,pic actions] 
plot[variable=\t,domain=\pv{xmin}-0.002:\pv{xmax}+0.002, samples=\pv{samples}, smooth]
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
 {(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spirosegment/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1, dx/.initial=0.05,samples/.initial=21,domain/.code args={#1:#2}{%
\pgfmathparse{#1}\tikzset{spirosegment/xmin/.expanded=\pgfmathresult}
\pgfmathparse{#2}\tikzset{spirosegment/xmax/.expanded=\pgfmathresult}},
xmin/.initial=0,xmax/.initial=2*pi}
% ====================== end spirosegment setting ======================
\begin{frame}[t]
\frametitle{Spiro,nRot5}
\begin{center}
\vskip -.6cm
\begin{tikzpicture}[line width=.2mm]
\foreach \scl/\clr/\p in 
{.8/blue/3.0,.4/purple/7.0} 
{
(0,0) \pic[draw=\clr,rotate=360/4/2*1,scale=\scl]{spiro={R=9.6,r=-12,p=\p,nRotations=5}};
}
\end{tikzpicture} 
\end{center} 
\end{frame}
\begin{frame}[fragile,t]%this frame needs fragile
\frametitle{spirosegment}
\begin{tikzpicture}[line width=.8mm]
\path (0,0)  
foreach \X [count=\Y starting from 0] in {blue,red,green,orange}
{pic[scale=0.5,draw=\X]{spirosegment={domain={-pi/4+\Y*pi/2}:{pi/4+\Y*pi/2}}}};%,dx=0.005
\end{tikzpicture}
\end{frame}
\begin{frame}[t]
\frametitle{spirofill with color interpolation}
\begin{center}
\begin{tikzpicture}[line width=.2mm,spirofill/.cd,R=10.5,r=-5.25,p=3]
\foreach\Z in {0,1}
{\foreach \clr [count=\X starting from 0, remember=\clr as \lastclr (initially purple)] in {blue,green,orange,purple}
{\foreach \Y [evaluate=\Y as \mycf using {int(100*\Y/9)}] in {1,...,9}
{\ifnum\Z\X\Y=102
 \clip[scale=.4,rotate=5,spirofill path];
\fi
\pic[draw=\clr!\mycf!\lastclr,rotate=45*\X+5*\Y+\Z*180,scale=.4,fill=\clr!\mycf!\lastclr!40]{spirofill};
}}}
\end{tikzpicture} 
\end{center} 
\end{frame}
\end{document}

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

Hany
  • 4,709
  • I think it will be very hard to do that since there are some operations that are specific to each of the constructions. –  Jan 28 '20 at 05:00
  • @ Schrödinger's cat Is it possible to make the variables global, I mean not specific to that particular operation, and then use the needed ones according to the situation! – Hany Jan 28 '20 at 05:05
  • Yes, this is possible. However, what is specific to the solutions is the clip path that protects earlier parts. Otherwise it is rather easy to unify these things, in particular if you list all the feature it should have. However, I do not think that the clip path can easily be made very general. –  Jan 28 '20 at 05:29
  • @ Schrödinger's cat Thank you for your comment. May I bother you to do that. I mean making a unified code for these situations. controlling nRotations, colours, and scales; controlling segmenting the drawing (including \pv{xmin}:\pv{xmax}); controlling \pv{dx}; and controlling filling. Thank you. – Hany Jan 28 '20 at 05:46
  • @ Schrödinger's cat The spirosegment second code ustilizes 2pi (one full rotation). How to apply it for patterns which needs 5 rotations (nRotations=5 in the first spiro code). For example spiro={R=9.6,r=-3,p=2,nRotations=5}}; which produces a 16-sides pattern. Thank you. – Hany Jan 30 '20 at 08:10
  • Can you please provide a clear and comprehensive post that describes in detail what the requirements are. The reason why there are three different versions doing similar things is that each of them was written to meett the requirements in the respective question. As of now, you have a question plus some additional comments. If you want a unified code, the first step is to have a simple unified question that is understandable and unambiguous. –  Jan 30 '20 at 16:30
  • @ Schrödinger's cat I posted the following question https://tex.stackexchange.com/questions/526386/modifying-a-spirograph-code explaining my need. – Hany Jan 31 '20 at 06:10

0 Answers0