简化 \foreach 以重复绘图

简化 \foreach 以重复绘图

我使用以下代码来重复绘图。可以简化吗?

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25,3.5,3.75,4,4.25}
  \draw<2-4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75,4.25}
  \draw<2> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25,3.5,3.75,4,4.25}
  \draw<3,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {.75}
  \foreach \x in {4.25}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75}
  \draw<2,3> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
  \foreach \x in {4.25}
  \draw<2,4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
  \foreach \x in {.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25,3.5,3.75,4,4.25}
  \draw<2-4> (\x,0) node [circle, draw, very thick, green!40!black, fill=green, scale=0.6]{};
\end{tikzpicture}

\vspace {-.18cm}

\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5}
  \draw (\x,0) node [circle, draw, brown, fill=yellow, scale=0.5]{};
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

答案1

非常简单的建议:只使用一个 TikZpicture 并且只绘制需要的部分,考虑到不同阶段中某些节点的重复。

\documentclass[svgnames]{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz} 
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}
  \foreach \x in {0,.25,.5,.75,1,...,5} {
        \foreach \y in {0,.25,.5,.75,1,...,4} {
            \fill[draw=brown, fill=yellow] (\x,-\y) circle (3pt);
      }
    }
    \only<2-4>{
    \foreach \x in {.75,1,1.25,...,4.25} {
        \fill[draw=DarkGreen, fill=green] (\x,-.25) circle (3pt);
        \fill[draw=DarkGreen, fill=green] (\x,-3.75) circle (3pt);
    }
    }
    \only<2,4> {
    \foreach \x in {-.5,-.75,-1,...,-2} {
        \pgfmathsetmacro\y{\x-1.75}
        \fill[draw=DarkGreen, fill=green] (.75,\x) circle (3pt);
        \fill[draw=DarkGreen, fill=green] (4.25,\y) circle (3pt);
    }
    }
    \only<2-3> {
    \foreach \x in {-.5,-.75,-1,...,-2} {
        \pgfmathsetmacro\y{\x-1.75}
        \fill[draw=DarkGreen, fill=green] (4.25,\x) circle (3pt);
        \fill[draw=DarkGreen, fill=green] (.75,\y) circle (3pt);
    }
    }
    \only<3-4> {
    \foreach \x in {.75,1,1.25,...,4.25} {
        \fill[draw=DarkGreen, fill=green] (\x,-2) circle (3pt);
    }
    }
\end{tikzpicture}
\end{frame}
\end{document}

答案2

你可以作弊:

  1. 填充一个矩形。
  2. 绘制路径。
  3. 除了矩形圆圈之外的所有部分都填充白色。

代码:

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz} 
\begin{document}
\begin{frame}[fragile,t]
\frametitle{Some dotted path}
\only<1>{Fill a rectangle}
\only<2>{Draw the path}
\only<3>{Fill everything but the circles white}

\begin{tikzpicture}
\clip (-4pt,-4pt) rectangle (25.5*8pt,25.5*8pt);
\fill[yellow] (-4pt,-4pt) rectangle (25.5*8pt,25.5*8pt);
\pause
\draw[line width=7pt,green!40!black] (3*8pt,1*8pt) rectangle (22*8pt,24*8pt);
\pause
\draw[brown,fill=white,even odd rule] 
(-5pt,-5pt) rectangle (25.5*8pt+1pt,25.5*8pt+1pt)
foreach \X in {0,...,25}
{foreach \Y in {0,...,25}
{ (\X*8pt,\Y*8pt) circle (3.5pt)
}
};
\end{tikzpicture}
\end{frame}

\begin{frame}[fragile,t]
\frametitle{Some dotted path (cont'd)}

\begin{tikzpicture}
\clip (-4pt,-4pt) rectangle (25.5*8pt,25.5*8pt);
\fill[yellow] (-4pt,-4pt) rectangle (25.5*8pt,25.5*8pt);
\draw[line width=7pt,green!40!black,line cap=round] (3*8pt,24*8pt) 
 --  (22*8pt,24*8pt)  -- (22*8pt,13*8pt)
 -- (3*8pt,13*8pt) -- (3*8pt,1*8pt) -- (22*8pt,1*8pt);
\draw[brown,fill=white,even odd rule] 
(-5pt,-5pt) rectangle (25.5*8pt+1pt,25.5*8pt+1pt)
foreach \X in {0,...,25}
{foreach \Y in {0,...,25}
{ (\X*8pt,\Y*8pt) circle (3.5pt)
}
};
\end{tikzpicture}
\end{frame}

\begin{frame}[fragile,t]
\frametitle{Some dotted path (cont'd)}

\begin{tikzpicture}[xscale=-1]
\clip (-4pt,-4pt) rectangle (25.5*8pt,25.5*8pt);
\fill[yellow] (-4pt,-4pt) rectangle (25.5*8pt,25.5*8pt);
\draw[line width=7pt,green!40!black,line cap=round] (3*8pt,24*8pt) 
 --  (22*8pt,24*8pt)  -- (22*8pt,13*8pt)
 -- (3*8pt,13*8pt) -- (3*8pt,1*8pt) -- (22*8pt,1*8pt);
\draw[brown,fill=white,even odd rule] 
(-5pt,-5pt) rectangle (25.5*8pt+1pt,25.5*8pt+1pt)
foreach \X in {0,...,25}
{foreach \Y in {0,...,25}
{ (\X*8pt,\Y*8pt) circle (3.5pt)
}
};
\end{tikzpicture}
\end{frame}
\end{document}

结果:

在此处输入图片描述

相关内容