我尝试在 pgfplots 中使用矩阵计算宏,但它在编译时挂起!
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\def\M{{2, 1, 1},%
{1, 2, 1},%
{1, 1, 2}}%
\pgfmathdeclarefunction{F}{3}{%
\begingroup%
\pgfmathsetmacro{\X}{{\M}[0][0]*#1+{\M}[0][1]*#2+{\M}[0][2]*#3}%
\pgfmathsetmacro{\Y}{{\M}[1][0]*#1+{\M}[1][1]*#2+{\M}[1][2]*#3}%
\pgfmathsetmacro{\Z}{{\M}[2][0]*#1+{\M}[2][1]*#2+{\M}[2][2]*#3}%
\edef\pgfmathresult{(\X,\Y,\Z)}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\newcommand{\T}[3]{
\pgfmathparse{F(#1,#2,#3)}\pgfmathresult
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot3[surf] coordinates {
\T{0}{0}{50} % <- output coordinates
\T{0}{50}{0}
\T{50}{0}{0} };
\end{axis}
\end{tikzpicture}
\end{document}
在 pgfplots 中进行此类计算的正确方法是什么?
我更喜欢这样使用它:\T(0,0,50) 但不确定是否可行!
答案1
您需要提供\addplot
一些略有不同的内容。例如,这有效:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\def\M{{2, 1, 1},%
{1, 2, 1},%
{1, 1, 2}}%
\pgfmathdeclarefunction{F}{3}{%
\begingroup%
\pgfmathsetmacro{\X}{{\M}[0][0]*#1+{\M}[0][1]*#2+{\M}[0][2]*#3}%
\pgfmathsetmacro{\Y}{{\M}[1][0]*#1+{\M}[1][1]*#2+{\M}[1][2]*#3}%
\pgfmathsetmacro{\Z}{{\M}[2][0]*#1+{\M}[2][1]*#2+{\M}[2][2]*#3}%
\edef\pgfmathresult{(\X,\Y,\Z)}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\newcommand{\T}[3]{
\pgfmathparse{F(#1,#2,#3)}\pgfmathresult
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\edef\mylst{"0,0,50","0,50,0","50,0,0"}
\pgfplotsforeachungrouped \XX in {0,1,2}
{
\pgfmathparse{{\mylst}[\XX]}
\pgfmathparse{F(\pgfmathresult)}
\ifnum\XX=0
\edef\mycoords{\pgfmathresult}
\else
\edef\mycoords{\mycoords \pgfmathresult}
\fi}
\addplot3[surf] coordinates {\mycoords};
\end{axis}
\end{tikzpicture}
\end{document}
当然,它不是一个表面。