考虑以下 MWE:
\documentclass[border=5pt,tikz]{standalone}
\begin{document}
\foreach \n in {2,3,...,20}
{
\begin{tikzpicture}
\useasboundingbox (-6,-4) rectangle (2,1);
\pgfmathsetmacro{\number}{2*\n}
\node at (1.5,.5) {$n = \pgfmathprintnumber{\number}$};
\pgfmathsetmacro{\samples}{360/\n}
\pgfmathsetmacro{\sampleslimit}{360-\samples}
\pgfmathsetmacro{\pii}{3.14}
\pgfmathsetmacro{\limit}{360/\samples-1}
\pgfmathsetmacro{\current}{(\pii/\n)*\limit}
\node[left] at (-1.5,.5) {\tiny
\begin{tabular}{lll}
$A_{\mathrm{real}}$ &=& 3.14 \\
$A_{\mathrm{current}}$ &=& \pgfmathprintnumber{\current}
\end{tabular}
};
\foreach \x in {0,\samples,...,\sampleslimit}
{
\pgfmathsetmacro{\arcangle}{\samples/2}
\fill[red] (\x:1) arc(\x:\x+\arcangle:1) -- (0,0) -- cycle;
\fill[blue] (\x+\arcangle:1) arc(\x+\arcangle:\x+2*\arcangle:1) -- (0,0) -- cycle;
}
\pgfmathsetmacro{\limit}{360/\samples-1}
\foreach \x in {0,1,...,\limit}
{
\pgfmathsetmacro{\shift}{\x*2*sin(\samples/4)}
\pgfmathsetmacro{\hshift}{\limit*sin(\samples/4)}
\fill[xshift=-\hshift cm,red,xshift=\shift cm,yshift=-1.5cm] (-90-\samples/4:1) arc(-90-\samples/4:-90+\samples/4:1) -- (0,0) -- cycle;
\fill[blue,yshift=-1.5cm,xshift=-\hshift cm,xshift=\shift cm] (-90-\samples/4:1) -- (0,0) arc(90-\samples/4:90+\samples/4:1) -- cycle;
}
\draw[xshift=-6cm,yshift=-4cm,<->] (0,3.5) -- (0,0) -- (4,0);
\pgfmathsetmacro{\newnumber}{\n/5}
\node[xshift=-6cm,yshift=-4cm,fill=green,circle,inner sep=1pt] at (\newnumber,\current) {};
\draw[xshift=-6cm,yshift=-4cm,red,dashed] (0,\pii) --+ (3.7,0) node[above left] {$A_\mathrm{real} = \pi$};
\end{tikzpicture}
}
\end{document}
输出如下:
我的问题是:绿色节点如何记住它之前的位置,以便所需的结果看起来像一条绿色虚线?
答案1
只需依次建立一个列表即可。
\documentclass[border=5pt,tikz]{standalone}
\begin{document}
\foreach \n in {2,3,...,20}
{
\begin{tikzpicture}
\useasboundingbox (-6,-4) rectangle (2,1);
\pgfmathsetmacro{\number}{2*\n}
\node at (1.5,.5) {$n = \pgfmathprintnumber{\number}$};
\pgfmathsetmacro{\samples}{360/\n}
\pgfmathsetmacro{\sampleslimit}{360-\samples}
\pgfmathsetmacro{\pii}{3.14}
\pgfmathsetmacro{\limit}{360/\samples-1}
\pgfmathsetmacro{\current}{(\pii/\n)*\limit}
\node[left] at (-1.5,.5) {\tiny
\begin{tabular}{lll}
$A_{\mathrm{real}}$ &=& 3.14 \\
$A_{\mathrm{current}}$ &=& \pgfmathprintnumber{\current}
\end{tabular}
};
\foreach \x in {0,\samples,...,\sampleslimit}
{
\pgfmathsetmacro{\arcangle}{\samples/2}
\fill[red] (\x:1) arc(\x:\x+\arcangle:1) -- (0,0) -- cycle;
\fill[blue] (\x+\arcangle:1) arc(\x+\arcangle:\x+2*\arcangle:1) -- (0,0) -- cycle;
}
\pgfmathsetmacro{\limit}{360/\samples-1}
\foreach \x in {0,1,...,\limit}
{
\pgfmathsetmacro{\shift}{\x*2*sin(\samples/4)}
\pgfmathsetmacro{\hshift}{\limit*sin(\samples/4)}
\fill[xshift=-\hshift cm,red,xshift=\shift cm,yshift=-1.5cm] (-90-\samples/4:1) arc(-90-\samples/4:-90+\samples/4:1) -- (0,0) -- cycle;
\fill[blue,yshift=-1.5cm,xshift=-\hshift cm,xshift=\shift cm] (-90-\samples/4:1) -- (0,0) arc(90-\samples/4:90+\samples/4:1) -- cycle;
}
\draw[xshift=-6cm,yshift=-4cm,<->] (0,3.5) -- (0,0) -- (4,0);
\pgfmathsetmacro{\newnumber}{\n/5}
\ifnum\n=2
\xdef\Lst{\newnumber/\current}
\else
\xdef\Lst{\Lst,\newnumber/\current}
\fi
\foreach \X/\Y in \Lst
{\node[xshift=-6cm,yshift=-4cm,fill=green,circle,inner sep=1pt] at
(\X,\Y) {};}
\draw[xshift=-6cm,yshift=-4cm,red,dashed] (0,\pii) --+ (3.7,0) node[above left] {$A_\mathrm{real} = \pi$};
\end{tikzpicture}
}
\end{document}