在下面的代码中,循环中的最后一行(在代码的末尾)似乎没有执行它应该做的事情:分配\angB
给\angA
:
\begin{tikzpicture}[scale=1]
\coordinate (o) at (0,0);
\draw[thick] (o) circle(1);
\path (o)+(270:1) coordinate (p1);
\path (o)+(90:1.2) coordinate (p2);
\draw[dotted] (p2) -- (p1);
\path (o)+(270:1) coordinate (q1);
\path (o)+(30:1) coordinate (q2);
\path (o)+(150:1) coordinate (q3);
\draw[] (q1) -- (q2);
\draw[] (q2) -- (q3);
\draw[] (q3) -- (q1);
\path (q1)+(90:0.8) coordinate (z);
\draw[->] (z) arc (90:60:0.8);
\path[above right] (z) node{$\theta$};
\path[below] (q1) node{$\theta=30^\circ$};
\begin{scope}[shift={(3,0)}]
\coordinate (o) at (0,0);
\draw[thick] (o) circle(1);
\path (o)+(270:1) coordinate (q1);
\pgfmathtruncatemacro{\angA}{270}
\pgfmathtruncatemacro{\t}{25}
\foreach \n in {0,...,100}
{
\pgfmathtruncatemacro{\angB}{\angA+180-2*\t}
\path (o)+({\angA}:1) coordinate (q1);
\path (o)+({\angB}:1) coordinate (q2);
\draw[] (q1) -- (q2);
\pgfmathtruncatemacro{\angA}{\angB}
}
\path[below] (q1) node{$\theta=\t^\circ$};
\end{scope}
\end{tikzpicture}
编辑是否存在某种“步骤变量范围”?我注意到,q1
尽管在循环中对其进行了大量重新定义,但它仍保持了其值。
答案1
常见的问题:每次循环都是一组执行,所以忘记了\foreach
的设置。\angA
使其成为全局的(确保使用不会干扰其他任何内容的命令名称)。
\documentclass{article}
\usepackage{tikz}
\newcommand{\pgfmathtruncategmacro}[2]{%
\pgfmathtruncatemacro\pgfmathresult{#2}%
\global\let#1\pgfmathresult
}
\begin{document}
\begin{tikzpicture}[scale=1]
\coordinate (o) at (0,0);
\draw[thick] (o) circle(1);
\path (o)+(270:1) coordinate (p1);
\path (o)+(90:1.2) coordinate (p2);
\draw[dotted] (p2) -- (p1);
\path (o)+(270:1) coordinate (q1);
\path (o)+(30:1) coordinate (q2);
\path (o)+(150:1) coordinate (q3);
\draw[] (q1) -- (q2);
\draw[] (q2) -- (q3);
\draw[] (q3) -- (q1);
\path (q1)+(90:0.8) coordinate (z);
\draw[->] (z) arc (90:60:0.8);
\path[above right] (z) node{$\theta$};
\path[below] (q1) node{$\theta=30^\circ$};
\begin{scope}[shift={(3,0)}]
\coordinate (o) at (0,0);
\draw[thick] (o) circle(1);
\path (o)+(270:1) coordinate (q1);
\pgfmathtruncategmacro{\angA}{270}
\pgfmathtruncatemacro{\t}{25}
\foreach \n [remember=\n as \prevn (initially -1)] in {0,...,100}
{
\pgfmathtruncatemacro{\angB}{\angA+180-2*\t}
\path (o)+({\angA}:1) coordinate (q\prevn);
\path (o)+({\angB}:1) coordinate (q\n);
\draw[] (q\prevn) -- (q\n);
\pgfmathtruncategmacro{\angA}{\angB}
}
\path[below] (q-1) node{$\theta=\t^\circ$};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
不需要全局宏。pgffor
有用remember
于此目的的关键。
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1]
\begin{scope}[local bounding box=L]
\coordinate (o) at (0,0);
\draw[thick] (o) circle(1);
\path (o)+(270:1) coordinate (p1);
\path (o)+(90:1.2) coordinate (p2);
\draw[dotted] (p2) -- (p1);
\path (o)+(270:1) coordinate (q1);
\path (o)+(30:1) coordinate (q2);
\path (o)+(150:1) coordinate (q3);
\draw[] (q1) -- (q2);
\draw[] (q2) -- (q3);
\draw[] (q3) -- (q1);
\path (q1)+(90:0.8) coordinate (z);
\draw[->] (z) arc (90:60:0.8);
\path[above right] (z) node{$\theta$};
\path (L.south) node[below]{$\theta=30^\circ$};
\end{scope}
%
\begin{scope}[shift={(3,0)},local bounding box=R]
\coordinate (o) at (0,0);
\draw[thick] (o) circle(1);
\path (o)+(270:1) coordinate (q1);
\pgfmathtruncatemacro{\angA}{270}
\pgfmathtruncatemacro{\t}{25}
\foreach \n [remember=\angA as \angA] in {0,...,100}
{
\pgfmathtruncatemacro{\angB}{\angA+180-2*\t}
\path (o)+({\angA}:1) coordinate (q1);
\path (o)+({\angB}:1) coordinate (q2);
\draw (q1) -- (q2);
\pgfmathtruncatemacro{\angA}{\angB}
}
\path (R.south) node[below]{$\theta=\t^\circ$};
\end{scope}
\end{tikzpicture}
\end{document}