使用 tikz、pgfplots 绘制直角三维正方形时出现问题

使用 tikz、pgfplots 绘制直角三维正方形时出现问题

我正在绘制一个 3D 正方形,用于直角跟随https://tex.stackexchange.com/a/163384/14423。这很好用,但我尝试填充该方块,结果却不是预期的那样:

\documentclass{memoir}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}

\pgfplotsset{ /pgfplots/area style/.style={%
 area cycle list,
 area legend,
 axis on top,
 }}

\newcommand{\RightAngle}[4][5pt]{%
    \draw[fill] ($#3!#1!#2$)
    --($ #3!2!($($#3!#1!#2$)!.5!($#3!#1!#4$)$) $)
    --($#3!#1!#4$) ;
    }

\begin{document}    
\begin{tikzpicture}
 \begin{axis}[scale=0.65,view={130}{30},axis equal,axis on top,axis lines=center,xlabel=$x(t)$,ylabel=$y(t)$,zlabel=$z(t)$,xtick=\empty,ytick=\empty,ztick=\empty]
  \addplot3[thick,->,black,samples y=0] coordinates {(0.261,1.047,1) (0.511,2.047,1)};
  \addlegendentryexpanded{$T(t)$}
  \addplot3[thick,->,color=green,samples y=0] coordinates {(0.261,1.047,1) (0.261,1.047,-0.5)};
 \addlegendentryexpanded{$T'(t)$}
 \addplot3[thick,->,color=red,samples y=0] coordinates {(0.261,1.047,1) (0.261,1.047,-0.125)};
 \addlegendentryexpanded{$N(t)$}
 \addplot3[blue,domain=0:pi,samples y=0] ({x/4},{x},{sin(deg(1.5*x))});
 %\addlegendentryexpanded{$\Ce$}
 \addplot3[mark=*,mark size=1.5pt] coordinates{(0.261,1.047,1)} node{};
 \coordinate (P) at (0.261,1.047,1);
 \coordinate (T) at (0.511,2.047,1);
 \coordinate (N) at (0.261,1.047,-0.125);
 \RightAngle{(T)}{(P)}{(N)};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

如您所见,有一个虚线节点,直角的正方形没有完全填充。我做错了什么?如何用虚线填充正方形?

答案1

因为你正在使用针对旧版本优化的东西pgf图输出错误,您需要指定版本才能获得正确的输出。至少,我大致是这样理解控制台警告的。添加兼容性选项后,newest我得到了问题中发布的输出。因此,使用来自阿莱南诺的建议,这可以设置为例如,1.12如果我要求的话,这似乎是我得到的版本newest

当您指定直角时,您指定从底部的左到右,然后向上到右上角,仅此而已。TikZ 使用最短的路线完成路径,即从正方形的右上角到左下角,并填充结果,即正方形的一半。您需要指定路径P在返回起点之前经过,以便填充正方形。

\newcommand{\RightAngle}[4][5pt]{%
  \draw[fill] ($#3!#1!#2$) -- ($ #3!2!($($#3!#1!#2$)!.5!($#3!#1!#4$)$) $) -- ($#3!#1!#4$) -- #3  -- cycle;
}

我还稍微移动了图例以便我能够看到自己在做什么。

实心正方形

要用线条填充正方形,请尝试使用patterns库并使用pattern而不是fill作为路径。例如:

\newcommand{\RightAngle}[4][5pt]{%
  \draw[pattern=north east lines] ($#3!#1!#2$) -- ($ #3!2!($($#3!#1!#2$)!.5!($#3!#1!#4$)$) $) -- ($#3!#1!#4$) -- #3  -- cycle;
}

图案正方形

完整代码:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{calc,patterns}

\pgfplotsset{ /pgfplots/area style/.style={%
    area cycle list,
    area legend,
    axis on top,
  }}

\newcommand{\RightAngle}[4][5pt]{%
  \draw[pattern=north east lines] ($#3!#1!#2$) -- ($ #3!2!($($#3!#1!#2$)!.5!($#3!#1!#4$)$) $) -- ($#3!#1!#4$) -- #3  -- cycle;
}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    [
      scale=0.65,
      view={130}{30},
      axis equal,
      axis on top,
      axis lines=center,
      xlabel=$x(t)$,
      ylabel=$y(t)$,
      zlabel=$z(t)$,
      xtick=\empty,
      ytick=\empty,
      ztick=\empty,
      legend style={anchor=east}
    ]
    \addplot3[thick,->,black,samples y=0] coordinates {(0.261,1.047,1) (0.511,2.047,1)};
    \addlegendentryexpanded{$T(t)$}
    \addplot3[thick,->,color=green,samples y=0] coordinates {(0.261,1.047,1) (0.261,1.047,-0.5)};
    \addlegendentryexpanded{$T'(t)$}
    \addplot3[thick,->,color=red,samples y=0] coordinates {(0.261,1.047,1) (0.261,1.047,-0.125)};
    \addlegendentryexpanded{$N(t)$}
    \addplot3[blue,domain=0:pi,samples y=0] ({x/4},{x},{sin(deg(1.5*x))});
    %\addlegendentryexpanded{$\Ce$}
    \addplot3[mark=*,mark size=1.5pt] coordinates{(0.261,1.047,1)} node{};
    \coordinate (P) at (0.261,1.047,1);
    \coordinate (T) at (0.511,2.047,1);
    \coordinate (N) at (0.261,1.047,-0.125);
    \RightAngle{(T)}{(P)}{(N)};
  \end{axis}
\end{tikzpicture}
\end{document}

相关内容