此椭圆轨道图在 TikZ 2.10 中编译成功,但在 TikZ 3.0 中编译失败。为什么?

此椭圆轨道图在 TikZ 2.10 中编译成功,但在 TikZ 3.0 中编译失败。为什么?

我借用了一些代码开普勒第二定律来自 Tosco 回答的 TeX 上的链接帖子。我有一段时间没有编译文档了,但今天需要。当我编译时,我被告知尺寸太大。尺寸太大指向以下几行:

% \draw[very thin,->](\oldx,\ol dy)--++(\forcex,\forcey);
\ifnumgreater{(\n - \startone) * (\endone - \n)}{-1}

在进行某些更新(不确定是哪个更新破坏了它)之前,此代码可以正确编译并运行。现在不行了。该怎么做?

\documentclass[convert = false, tikz]{standalone}

\usepackage{amsmath}
\usepackage{etoolbox}
\usetikzlibrary{backgrounds}

\begin{document}

\gdef\myposx{10.0}
\gdef\myposy{0.0}
\gdef\vx{0.0}
\gdef\vy{4.6}
\gdef\forcefactor{150}
\gdef\deltat{0.025}
\gdef\smallmass{1}
\gdef\startone{100}
\gdef\endone{200}
\gdef\starttwo{1800}
\gdef\endtwo{1900}
\gdef\pathone{}
\gdef\pathtwo{}

\begin{tikzpicture}[scale = .2]
  \filldraw(0, 0) circle [radius = 0.5cm];
  \foreach \n in {1, ..., 3625}
  {
    \pgfextra{%
      \global\let\oldx\myposx
      \global\let\oldy\myposy
      \pgfmathsetmacro{\currentsquareddistance}{\myposx * \myposx + \myposy *
        \myposy}
      \pgfmathsetmacro{\currentforce}{\forcefactor / \currentsquareddistance}
      \pgfmathsetmacro{\currentangle}{atan2(\myposx, \myposy)}
      \pgfmathsetmacro{\currentforcex}{-1 * \currentforce * cos(\currentangle)}
      \pgfmathsetmacro{\currentforcey}{-1 * \currentforce * sin(\currentangle)}
      \pgfmathsetmacro{\currentvx}{\vx + \deltat * \currentforcex / \smallmass}
      \pgfmathsetmacro{\currentvy}{\vy + \deltat * \currentforcey / \smallmass}
      \pgfmathsetmacro{\currentposx}{\myposx + \deltat * \currentvx}
      \pgfmathsetmacro{\currentposy}{\myposy + \deltat * \currentvy}
      \global\let\myposx\currentposx
      \global\let\myposy\currentposy
      \global\let\vx\currentvx
      \global\let\vy\currentvy
      \global\let\forcex\currentforcex
      \global\let\forcey\currentforcey
      \global\let\myangle\currentangle
      \ifnumequal{\n}{\startone}{%
        \global\let\startonex\oldx
        \global\let\startoney\oldy
        \xappto{\pathone}{(\oldx, \oldy)}
      }{}
      \ifnumequal{\n}{\starttwo}{%
        \global\let\starttwox\oldx
        \global\let\starttwoy\oldy
        \xappto{\pathtwo}{(\oldx, \oldy)}
      }{}
      \ifnumequal{\n}{\endone}{%
        \global\let\endonex\myposx
        \global\let\endoney\myposy
        \xappto{\pathone}{,(\myposx,\myposy)}
      }{}
      \ifnumequal{\n}{\endtwo}{%
        \global\let\endtwox\myposx
        \global\let\endtwoy\myposy
        \xappto{\pathtwo}{,(\myposx, \myposy)}
      }{}
    }
    % \draw[very thin,->](\oldx,\ol dy)--++(\forcex,\forcey);
    \ifnumgreater{(\n - \startone) * (\endone - \n)}{-1}
    {
      \pgfextra{%
        \xappto{\pathone}{,(\myposx, \myposy)}
      }
    }
    {}
    \ifnumgreater{(\n - \starttwo) * (\endtwo - \n)}{-1}
    {
      \pgfextra{%
        \xappto{\pathtwo}{,(\myposx, \myposy)}
      }
    }
    {}
    \draw(\oldx, \oldy)--(\myposx, \myposy);
  }
  \begin{scope}[on background layer]
    \filldraw[blue, opacity = .5] (0, 0)%
    \foreach \point in \pathone
    {%
      --\point
    }--(0, 0);
    \filldraw[blue, opacity = .5] (0, 0)%
    \foreach \point in \pathtwo
    {%
      --\point
    }--(0, 0);
  \end{scope}
\end{tikzpicture}
\end{document}

答案1

问题在于atan2反正切的双参数变体,在 TikZ 2.10 和 TikZ 3.0 之间发生了变化。

这种语法变化对托肖的代码使得轨道物体(小行星?)的轨迹很快变得完全不稳定:小行星很快“从页面上射出”。结果,生成的图表的尺寸迅速超出了 TeX 可以处理的范围(大约 19 英尺),TeX 发出Dimension too large错误并发出投诉。

atan2让我们仔细检查一下 v2.10 和 v3.0 手册中的描述……

TikZ v2.10,发布于 2010/12/20(63.2.3三角函数

在此处输入图片描述

TikZ 3.0,发布于 2013/12/20(90.2.4三角函数

在此处输入图片描述

问题

在这两本手册中,示例中的输入是相同的,语法也是相同的……是吗?正如 percusse 在他的评论两个参数xy已在两个版本之间交换!因此,输出不同。那些 TikZ 维护者肯定很狡猾}:-)

解决方案

atan2只需在代码中交换参数,即编写

\pgfmathsetmacro{\currentangle}{atan2(\myposy, \myposx)}

代替

\pgfmathsetmacro{\currentangle}{atan2(\myposx, \myposy)}

产生正确的输出,如下所示。

在此处输入图片描述

相关内容