在 foreach 中协调 [turn] 选项

在 foreach 中协调 [turn] 选项

我正在尝试用 - 重新创建一个三角形螺旋(红色),但foreachturn选项表现不佳 - 每次似乎旋转得越来越少。有什么解释吗?我做错了什么?

这里我使用90度数、角度和长度1来说明:

\documentclass[tikz, border=1 cm]{standalone}
\begin{document}
\begin{tikzpicture}
\newcommand{\x}{0.872}
\draw[red] (0,0) -- (1,0)
  -- ([turn]125:\x^1)
  -- ([turn]125:\x^2) 
  -- ([turn]125:\x^3) 
  -- ([turn]125:\x^4) 
  -- ([turn]125:\x^5) 
  -- ([turn]125:\x^6) 
  -- ([turn]125:\x^7) 
  -- ([turn]125:\x^8) 
  -- ([turn]125:\x^9) 
  -- ([turn]125:\x^10);
\draw (0,0) -- (1,0) foreach \i in {1,...,50} {-- ([turn]90:1)};
\end{tikzpicture}
\end{document}

红色三角形螺旋和大黑色螺旋

编辑:

\draw (0,0) -- (1,0) foreach \i in {1,...,10} { -- ([turn]125:\x^\i)};得到:

不同的红色和黑色螺旋

答案1

我同意Black Mild 在评论中这似乎是一个tikz错误,并已在此处报告:https://github.com/pgf-tikz/pgf/issues/1047

可能的解决方法:

\documentclass{article}
\usepackage{tikz}

\makeatletter
% see https://github.com/pgf-tikz/pgf/blob/85d2c38b78407e5b1ba42140dfd11fcdf8df1edf/tex/generic/pgf/frontendlayer/tikz/tikz.code.tex#L2582-L2616
\def\tikz@foreach{%
  \def\pgffor@beginhook{%
    \tikz@lastx=\tikz@foreach@save@lastx%
    \tikz@lasty=\tikz@foreach@save@lasty%
    \tikz@lastxsaved=\tikz@foreach@save@lastxsaved%
    \tikz@lastysaved=\tikz@foreach@save@lastysaved%
    \let\tikz@tangent=\tikz@foreach@save@tangent % added
    \setbox\tikz@figbox=\box\tikz@tempbox%
    \setbox\tikz@figbox@bg=\box\tikz@tempbox@bg%
    \expandafter\tikz@scan@next@command\pgfutil@firstofone}%
  \def\pgffor@endhook{\pgfextra{%
      \xdef\tikz@foreach@save@lastx{\the\tikz@lastx}%
      \xdef\tikz@foreach@save@lasty{\the\tikz@lasty}%
      \xdef\tikz@foreach@save@lastxsaved{\the\tikz@lastxsaved}%
      \xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
      \global\let\tikz@foreach@save@tangent=\tikz@tangent % added
      \global\setbox\tikz@tempbox=\box\tikz@figbox%
      \global\setbox\tikz@tempbox@bg=\box\tikz@figbox@bg%
      \pgfutil@gobble}}%
  \def\pgffor@afterhook{%
    \tikz@lastx=\tikz@foreach@save@lastx%
    \tikz@lasty=\tikz@foreach@save@lasty%
    \tikz@lastxsaved=\tikz@foreach@save@lastxsaved%
    \tikz@lastysaved=\tikz@foreach@save@lastysaved%
    \let\tikz@tangent=\tikz@foreach@save@tangent % added
    \let\pgffor@beginhook\relax%
    \let\pgffor@endhook\relax%
    \let\pgffor@afterhook\relax%
    \setbox\tikz@figbox=\box\tikz@tempbox%
    \setbox\tikz@figbox@bg=\box\tikz@tempbox@bg%
    \tikz@scan@next@command}%
  \global\setbox\tikz@tempbox=\box\tikz@figbox%
  \global\setbox\tikz@tempbox@bg=\box\tikz@figbox@bg%
  \global\let\tikz@foreach@save@tangent=\tikz@tangent % added
  \xdef\tikz@foreach@save@lastx{\the\tikz@lastx}%
  \xdef\tikz@foreach@save@lasty{\the\tikz@lasty}%
  \xdef\tikz@foreach@save@lastxsaved{\the\tikz@lastxsaved}%
  \xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
  \foreach}%
\makeatother

\begin{document}
\newcommand{\x}{0.872}

\begin{tikzpicture}
  \draw (0,0) -- (1,0) foreach \i in {1,...,50} {-- ([turn]90:1)};
  
  \draw[xshift=2cm] (0,0) -- (1,0) foreach \i in {1,...,10} { -- ([turn]125:\x^\i)};
\end{tikzpicture}
\end{document}

输出: 在此处输入图片描述


为了节省一些线路,

\usepackage{regexpatch}

\makeatletter
\xpatchcmd*\tikz@foreach
  {\tikz@lastysaved=\tikz@foreach@save@lastysaved}
  {\tikz@lastysaved=\tikz@foreach@save@lastysaved
   \let\tikz@tangent=\tikz@foreach@save@tangent}
  {}{\PatchFailed}

\xpatchcmd*\tikz@foreach
  {\xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}}
  {\xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
   \global\let\tikz@foreach@save@tangent=\tikz@tangent}
  {}{\PatchFailed}
\makeatother

答案2

似乎foreach与 配合得不太好[turn]。我找不到解决您问题的方法。无论如何,我建议您只使用强大的命令来执行此操作。也许像这样:

每一行

\documentclass[tikz, border=3.14mm]{standalone}
\begin{document}
    \begin{tikzpicture}
        \def\x{0.87}
        \draw (0,0) foreach \i in {1,...,50} {--++(125*\i:3*\x^\i)};
    \end{tikzpicture}
\end{document}

相关内容