我结合了来自其他两个线程的部分代码来创建长方体图。效果很好,但我想以更“3D”的方式显示连接长方体的线。现在有些线重叠了。此外,我将在长方体的两侧连接更多组件。不知道如何进行正确的 3D 连接。
不是真正的 tikz 程序员。所以我只是阅读并组合代码。Cuboid 来自汤姆·邦巴迪尔
\newif\ifcuboidshade
\newif\ifcuboidemphedge
\definecolor{babyblue}{rgb}{0.54, 0.81, 0.94}
\definecolor{carolinablue}{rgb}{0.6, 0.73, 0.89}
\definecolor{ceruleanblue}{rgb}{0.16, 0.32, 0.75}
\definecolor{languidlavender}{rgb}{0.84, 0.79, 0.87}
\definecolor{tan}{rgb}{0.82, 0.71, 0.55}
\definecolor{tearose(rose)}{rgb}{0.96, 0.76, 0.76}
\definecolor{wisteria}{rgb}{0.79, 0.63, 0.86}
\definecolor{skymagenta}{rgb}{0.81, 0.44, 0.69}
\tikzset{
laser beam action/.style={
line width=\pgflinewidth+.1pt,draw opacity=.1,draw=#1,
},
laser beam recurs/.code 2 args={%
\pgfmathtruncatemacro{\level}{#1-1}%
\ifthenelse{\equal{\level}{0}}%
{\tikzset{preaction={laser beam action=#2}}}%
{\tikzset{preaction={laser beam action=#2,laser beam recurs={\level}{#2}}}}
},
laser beam/.style={preaction={laser beam recurs={10}{#1}},draw opacity=1,draw=#1},
}
\tikzset{
cuboid/.is family,
cuboid,
shiftx/.initial=0,
shifty/.initial=0,
dimx/.initial=3,
dimy/.initial=3,
dimz/.initial=3,
scale/.initial=1,
densityx/.initial=1,
densityy/.initial=1,
densityz/.initial=1,
rotation/.initial=0,
anglex/.initial=0,
angley/.initial=90,
anglez/.initial=225,
scalex/.initial=0.3,
scaley/.initial=0.3,
scalez/.initial=0.3,
front/.style={draw=black,fill=babyblue},
top/.style={draw=black,fill=babyblue},
right/.style={draw=black,fill=babyblue},
shade/.is if=cuboidshade,
shadecolordark/.initial=black,
shadecolorlight/.initial=white,
shadeopacity/.initial=0.15,
shadesamples/.initial=16,
emphedge/.is if=cuboidemphedge,
emphstyle/.style={thick},
}
\newcommand{\tikzcuboidkey}[1]{\pgfkeysvalueof{/tikz/cuboid/#1}}
% Commands
\newcommand{\tikzcuboid}[1]{
\tikzset{cuboid,#1} % Process Keys passed to command
\pgfmathsetlengthmacro{\vectorxx}{\tikzcuboidkey{scalex}*cos(\tikzcuboidkey{anglex})*28.452756}
\pgfmathsetlengthmacro{\vectorxy}{\tikzcuboidkey{scalex}*sin(\tikzcuboidkey{anglex})*28.452756}
\pgfmathsetlengthmacro{\vectoryx}{\tikzcuboidkey{scaley}*cos(\tikzcuboidkey{angley})*28.452756}
\pgfmathsetlengthmacro{\vectoryy}{\tikzcuboidkey{scaley}*sin(\tikzcuboidkey{angley})*28.452756}
\pgfmathsetlengthmacro{\vectorzx}{\tikzcuboidkey{scalez}*cos(\tikzcuboidkey{anglez})*28.452756}
\pgfmathsetlengthmacro{\vectorzy}{\tikzcuboidkey{scalez}*sin(\tikzcuboidkey{anglez})*28.452756}
\begin{scope}[xshift=\tikzcuboidkey{shiftx}, yshift=\tikzcuboidkey{shifty}, scale=\tikzcuboidkey{scale}, rotate=\tikzcuboidkey{rotation}, x={(\vectorxx,\vectorxy)}, y={(\vectoryx,\vectoryy)}, z={(\vectorzx,\vectorzy)}]
\pgfmathsetmacro{\steppingx}{1/\tikzcuboidkey{densityx}}
\pgfmathsetmacro{\steppingy}{1/\tikzcuboidkey{densityy}}
\pgfmathsetmacro{\steppingz}{1/\tikzcuboidkey{densityz}}
\newcommand{\dimx}{\tikzcuboidkey{dimx}}
\newcommand{\dimy}{\tikzcuboidkey{dimy}}
\newcommand{\dimz}{\tikzcuboidkey{dimz}}
\pgfmathsetmacro{\secondx}{2*\steppingx}
\pgfmathsetmacro{\secondy}{2*\steppingy}
\pgfmathsetmacro{\secondz}{2*\steppingz}
\ifthenelse{\equal{\dimx}{1}}
{\foreach \x in {\steppingx,...,\dimx}}
{\foreach \x in {\steppingx,\secondx,...,\dimx}}
{ \ifthenelse{\equal{\dimy}{1}}
{\foreach \y in {\steppingy,...,\dimy}}
{\foreach \y in {\steppingy,\secondy,...,\dimy}}
{ \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
\pgfmathsetmacro{\lowy}{(\y-\steppingy)}
\filldraw[cuboid/front] (\lowx,\lowy,\dimz) -- (\lowx,\y,\dimz) -- (\x,\y,\dimz) -- (\x,\lowy,\dimz) -- cycle;
}
}
\ifthenelse{\equal{\dimx}{1}}
{\foreach \x in {\steppingx,...,\dimx}}
{\foreach \x in {\steppingx,\secondx,...,\dimx}}
{ \ifthenelse{\equal{\dimz}{1}}
{\foreach \z in {\steppingz,...,\dimz}}
{\foreach \z in {\steppingz,\secondz,...,\dimz}}
{ \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
\pgfmathsetmacro{\lowz}{(\z-\steppingz)}
\filldraw[cuboid/top] (\lowx,\dimy,\lowz) -- (\lowx,\dimy,\z) -- (\x,\dimy,\z) -- (\x,\dimy,\lowz) -- cycle;
}
}
\ifthenelse{\equal{\dimy}{1}}
{\foreach \y in {\steppingy,...,\dimy}}
{\foreach \y in {\steppingy,\secondy,...,\dimy}}
{ \ifthenelse{\equal{\dimz}{1}}
{\foreach \z in {\steppingz,...,\dimz}}
{\foreach \z in {\steppingz,\secondz,...,\dimz}}
{ \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
\pgfmathsetmacro{\lowz}{(\z-\steppingz)}
\filldraw[cuboid/right] (\dimx,\lowy,\lowz) -- (\dimx,\lowy,\z) -- (\dimx,\y,\z) -- (\dimx,\y,\lowz) -- cycle;
}
}
\ifcuboidemphedge
\draw[cuboid/emphstyle] (0,\dimy,0) -- (\dimx,\dimy,0) -- (\dimx,\dimy,\dimz) -- (0,\dimy,\dimz) -- cycle;%
\draw[cuboid/emphstyle] (0,\dimy,\dimz) -- (0,0,\dimz) -- (\dimx,0,\dimz) -- (\dimx,\dimy,\dimz);%
\draw[cuboid/emphstyle] (\dimx,\dimy,0) -- (\dimx,0,0) -- (\dimx,0,\dimz);%
\fi
\ifcuboidshade
\pgfmathsetmacro{\cstepx}{\dimx/\tikzcuboidkey{shadesamples}}
\pgfmathsetmacro{\cstepy}{\dimy/\tikzcuboidkey{shadesamples}}
\pgfmathsetmacro{\cstepz}{\dimz/\tikzcuboidkey{shadesamples}}
\foreach \s in {1,...,\tikzcuboidkey{shadesamples}}
{ \pgfmathsetmacro{\lows}{\s-1}
\pgfmathsetmacro{\cpercent}{(\lows)/(\tikzcuboidkey{shadesamples}-1)*100}
\fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\s*\cstepy,\dimz) -- (\s*\cstepx,\s*\cstepy,\dimz) -- (\s*\cstepx,0,\dimz) -- (\lows*\cstepx,0,\dimz) -- (\lows*\cstepx,\lows*\cstepy,\dimz) -- (0,\lows*\cstepy,\dimz) -- cycle;
\fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,\lows*\cstepz) -- (0,\dimy,\lows*\cstepz) -- cycle;
\fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (\dimx,0,\s*\cstepz) -- (\dimx,\s*\cstepy,\s*\cstepz) -- (\dimx,\s*\cstepy,0) -- (\dimx,\lows*\cstepy,0) -- (\dimx,\lows*\cstepy,\lows*\cstepz) -- (\dimx,0,\lows*\cstepz) -- cycle;
}
\fi
\end{scope}
}
\makeatother
\tikzcuboid{dimx=1,dimy=20,dimz=30,shiftx=100};
\tikzcuboid{dimx=1,dimy=3,dimz=3,shiftx=150,,shifty=-99,front/.style={draw=blue!75!black,fill=languidlavender!25!white},top/.style={draw=blue!75!black,fill=languidlavender!25!white},right/.style={draw=blue!75!black,fill=languidlavender!25!white}};
\tikzcuboid{dimx=1,dimy=3,dimz=3,shiftx=180,,shifty=-69,front/.style={draw=skymagenta!75!white,fill=wisteria!25!white},top/.style={draw=skymagenta!75!skymagenta,fill=wisteria!25!white},right/.style={draw=skymagenta!75!skymagenta,fill=wisteria!25!white}};
\path[laser beam=ceruleanblue] (-2.61,-0.396) -- (4.7,-3.2);
\path[laser beam=ceruleanblue] (-2.61,-1.3) -- (4.7,-4.1);
\path[laser beam=ceruleanblue] (-1.91,-0.7) -- (5.6,-3.5);
\path[laser beam=ceruleanblue] (-1.9,0.2) -- (5.2,-2.6);
\tikzcuboid{dimx=1,dimy=3,dimz=3,shiftx=-63.54,shifty=-19.37,front/.style={draw=blue!75!black,fill=tan!25!white},top/.style={draw=blue!75!black,fill=tan!25!white},right/.style={draw=blue!75!black,fill=tan!25!white}};
更新:我认为命名立方体的边缘将是第一步,因为这样线条将更加标准化。让我试试。
答案1
您需要按照正确的顺序绘制事物。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{ifthen}
\definecolor{babyblue}{rgb}{0.54, 0.81, 0.94}
\definecolor{carolinablue}{rgb}{0.6, 0.73, 0.89}
\definecolor{ceruleanblue}{rgb}{0.16, 0.32, 0.75}
\definecolor{languidlavender}{rgb}{0.84, 0.79, 0.87}
\definecolor{tan}{rgb}{0.82, 0.71, 0.55}
\definecolor{tearose(rose)}{rgb}{0.96, 0.76, 0.76}
\definecolor{wisteria}{rgb}{0.79, 0.63, 0.86}
\definecolor{skymagenta}{rgb}{0.81, 0.44, 0.69}
% from https://tex.stackexchange.com/a/29882/121799
% and https://tex.stackexchange.com/a/387530/121799
%-------------------
\newif\ifcuboidshade
\newif\ifcuboidemphedge
\tikzset{
cuboid/.is family,
cuboid,
shiftx/.initial=0,
shifty/.initial=0,
dimx/.initial=3,
dimy/.initial=3,
dimz/.initial=3,
scale/.initial=1,
densityx/.initial=1,
densityy/.initial=1,
densityz/.initial=1,
rotation/.initial=0,
anglex/.initial=0,
angley/.initial=90,
anglez/.initial=225,
scalex/.initial=1,
scaley/.initial=1,
scalez/.initial=0.5,
front/.style={draw=black,fill=white},
top/.style={draw=black,fill=white},
right/.style={draw=black,fill=white},
shade/.is if=cuboidshade,
shadecolordark/.initial=black,
shadecolorlight/.initial=white,
shadeopacity/.initial=0.15,
shadesamples/.initial=16,
emphedge/.is if=cuboidemphedge,
emphstyle/.style={thick},
}
\newcommand{\tikzcuboidkey}[1]{\pgfkeysvalueof{/tikz/cuboid/#1}}
% Commands
\newcommand{\tikzcuboid}[1]{
\tikzset{cuboid,#1} % Process Keys passed to command
\pgfmathsetlengthmacro{\vectorxx}{\tikzcuboidkey{scalex}*cos(\tikzcuboidkey{anglex})*28.452756}
\pgfmathsetlengthmacro{\vectorxy}{\tikzcuboidkey{scalex}*sin(\tikzcuboidkey{anglex})*28.452756}
\pgfmathsetlengthmacro{\vectoryx}{\tikzcuboidkey{scaley}*cos(\tikzcuboidkey{angley})*28.452756}
\pgfmathsetlengthmacro{\vectoryy}{\tikzcuboidkey{scaley}*sin(\tikzcuboidkey{angley})*28.452756}
\pgfmathsetlengthmacro{\vectorzx}{\tikzcuboidkey{scalez}*cos(\tikzcuboidkey{anglez})*28.452756}
\pgfmathsetlengthmacro{\vectorzy}{\tikzcuboidkey{scalez}*sin(\tikzcuboidkey{anglez})*28.452756}
\begin{scope}[xshift=\tikzcuboidkey{shiftx}, yshift=\tikzcuboidkey{shifty}, scale=\tikzcuboidkey{scale}, rotate=\tikzcuboidkey{rotation}, x={(\vectorxx,\vectorxy)}, y={(\vectoryx,\vectoryy)}, z={(\vectorzx,\vectorzy)}]
\pgfmathsetmacro{\steppingx}{1/\tikzcuboidkey{densityx}}
\pgfmathsetmacro{\steppingy}{1/\tikzcuboidkey{densityy}}
\pgfmathsetmacro{\steppingz}{1/\tikzcuboidkey{densityz}}
\newcommand{\dimx}{\tikzcuboidkey{dimx}}
\newcommand{\dimy}{\tikzcuboidkey{dimy}}
\newcommand{\dimz}{\tikzcuboidkey{dimz}}
\pgfmathsetmacro{\secondx}{2*\steppingx}
\pgfmathsetmacro{\secondy}{2*\steppingy}
\pgfmathsetmacro{\secondz}{2*\steppingz}
\ifthenelse{\equal{\dimx}{1}}
{\foreach \x in {\steppingx,...,\dimx}}
{\foreach \x in {\steppingx,\secondx,...,\dimx}}
{ \ifthenelse{\equal{\dimy}{1}}
{\foreach \y in {\steppingy,...,\dimy}}
{\foreach \y in {\steppingy,\secondy,...,\dimy}}
{ \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
\pgfmathsetmacro{\lowy}{(\y-\steppingy)}
\filldraw[cuboid/front] (\lowx,\lowy,\dimz) -- (\lowx,\y,\dimz) -- (\x,\y,\dimz) -- (\x,\lowy,\dimz) -- cycle;
}
}
\ifthenelse{\equal{\dimx}{1}}
{\foreach \x in {\steppingx,...,\dimx}}
{\foreach \x in {\steppingx,\secondx,...,\dimx}}
{ \ifthenelse{\equal{\dimz}{1}}
{\foreach \z in {\steppingz,...,\dimz}}
{\foreach \z in {\steppingz,\secondz,...,\dimz}}
{ \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
\pgfmathsetmacro{\lowz}{(\z-\steppingz)}
\filldraw[cuboid/top] (\lowx,\dimy,\lowz) -- (\lowx,\dimy,\z) -- (\x,\dimy,\z) -- (\x,\dimy,\lowz) -- cycle;
}
}
\ifthenelse{\equal{\dimy}{1}}
{\foreach \y in {\steppingy,...,\dimy}}
{\foreach \y in {\steppingy,\secondy,...,\dimy}}
{ \ifthenelse{\equal{\dimz}{1}}
{\foreach \z in {\steppingz,...,\dimz}}
{\foreach \z in {\steppingz,\secondz,...,\dimz}}
{ \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
\pgfmathsetmacro{\lowz}{(\z-\steppingz)}
\filldraw[cuboid/right] (\dimx,\lowy,\lowz) -- (\dimx,\lowy,\z) -- (\dimx,\y,\z) -- (\dimx,\y,\lowz) -- cycle;
}
}
\ifcuboidemphedge
\draw[cuboid/emphstyle] (0,\dimy,0) -- (\dimx,\dimy,0) -- (\dimx,\dimy,\dimz) -- (0,\dimy,\dimz) -- cycle;%
\draw[cuboid/emphstyle] (0,\dimy,\dimz) -- (0,0,\dimz) -- (\dimx,0,\dimz) -- (\dimx,\dimy,\dimz);%
\draw[cuboid/emphstyle] (\dimx,\dimy,0) -- (\dimx,0,0) -- (\dimx,0,\dimz);%
\fi
\ifcuboidshade
\pgfmathsetmacro{\cstepx}{\dimx/\tikzcuboidkey{shadesamples}}
\pgfmathsetmacro{\cstepy}{\dimy/\tikzcuboidkey{shadesamples}}
\pgfmathsetmacro{\cstepz}{\dimz/\tikzcuboidkey{shadesamples}}
\foreach \s in {1,...,\tikzcuboidkey{shadesamples}}
{ \pgfmathsetmacro{\lows}{\s-1}
\pgfmathsetmacro{\cpercent}{(\lows)/(\tikzcuboidkey{shadesamples}-1)*100}
\fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\s*\cstepy,\dimz) -- (\s*\cstepx,\s*\cstepy,\dimz) -- (\s*\cstepx,0,\dimz) -- (\lows*\cstepx,0,\dimz) -- (\lows*\cstepx,\lows*\cstepy,\dimz) -- (0,\lows*\cstepy,\dimz) -- cycle;
\fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,\lows*\cstepz) -- (0,\dimy,\lows*\cstepz) -- cycle;
\fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (\dimx,0,\s*\cstepz) -- (\dimx,\s*\cstepy,\s*\cstepz) -- (\dimx,\s*\cstepy,0) -- (\dimx,\lows*\cstepy,0) -- (\dimx,\lows*\cstepy,\lows*\cstepz) -- (\dimx,0,\lows*\cstepz) -- cycle;
}
\fi
\end{scope}
}
\makeatother
% from https://tex.stackexchange.com/a/80207/121799
\tikzset{
laser beam action/.style={
line width=\pgflinewidth+.2pt,draw opacity=.1,draw=#1,
},
laser beam recurs/.code 2 args={%
\pgfmathtruncatemacro{\level}{#1-1}%
\ifthenelse{\equal{\level}{0}}%
{\tikzset{preaction={laser beam action=#2}}}%
{\tikzset{preaction={laser beam action=#2,laser beam recurs={\level}{#2}}}}
},
laser beam/.style={preaction={laser beam recurs={10}{#1}},draw opacity=1,draw=#1},
}
\begin{document}
\begin{tikzpicture}[scale=0.3]
\tikzcuboid{dimx=1,dimy=20,dimz=30,shiftx=100,shade,shadeopacity=0.35};
\tikzcuboid{dimx=1,dimy=3,dimz=3,shiftx=-63.54,shifty=-19.37,front/.style={draw=blue!75!black,fill=tan!25!white},top/.style={draw=blue!75!black,fill=tan!25!white},right/.style={draw=blue!75!black,fill=tan!25!white},shade,%
shadeopacity=0.35};
\path[laser beam=ceruleanblue] (-2.61,-0.396) -- (4.7,-3.2);
\path[laser beam=ceruleanblue] (-2.61,-1.3) -- (4.7,-4.1);
\path[laser beam=ceruleanblue] (-1.91,-0.7) -- (5.6,-3.5);
\path[laser beam=ceruleanblue] (-1.9,0.2) -- (5.2,-2.6);
\tikzcuboid{dimx=1,dimy=3,dimz=3,shiftx=150,,shifty=-99,front/.style={draw=blue!75!black,fill=languidlavender!25!white},top/.style={draw=blue!75!black,fill=languidlavender!25!white},right/.style={draw=blue!75!black,fill=languidlavender!25!white},
shade,shadeopacity=0.35};
\tikzcuboid{dimx=1,dimy=3,dimz=3,shiftx=180,,shifty=-69,front/.style={draw=skymagenta!75!white,fill=wisteria!25!white},top/.style={draw=skymagenta!75!skymagenta,fill=wisteria!25!white},right/.style={draw=skymagenta!75!skymagenta,fill=wisteria!25!white},
shade,shadeopacity=0.35};
\end{tikzpicture}
\end{document}