我正在尝试用 - 重新创建一个三角形螺旋(红色),但foreach
该turn
选项表现不佳 - 每次似乎旋转得越来越少。有什么解释吗?我做错了什么?
这里我使用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