我有一个 3D 子集,它有以下限制:
1<=x<=2,
1<=y<=2,
1<=z,
2x+2y-z<=5.
到目前为止,我刚刚绘制了限制 x 和 y 的点。我该如何绘制最后的限制?
我目前拥有的:
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{155}
\usetikzlibrary{3d}
\begin{tikzpicture}[scale=2,tdplot_main_coords]
\tikzstyle{grid}=[thin,color=red,tdplot_rotated_coords]
\draw [color=gray, fill=blue!15] (0,0,0) -- (0,0,4) --(0,4,4) -- (0,4,0) -- (0,0,0);
\draw [color=gray, fill=blue!10] (0,0,0) -- (0,4,0) --(4,4,0) -- (4,0,0) -- (0,0,0);
\draw [color=gray, fill=blue!20] (0,0,0) -- (4,0,0) --(4,0,4) -- (0,0,4) -- (0,0,0);
%for X (greater than 1, less than 2)
\filldraw[draw=black,fill=black!60, fill opacity=.5] (1,1,1) -- (1,1,4) -- (1,2,4) -- (1,2,1) -- (1,1,1);
\filldraw[draw=black,fill=black!60,fill opacity=.2] (2,1,1) -- (2,1,4) -- (2,2,4) -- (2,2,1) -- (2,1,1);
%for Y (greater than 1, less than 2)
\filldraw[draw=black,fill=black!60,fill opacity=.5] (1,1,1) -- (1,1,4) -- (2,1,4) -- (2,1,1) -- (1,1,1);
\filldraw[draw=black,fill=black!60,fill opacity=.2] (1,2,1) -- (1,2,4) -- (2,2,4) -- (2,2,1) -- (1,2,1);
%for Z (greater than 1)
\filldraw[draw=black, fill=black!60, fill opacity=.5] (1,1,1) -- (1,2,1) -- (2,2,1) -- (2,1,1) -- (1,1,1);
%grid
\draw[thick,->] (0,0,0) -- (4,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0,0) -- (0,4,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,4) node[anchor=south] {$z$};
%
%\node at (2,2,4) [circle,fill=black] {};
\node at (2,2,1) [circle,fill=black] {};
%
%\node at (2,1,4) [circle,fill=black] {};
\node at (2,1,1) [circle,fill=black] {};
%
%\node at (1,2,4) [circle,fill=black] {};
\node at (1,2,1) [circle,fill=black] {};
%
%\node at (1,1,4) [circle,fill=black] {};
\node at (1,1,1) [circle,fill=black] {};
\end{tikzpicture}
\end{document}
答案1
手动解决方案(参见里面的评论):
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{155}
\usetikzlibrary{3d}
\begin{tikzpicture}[scale=2,tdplot_main_coords]
\tikzstyle{grid}=[thin,color=red,tdplot_rotated_coords]
\draw [color=gray, fill=blue!15] (0,0,0) -- (0,0,4) --(0,4,4) --(0,4,0)-- cycle;
\draw [color=gray, fill=blue!20] (0,0,0) -- (4,0,0) --(4,0,4) -- (0,0,4) -- cycle;
\draw [color=gray, fill=blue!10,opacity=0.3] (0,0,0) -- (0,4,0) --(4,4,0) -- (4,0,0) -- cycle;
%for Z (greater than 1 uncomment to see as blue)
%\filldraw[draw=black, fill=blue!80, fill opacity=.5] (1,1,1) -- (1,2,1) -- (2,2,1) -- (2,1,1) -- cycle;
%grid
\draw[thick,->] (0,0,0) -- (4,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0,0) -- (0,4,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,4) node[anchor=south] {$z$};
%2x+2y-z<=5 ,means that z<=2x+2y-5 For x=1: z<=2y-3 For x=2: z<=2y-1
%Taking in account y values:
%for(x,y)=(1,1):z_maxpoint(1,1,-1)
%for(x,y)=(2,1):z_maxpoint(2,1,1)
%for(x,y)=(1,2):z_maxpoint(1,2,1)
%for(x,y)=(2,2):z_maxpoint(2,2,3)
%If the resulting points are on one plain means that we possibly
%did it correct
%Just connecting these lines will give the result (uncomment to see it ar red):
%\filldraw[draw=black, fill=red!60, fill opacity=.5,name path=upz] (1,1,-1)node[below]{-} -- (2,1,1) -- (2,2,3) -- (1,2,1) -- cycle;
% (1,1-1) not needed because it is less than out zmin=1
% Result
%The above plane intersects with z=1 on line:
\draw[dashed] (2,1,1)--(1,2,1);
%The plane above that is:
\filldraw[draw=black,dashed, fill=red!20, fill opacity=.5] (2,1,1) -- (2,2,3) -- (1,2,1) -- cycle;
%point (2,2,3) is above (2,2,1) of minimum z plane
\draw (2,2,3)--(2,2,1);
%plane below
\filldraw[draw=black,dashed,fill=red!40,fill opacity=.5] (2,1,1)--(2,2,1)--(1,2,1)--cycle;
%last two planes
\filldraw[draw=black,fill=red!20,fill opacity=.5] (2,1,1)--(2,2,3)--(2,2,1)--cycle;
\filldraw[draw=black,fill=red!20,fill opacity=.5] (2,2,3)--(1,2,1)--(2,2,1)--cycle;
\node at (2,2,1) [circle,fill=black] {};
%
%\node at (2,1,4) [circle,fill=black] {};
\node at (2,1,1) [circle,fill=black] {};
%
%\node at (1,2,4) [circle,fill=black] {};
\node at (1,2,1) [circle,fill=black] {};
%
%\node at (1,1,1) [circle,fill=black] {};
\node at (2,2,3) [circle,fill=black] {};
\end{tikzpicture}
\end{document}
输出: