如何根据索引值更改 foreach 循环中的索引

如何根据索引值更改 foreach 循环中的索引

我这个例子:

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usepackage    {siunitx} 
\usetikzlibrary{3d}      
\usetikzlibrary{babel}   
\usetikzlibrary{calc}    

% \usepackage{enumitem,setspace,kantlipsum}
% isometric
\pgfmathsetmacro\xx{1/sqrt(2)}
\pgfmathsetmacro\xy{1/sqrt(6)}
\pgfmathsetmacro\zy{sqrt(2/3)}

\begin{document}
\begin{tikzpicture}[x={(\xx cm,-\xy cm)},y={(\xx cm,\xy cm)},z={(0 cm,\zy cm)},% vectores i,j,k
                    thick,line cap=round,line join=round, scale=0.7]

\def\br{1.5}  
\def\be{4}    

\def\we{17.6} 
\def\ws{4}    


\fill[canvas is yz plane at x=-\ws,gray!30] (-1.2*\ws,0)    rectangle (1.2*\ws,\we+1); 
\fill[canvas is xy plane at z=0   ,gray!15] (-\ws,-1.2*\ws) rectangle (7,1.2*\ws); 
\draw[dashed] (0,0,\be) -- (0,0,0) -- (\ws,0,0)  node[midway,sloped, above, xshift=-0.65cm] {\SI{40}{cm}};
\fill[canvas is xy plane at z=0,gray!80] (\ws,0) circle (0.25) node[black,xshift=1.25cm] {}; %
\draw[canvas is xy plane at z=0,cyan, dashed] (\ws,0) circle (2.25) node[black,xshift=0.85cm, align=left] {Boxing\\ Zone};


\draw[fill= red!20] ($(-135:\br)+(0,0,\be+9.6)$) --++ (0,0,-9.6)
    {[canvas is xy plane at z=\be] arc (-135:45:\br)} -- ($(45:\br)+(0,0,\be+9.6)$);

\draw[canvas is xy plane at z=\be+9.6,fill=red!40] (0,0) circle (\br);

\foreach[count=\xi]\i in{1.5,4.2,6.9}
{%                       1.5, 1.5+2.7, 1.5+2.7+2.7
  \begin{scope}[canvas is xy plane at z=\be+\i] 
    \draw[orange] (0,0) circle (\br);
    \fill[orange] (0,0,\be+\i) circle (1pt); 
    \coordinate   (C\xi) at (225:\br+0.2); 
    %--
    \foreach \k in {0,120,240}
    {%
      \begin{scope}[rotate=\k]  
        \draw[gray,dashed] (0,0) -- (\br,0);
        \draw[red, opacity=0.85, fill=blue!75,fill opacity=0.6,] (0.75,-0.12) rectangle (1.1,0.12);
      \end{scope}
    }
    %--
  \end{scope}
}

\foreach\i in {0,90,180,270}
{%
  \draw[line width=0.5mm] ($(\i:\br)+(0,0,\be+9.6)$) -- (0,0,\we-0.15);
}
\draw[line width=0.5mm] (0,-\br,\be+9.6) --++ (0,0,-0.5); 
\draw[line width=0.5mm] (\br, 0,\be+9.6) --++ (0,0,-0.5); 
% Wall structure
\begin{scope}[shift={(-\ws,0,\we)},line width=1.5mm]
  \draw (0.5*\ws ,0,0) -- (0,0,-0.5*\ws) -- (0,0,-\ws); 
  \draw (0,-0.5*\ws,0) -- (0,0.5*\ws,0); 
  \draw (0,0,0)        -- (1.25*\ws,0,0);
 
\end{scope}


\end{tikzpicture}
\end{document}

我想更改绘制的部分的角度 \k:

    %--
    \foreach \k in {0,120,240}
    {%
      \begin{scope}[rotate=\k]  
        \draw[gray,dashed] (0,0) -- (\br,0);
        \draw[red, opacity=0.85, fill=blue!75,fill opacity=0.6,] (0.75,-0.12) rectangle (1.1,0.12);
      \end{scope}
    }
    %--

作为索引为 \i 的 foreach 函数(\foreach[count=\xi]\i in{1.5,4.2,6.9})例如,如果 \i 的值为 1.5(列表的第一个值),则不执行任何操作,但如果值为 4.2 或 6.9,则角度 \k 给出的旋转应变为 60 度、180 度和 300 度。

答案1

简而言之:

\foreach [evaluate={\kk={ifthenelse(\i<4, \k, \k+60)}}] \k in {0,120,240}    
    {       
      \begin{scope}[rotate=\kk]

evaluate让您根据循环变量计算新值。因此,我在这里使用ifthenelse检查 的值\i,并将新变量设置\kk\k\k+60

在此处输入图片描述

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usepackage    {siunitx} 
\usetikzlibrary{3d}      
\usetikzlibrary{babel}   
\usetikzlibrary{calc}    

% \usepackage{enumitem,setspace,kantlipsum}
% isometric
\pgfmathsetmacro\xx{1/sqrt(2)}
\pgfmathsetmacro\xy{1/sqrt(6)}
\pgfmathsetmacro\zy{sqrt(2/3)}

\begin{document}
\begin{tikzpicture}[x={(\xx cm,-\xy cm)},y={(\xx cm,\xy cm)},z={(0 cm,\zy cm)},% vectores i,j,k
                    thick,line cap=round,line join=round, scale=0.7]

\def\br{1.5}  
\def\be{4}    

\def\we{17.6} 
\def\ws{4}    


\fill[canvas is yz plane at x=-\ws,gray!30] (-1.2*\ws,0)    rectangle (1.2*\ws,\we+1); 
\fill[canvas is xy plane at z=0   ,gray!15] (-\ws,-1.2*\ws) rectangle (7,1.2*\ws); 
\draw[dashed] (0,0,\be) -- (0,0,0) -- (\ws,0,0)  node[midway,sloped, above, xshift=-0.65cm] {\SI{40}{cm}};
\fill[canvas is xy plane at z=0,gray!80] (\ws,0) circle (0.25) node[black,xshift=1.25cm] {}; %
\draw[canvas is xy plane at z=0,cyan, dashed] (\ws,0) circle (2.25) node[black,xshift=0.85cm, align=left] {Boxing\\ Zone};


\draw[fill= red!20] ($(-135:\br)+(0,0,\be+9.6)$) --++ (0,0,-9.6)
    {[canvas is xy plane at z=\be] arc (-135:45:\br)} -- ($(45:\br)+(0,0,\be+9.6)$);

\draw[canvas is xy plane at z=\be+9.6,fill=red!40] (0,0) circle (\br);

\foreach[count=\xi]\i in{1.5,4.2,6.9}
{%                       1.5, 1.5+2.7, 1.5+2.7+2.7
  \begin{scope}[canvas is xy plane at z=\be+\i] 
    \draw[orange] (0,0) circle (\br);
    \fill[orange] (0,0,\be+\i) circle (1pt); 
    \coordinate   (C\xi) at (225:\br+0.2); 
    %--
\foreach [evaluate={\kk={ifthenelse(\i<4, \k, \k+60)}}] \k in {0,120,240}    
    {       
      \begin{scope}[rotate=\kk]
        \draw[gray,dashed] (0,0) -- (\br,0);
        \draw[red, opacity=0.85, fill=blue!75,fill opacity=0.6,] (0.75,-0.12) rectangle (1.1,0.12);
      \end{scope}
    }
    %--
  \end{scope}
}

\foreach\i in {0,90,180,270}
{%
  \draw[line width=0.5mm] ($(\i:\br)+(0,0,\be+9.6)$) -- (0,0,\we-0.15);
}
\draw[line width=0.5mm] (0,-\br,\be+9.6) --++ (0,0,-0.5); 
\draw[line width=0.5mm] (\br, 0,\be+9.6) --++ (0,0,-0.5); 
% Wall structure
\begin{scope}[shift={(-\ws,0,\we)},line width=1.5mm]
  \draw (0.5*\ws ,0,0) -- (0,0,-0.5*\ws) -- (0,0,-\ws); 
  \draw (0,-0.5*\ws,0) -- (0,0.5*\ws,0); 
  \draw (0,0,0)        -- (1.25*\ws,0,0);
 
\end{scope}


\end{tikzpicture}
\end{document}

相关内容