我想创建在单位单纯形上定义的函数 {(x, y, z): x + y + z = 1} 的精确曲面图。以下是我想创建的图形的草图。
我知道 和 中的曲面图PSTricks
、TikZ
中的重心坐标系TikZ
以及ternaryaxis
中的曲面图pgfplots
,但我不明白如何将它们结合起来得到我想要的结果。(我的困难不在于函数的具体形式;只要能绘制函数 x 的答案就足够了。)
我想说的是
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} % or some special type of axis
\addplot3 {<f(x,y,z)>};
\end{axis}
\end{tikzpicture}
\end{document}
f(x,y,z)
我的函数定义在哪里。这样的事情可能吗?
答案1
PGFPLOTS 有一种称为三角形的修补类型
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot3
[patch,patch type=triangle]
coordinates { (0,0,0) (1,1,.1) (0,1,.2) };
\end{axis}
\end{tikzpicture}
\end{document}
每给你三个坐标,它就会为你绘制(填充)一个三角形。
因此下一步是生成这些坐标:
\def\addtriangle#1{
\xdef\trianglesbuffer{\trianglesbuffer #1}
}
\def\calculatecoordinate(#1,#2,#3)[#4]=\f(#5,#6){
\pgfmathsetmacro#1{#5}
\pgfmathsetmacro#2{#6}
\pgfmathsetmacro#3{\f({(#5)},{(#6)})}
\pgfmathsetmacro#4{\g({(#5)},{(#6)})}
}
\def\calculatetriangle#1{
% #1 is + #1 is -
% C B A
% ◣ ◥
% A B C
\calculatecoordinate(\xa,\ya,\za)[\wa]=\f(\x,\y)
\calculatecoordinate(\xb,\yb,\zb)[\wb]=\f(\x#11,\y)
\calculatecoordinate(\xc,\yc,\zc)[\wc]=\f(\x,\y#11)
\addtriangle{(\xa,\ya,\za)[\wa] (\xb,\yb,\zb)[\wb] (\xc,\yc,\zc)[\wc]}
}
\def\calculatetheplot{
\foreach\x in{0,...,20}{
\foreach\y in{0,...,20}{
% check \x + \y + \z =1
% ◣
% ◣ ◣
% ◣ ◣ ◣
\ifnum\numexpr\x+\y<20
\calculatetriangle+
\fi
%
% ◥
% ◥ ◥
\ifnum\numexpr\x>0 \ifnum\numexpr\y>0 \ifnum\numexpr\x+\y<21
\calculatetriangle-
\fi\fi\fi
}
}
}
\begin{tikzpicture}
\begin{axis}
\def\trianglesbuffer{} % initialize the buffer
\def\f(#1,#2){#2*#2/20+2*sin(40*#1)} % we want to plot this function
\def\g(#1,#2){sqrt((#1-20/3)^2+(#2-20/3)^2)} % with this point meta
\calculatetheplot
\edef\pgfmarshal{
\noexpand\addplot3
[patch,patch type=triangle,point meta=explicit]
coordinates{\trianglesbuffer};
}
\pgfmarshal
\end{axis}
\end{tikzpicture}
接下来我们要将坐标改为单位单纯形
\def\calculatecoordinate(#1,#2,#3)[#4]=\f(#5,#6){
\pgfmathsetmacro#1{#5+\f({(#5)},{(#6)})}
\pgfmathsetmacro#2{#6+\f({(#5)},{(#6)})}
\pgfmathsetmacro#3{20-(#5)-(#6)+\f({(#5)},{(#6)})}
\pgfmathsetmacro#4{\g({(#5)},{(#6)})}
}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,axis equal,view={80}{15}]
\def\trianglesbuffer{} % clear the buffer
\def\f(#1,#2){0} % we want to plot this function
\def\g(#1,#2){0} % with this point meta
\calculatetheplot
\edef\pgfmarshal{
\noexpand\addplot3
[patch,patch type=triangle,mesh,point meta=explicit]
coordinates{\trianglesbuffer};
}
\pgfmarshal
\def\trianglesbuffer{} % clear the buffer
\def\f(#1,#2){1+sin(20*#1)*cos(30*#2)} % we want to plot this function
\def\g(#1,#2){\f({(#1)},{(#2)})} % with this point meta
\calculatetheplot
\edef\pgfmarshal{
\noexpand\addplot3
[patch,patch type=triangle,point meta=explicit]
coordinates{\trianglesbuffer};
}
\pgfmarshal
\end{axis}
\end{tikzpicture}