TikZ \pic 角度保持不可见

TikZ \pic 角度保持不可见

我正在尝试H--S--E使用\pic之前绘图中使用的命令在点之间绘制一个角度,但这次没有显示任何角度,尽管没有错误消息表明出了问题,所以我猜它位于较低的层或类似的东西中?图形的其余部分已经完成(不过,就代码的其他部分而言,我愿意接受代码审查)。

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{decorations.pathreplacing,calligraphy,quotes,angles,calc}

\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \global\edef#2{\the\pgf@x}%
  \global\edef#3{\the\pgf@y}%
}
\newcommand{\gettikzcoordinates}[2]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \pgfmathsetmacro{\myx}{round(0.99626*\the\pgf@x/0.0283465)/1000}
  \pgfmathsetmacro{\myy}{round(0.99626*\the\pgf@y/0.0283465)/1000}
  \global\edef#2{(\myx,\myy)}%
}
\makeatother

\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \pgfmathsetmacro{\g}{9.81}
  \pgfmathsetmacro{\v}{10}
  \pgfmathsetmacro{\h}{10}
  \pgfmathsetmacro{\a}{48}
  \pgfmathsetmacro{\Range}{\v * cos(\a)/\g * (\v * sin(\a) + sqrt(\v^2 * sin(\a)^2 + 2 * \g * \h))}
  \pgfmathsetmacro{\Height}{\v^2 * sin(\a)^2 / (2 * \g) + \h}
  \pgfmathsetmacro{\Start}{\v * cos(\a)/\g * (\v * sin(\a) - sqrt(\v^2 * sin(\a)^2 + 2 * \g * \h))}

  \coordinate (S) at (0,\h);
  \coordinate (E) at (\Range+\Start,\h);
  \coordinate (H) at (\Range/2+\Start/2,\Height);
  \coordinate (X) at (\Range/2+\Start/2,0);
  \coordinate (T) at (\Range,0);

  \begin{axis}[
      xmin=0,
      xmax=\Range + 2,
      ymin=0,
      ymax=\Height + 2,
      xlabel near ticks,
      ylabel near ticks,
      xlabel={$x$ in m},
      ylabel={$y$ in m},
      legend pos=north east
    ]

    \addlegendentry{$y(x)$}
    \edef\temp{%
      \noexpand
      \addplot[smooth,thick,blue,domain=0:\Range,samples=200] {
        \h + tan(\a) * x - (\g * x^2)/(2 * \v^2) * sec(\a)^2
      } node[] {};
    }\temp

    \gettikzcoordinates{(S)}{\S}
    \gettikzcoordinates{(E)}{\E}
    \gettikzcoordinates{(H)}{\H}
    \gettikzcoordinates{(X)}{\X}
    \gettikzcoordinates{(T)}{\T}

    % point of impact
    \addplot[mark=*] coordinates {\T} node[pin=80:{$T$}]{};
    % max trajectory height
    \addplot[mark=*] coordinates {\H} node[pin=150:{$H$}]{};
    % maximum height
    \draw[dashed,color=gray] \X -- \H;
    \addplot[dashed,color=gray,domain=0:\Range+2] { \Height } node[] {};
    % angle
    \draw[dashed,color=gray] \S -- \E;
    \pic[draw,fill=blue,fill opacity=0.3,angle radius=12mm,"$\theta$" opacity=1] {angle=H--S--E};
    % horizontal distance
    \draw[decorate,decoration={calligraphic brace,raise=5pt,amplitude=5pt,aspect=0.5},thick,pen colour=red] (0,0) -- \T node[pos=0.5,above=10pt,black]{$d$};
  \end{axis}
\end{tikzpicture}

\end{document}

答案1

您的角度被轴框剪裁了。

我不会混合 TikZ 坐标(x, y)和 PGFPlots 坐标(axis cs: x, y)

如果您在坐标中添加标签 ( \coordinate[label=S](S)at(0,\h);),您会注意到这些坐标可能不在您认为的位置。如果您移动它,\pic\end{axis}会显示这些坐标的位置。

axis用定义里面的坐标axis cs然后使用它们。不需要\get任何坐标。

代码

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{decorations.pathreplacing,calligraphy,quotes,angles,calc}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\g}{9.81}
  \pgfmathsetmacro{\v}{10}
  \pgfmathsetmacro{\h}{10}
  \pgfmathsetmacro{\a}{48}
  \pgfmathsetmacro{\Range}{\v * cos(\a)/\g * (\v * sin(\a) + sqrt(\v^2 * sin(\a)^2 + 2 * \g * \h))}
  \pgfmathsetmacro{\Height}{\v^2 * sin(\a)^2 / (2 * \g) + \h}
  \pgfmathsetmacro{\Start}{\v * cos(\a)/\g * (\v * sin(\a) - sqrt(\v^2 * sin(\a)^2 + 2 * \g * \h))}

  \begin{axis}[
      xmin=0,
      xmax=\Range + 2,
      ymin=0,
      ymax=\Height + 2,
      xlabel near ticks,
      ylabel near ticks,
      xlabel={$x$ in m},
      ylabel={$y$ in m},
      legend pos=north east
    ]

    \addlegendentry{$y(x)$}
    % \edef\temp{%
    %   \noexpand
      \addplot[smooth,thick,blue,domain=0:\Range,samples=200] {
        \h + tan(\a) * x - (\g * x^2)/(2 * \v^2) * sec(\a)^2
      } node[] {};
    % }\temp


    % point of impact
    \addplot[mark=*] coordinates {(\Range,0)} coordinate[name=T,pin=80:{$T$}];
    % max trajectory height
    \addplot[mark=*] coordinates {(\Range/2+\Start/2,\Height)} coordinate[name=H,pin=150:{$H$}];
    
    % the other coordinates:
    \coordinate (S) at (axis cs: 0,\h);
    \coordinate (E) at (axis cs: \Range+\Start,\h);
    \coordinate (X) at (axis cs: \Range/2+\Start/2,0);
    
    % maximum height
    \draw[dashed,color=gray] (X) -- (H);
    \addplot[dashed,color=gray,domain=0:\Range+2] { \Height } node[] {};
    
    % angle
    \draw[dashed,color=gray] (S) -- (E);
    \pic[draw,fill=blue,fill opacity=0.3,angle radius=12mm,"$\theta$" opacity=1] {angle=E--S--H};
    \draw[dashed] (S) -- (H);% just for fun
    % horizontal distance
    \draw[decorate,decoration={calligraphic brace,raise=5pt,amplitude=5pt,aspect=0.5},thick,pen colour=red] (0,0) -- (T) node[pos=0.5,above=10pt,black]{$d$};
  \end{axis}
\end{tikzpicture}
\end{document}

相关内容