![有没有一种简单的方法可以绘制带阴影的等距物体](https://linux22.com/image/409686/%E6%9C%89%E6%B2%A1%E6%9C%89%E4%B8%80%E7%A7%8D%E7%AE%80%E5%8D%95%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%AF%E4%BB%A5%E7%BB%98%E5%88%B6%E5%B8%A6%E9%98%B4%E5%BD%B1%E7%9A%84%E7%AD%89%E8%B7%9D%E7%89%A9%E4%BD%93.png)
我已经开始尝试使用以下代码来绘制它这里这就是我所拥有的:
documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes}
\tikzset{
iso/.style={kite, draw, kite vertex angles=120, minimum size=1cm, outer sep=0pt}
}
\newcommand{\isogrid}[2]{
\foreach \i [count=\row from 0, remember=\row as \lastrow (initially 0)] in {0,...,#1}{
\foreach \j [count=\col from 0, remember=\col as \lastcol (initially 0)] in {0,...,#2}{
\ifnum\row=0
\ifnum\col=0
\node[iso] (\row-\col) {\row-\col};
\else
\node[iso, anchor=left vertex] (\row-\col) at (\row-\lastcol.right vertex) {\row-\col};
\fi
\else
\ifnum\col=0
\node[iso, anchor=upper vertex] (\row-\col) at (\lastrow-\col.lower vertex) {\row-\col};
\else
\node[iso, anchor=left vertex] (\row-\col) at (\row-\lastcol.right vertex) {\row-\col};
\fi
\fi
}
}
\draw (0-0.left vertex)--(#1-0.left vertex);
\foreach \i in {0,...,#2}{
\draw (0-\i.upper vertex)--(#1-\i.lower vertex);
\draw (0-\i.right vertex)--(#1-\i.right vertex);
}
}
\begin{document}
\begin{tikzpicture}
\isogrid{3}{3}
\draw[fill=red!70!black] (3-0.left vertex) -- (0-0.left vertex) -- (0-0.lower vertex) -- (3-0.lower vertex) -- cycle;
\draw[fill=red!40!black] (0-0.lower vertex) -- (0-0.right vertex) -- (3-0.right vertex) -- (3-0.lower vertex);
\draw[fill=red!80!white] (0-0.lower vertex) -- (0-0.left vertex) -- (0-0.upper vertex) -- (0-0.right vertex) -- cycle;
\end{tikzpicture}
\end{document}
我想知道是否有一个包或者更简单的方法来绘制这类图表
答案1
据我所知,没有明确的方案。然而,有一个回答可以使用。
\documentclass[tikz,border=3.14mm]{standalone}
% from https://tex.stackexchange.com/a/499703
\definecolor{redxy}{RGB}{214,106,77}
\definecolor{redxz}{RGB}{185,90,66}
\definecolor{redyz}{RGB}{149,61,38}
\usepackage{tikz-3dplot}
\tikzset{plane/.style n args={3}{insert path={%
#1 -- ++ #2 -- ++ #3 -- ++ ($-1*#2$) -- cycle}},
unit xy plane/.style={plane={#1}{(\CubeX,0,0)}{(0,\CubeY,0)}},
unit xz plane/.style={plane={#1}{(\CubeX,0,0)}{(0,0,\CubeZ)}},
unit yz plane/.style={plane={#1}{(0,\CubeY,0)}{(0,0,\CubeZ)}},
get projections/.style={insert path={%
let \p1=(1,0,0),\p2=(0,1,0) in
[/utils/exec={\pgfmathtruncatemacro{\xproj}{sign(\x1)}\xdef\xproj{\xproj}
\pgfmathtruncatemacro{\yproj}{sign(\x2)}\xdef\yproj{\yproj}
\pgfmathtruncatemacro{\zproj}{sign(cos(\tdplotmaintheta))}\xdef\zproj{\zproj}}]}},
pics/unit cube/.style={code={
\path[get projections];
\ifnum\zproj=-1
\path[3d cube/every face,3d cube/xy face,unit xy plane={(-\CubeX/2,-\CubeY/2,-\CubeZ/2)}];
\else
\path[3d cube/every face,3d cube/xy face,unit xy plane={(-\CubeX/2,-\CubeY/2,\CubeZ/2)}];
\fi
\ifnum\yproj=1
\path[3d cube/every face,3d cube/yz face,unit yz plane={(-\CubeX/2,-\CubeY/2,-\CubeZ/2)}];
\else
\path[3d cube/every face,3d cube/yz face,unit yz plane={(\CubeX/2,-\CubeY/2,-\CubeZ/2)}];
\fi
\ifnum\xproj=1
\path[3d cube/every face,3d cube/xz face,unit xz plane={(-\CubeX/2,\CubeY/2,-\CubeZ/2)}];
\else
\path[3d cube/every face,3d cube/xz face,unit xz plane={(-\CubeX/2,-\CubeY/2,-\CubeZ/2)}];
\fi
\ifnum\yproj=1
\path[3d cube/every face,3d cube/yz face,unit yz plane={(\CubeX/2,-\CubeY/2,-\CubeZ/2)}];
\else
\path[3d cube/every face,3d cube/yz face,unit yz plane={(-\CubeX/2,-\CubeY/2,-\CubeZ/2)}];
\fi
\ifnum\xproj=1
\path[3d cube/every face,3d cube/xz face,unit xz plane={(-\CubeX/2,-\CubeY/2,-\CubeZ/2)}];
\else
\path[3d cube/every face,3d cube/xz face,unit xz plane={(-\CubeX/2,\CubeY/2,-\CubeZ/2)}];
\fi
\ifnum\zproj>-1
\path[3d cube/every face,3d cube/xy face,unit xy plane={(-\CubeX/2,-\CubeY/2,\CubeZ/2)}];
\else
\path[3d cube/every face,3d cube/xy face,unit xy plane={(-\CubeX/2,-\CubeY/2,-\CubeZ/2)}];
\fi
}},
3d cube/.cd,
xy face/.style={fill=red!20},
xz face/.style={fill=blue!20},
yz face/.style={fill=orange!30},
num cubes x/.estore in=\NumCubesX,
num cubes y/.estore in=\NumCubesY,
num cubes z/.estore in=\NumCubesZ,
num cubes x/.initial=1,num cubes y/.initial=1,num cubes z/.initial=1,
cube x/.estore in=\CubeX,
cube y/.estore in=\CubeY,
cube z/.estore in=\CubeZ,
cube x=1,cube y=1,cube z=1,
cube scale/.initial=1,
every face/.style={draw,very thick},
/tikz/pics/.cd,
cube array/.style={code={%
\tikzset{3d cube/.cd,#1}
%\typeout{\NumCubesX,\NumCubesY,\NumCubesZ}
\path[get projections];
\ifnum\yproj=1
\def\LstX{1,...,\NumCubesX}
\else
\ifnum\NumCubesX>1
\pgfmathtruncatemacro{\NextToLast}{\NumCubesX-1}
\def\LstX{\NumCubesX,\NextToLast,...,1}
\else
\def\LstX{1}
\fi
\fi
\ifnum\xproj=-1
\def\LstY{1,...,\NumCubesY}
\else
\ifnum\NumCubesY>1
\pgfmathtruncatemacro{\NextToLast}{\NumCubesY-1}
\def\LstY{\NumCubesY,\NextToLast,...,1}
\else
\def\LstY{1}
\fi
\fi
\ifnum\zproj=1
\def\LstZ{1,...,\NumCubesZ}
\else
\ifnum\NumCubesZ>1
\pgfmathtruncatemacro{\NextToLast}{\NumCubesZ-1}
\def\LstZ{\NumCubesZ,\NextToLast,...,1}
\else
\def\LstZ{1}
\fi
\fi
\foreach \X in \LstX
{\foreach \Y in \LstY
{\foreach \Z in \LstZ
{\path (\X-1/2,\Y-1/2,\Z-1/2)
pic[scale=\pgfkeysvalueof{/tikz/3d cube/cube scale}]{unit cube};}}
}
}}
}
\begin{document}
\tdplotsetmaincoords{70}{50} % the first argument cannot be larger than 90
\begin{tikzpicture}[line join=round,tdplot_main_coords,font=\sffamily,3d cube/.cd,
num cubes x=1,num cubes y=1,num cubes z=1]
\begin{scope}[3d cube/.cd,every face/.style={draw=none},
xy face/.style={fill=redxy},xz face/.style={fill=redxz},yz face/.style={fill=redyz}]
% top left
\path (0,0,0) pic{cube array={num cubes y=2,num cubes z=3}}
(0,-1,0) pic{cube array={num cubes y=1,num cubes z=1}}
(1,0,0) pic{cube array={num cubes y=2,num cubes z=2}}
(1,1,2) pic{cube array={num cubes y=1,num cubes z=1}}
(2,1,0) pic{cube array={num cubes y=1,num cubes z=1}}
;
\end{scope}
\end{tikzpicture}
\end{document}
对于等距视图,选择
\tdplotsetmaincoords{90-35.26}{45}
你可以相当自由地选择视角。对于非常不同的视角,需要以不同的顺序排列数组。
\documentclass[tikz,border=3.14mm]{standalone}
\definecolor{redxy}{RGB}{214,106,77}
\definecolor{redxz}{RGB}{185,90,66}
\definecolor{redyz}{RGB}{149,61,38}
\usepackage{tikz-3dplot}
\usetikzlibrary{backgrounds}
\newcounter{x}
\newcounter{y}
\newcounter{z}
\tikzset{plane/.style n args={3}{insert path={%
#1 -- ++ #2 -- ++ #3 -- ++ ($-1*#2$) -- cycle}},
unit xy plane/.style={plane={#1}{(1,0,0)}{(0,1,0)}},
unit xz plane/.style={plane={#1}{(1,0,0)}{(0,0,1)}},
unit yz plane/.style={plane={#1}{(0,1,0)}{(0,0,1)}},
get projections/.style={insert path={%
let \p1=(1,0,0),\p2=(0,1,0) in
[/utils/exec={\pgfmathtruncatemacro{\xproj}{sign(\x1)}\xdef\xproj{\xproj}
\pgfmathtruncatemacro{\yproj}{sign(\x2)}\xdef\yproj{\yproj}
\pgfmathtruncatemacro{\zproj}{sign(cos(\tdplotmaintheta))}\xdef\zproj{\zproj}}]}},
pics/unit cube/.style={code={
\path[get projections];
\draw (0,0,0) -- (1,1,1);
\ifnum\zproj=-1
\path[3d cube/every face,3d cube/xy face,unit xy plane={(0,0,0)}];
\fi
\ifnum\yproj=1
\path[3d cube/every face,3d cube/yz face,unit yz plane={(1,0,0)}];
\else
\path[3d cube/every face,3d cube/yz face,unit yz plane={(0,0,0)}];
\fi
\ifnum\xproj=1
\path[3d cube/every face,3d cube/xz face,unit xz plane={(0,0,0)}];
\else
\path[3d cube/every face,3d cube/xz face,unit xz plane={(0,1,0)}];
\fi
\ifnum\zproj>-1
\path[3d cube/every face,3d cube/xy face,unit xy plane={(0,0,1)}];
\fi
}},
3d cube/.cd,
xy face/.style={fill=red!20},
xz face/.style={fill=blue!20},
yz face/.style={fill=orange!30},
num cubes x/.estore in=\NumCubesX,
num cubes y/.estore in=\NumCubesY,
num cubes z/.estore in=\NumCubesZ,
num cubes x=1,num cubes y/.initial=1,num cubes z/.initial=1,
cube scale/.initial=0.9,
every face/.style={draw,very thick},}
\newcommand\Planepartition[1]{
\setcounter{x}{0}\setcounter{y}{0}\setcounter{z}{0}
\foreach \Lst [count=\Z starting from 0] in {#1} {
\pgfmathtruncatemacro{\tmp}{max(\value{z},\Z)}
\setcounter{z}{\tmp}
\foreach \Xmax [count=\Y] in \Lst {
\foreach \X in {1,...,\Xmax}
{\path (\X-1,-\Y,\Z) pic{unit cube};
\pgfmathtruncatemacro{\tmp}{max(\value{x},\X)}
\setcounter{x}{\tmp}
\pgfmathtruncatemacro{\tmp}{max(\value{y},\Y)}
\setcounter{y}{\tmp}
}
}
}
\begin{scope}[on background layer]
\begin{scope}[canvas is xy plane at z=0,transform shape]
\path[/tikz/3d cube/xy face] (0,0) rectangle (\value{x},-\value{y});
\draw[/tikz/3d cube/every face] (0,0) grid (\value{x},-\value{y});
\end{scope}
\begin{scope}[canvas is yz plane at x=0,transform shape]
\path[/tikz/3d cube/yz face] (0,0) rectangle (-\value{y},1+\value{z});
\draw[/tikz/3d cube/every face] (0,0) grid (-\value{y},1+\value{z});
\end{scope}
\begin{scope}[canvas is zx plane at y=0,transform shape]
\path[/tikz/3d cube/xz face] (0,0) rectangle (1+\value{z},\value{x});
\draw[/tikz/3d cube/every face] (0,0) grid (1+\value{z},\value{x});
\end{scope}
\end{scope}
}
\begin{document}
\tdplotsetmaincoords{70}{50} % the first argument cannot be larger than 90
\begin{tikzpicture}[line join=round,tdplot_main_coords]
\begin{scope}[3d cube/.cd,every face/.style={},xy face/.style={fill=redxy},
xz face/.style={fill=redxz},yz face/.style={fill=redyz}]
\Planepartition{{6,6,5,4,4,4,2},{6,4,4,3,2,2},{5,4,3,3,2,1},{4,3,3,2,1},{4,2,2,1,1},{4,2,1},{2}}
\end{scope}
\end{tikzpicture}
\end{document}