![如何用 tikz 画出这样的图片](https://linux22.com/image/420565/%E5%A6%82%E4%BD%95%E7%94%A8%20tikz%20%E7%94%BB%E5%87%BA%E8%BF%99%E6%A0%B7%E7%9A%84%E5%9B%BE%E7%89%87.png)
有人可以帮我用 tikz 画这幅画吗?
代码
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick,scale=10]
%
\coordinate (C1) at (0.3,-0.15);
\coordinate (C2) at (0.55,-0.15);
\coordinate (C3) at (0.425,-0.025);
\coordinate (C4) at (0.425,-0.275);
%
\coordinate (D1) at (0.85,-0.05);
\coordinate (D2) at (0.9,0.05);
\coordinate (D3) at (0.875,0.125);
\coordinate (D4) at (0.875,-0.125);
%
\coordinate (E1) at (0.375,0.375);
\coordinate (E2) at (0.625,0.375);
\coordinate (E3) at (0.525,0.425);
\coordinate (E4) at (0.525,0.325);
%
\coordinate (F1) at (0.15,0.015);
\coordinate (F2) at (0.1,-0.015);
\coordinate (F3) at (0.125,0.125);
\coordinate (F4) at (0.125,-0.125);
%
\coordinate (G1) at (0.375,-0.375);
\coordinate (G2) at (0.625,-0.375);
\coordinate (G3) at (0.475,-0.425);
\coordinate (G4) at (0.575,-0.325);
\begin{scope}[thick,dashed]
\draw (C1) -- (C4) -- (C2) -- (C3) -- (C1);
\draw (D1) -- (D4) -- (D2) -- (D3) -- (D1);
\draw (E1) -- (E4) -- (E2) -- (E3) -- (E1);
\draw (F1) -- (F4) -- (F2) -- (F3) -- (F1);
\draw (G1) -- (G4) -- (G2) -- (G3) -- (G1);
\end{scope}
\draw (F3) -- (F2) -- (C1) -- (C3) -- (E4) -- (E1);
\draw (C1) -- (C4) -- (G3) -- (G1) -- (F4) -- (F2);
\draw (D1) -- (C2) -- (C3) -- (E4) -- (E2) -- (D3) -- (D1);
\draw (D1) -- (C2) -- (C4) -- (G3) -- (G2) --(D4) -- (D1);
\draw (E2) -- (E3) -- (E1) -- (F3) -- (F2) -- (F4) -- (G1) -- (G3) -- (G2) -- (D4) -- (D1) -- (D3) --cycle;
\end{tikzpicture}
\end{document}
答案1
与 TikZ 和 3d 一样:从后往前构建图形。(或者使用图层,但仍需要组织。)
- 立方体的背面
- 立方体的后边缘
- 背景中的钻石
- 立方体内部的六边形,但在背面
- 立方体内的轴
- 立方体内部的六边形,但在正面
- 前面的钻石
- 立方体的正面
- 立方体外部的轴
我选择不填充立方体的正面,因为那样会使整个图像变得模糊。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{3d,arrows.meta}
\begin{document}
\begin{tikzpicture}[
x=(-120:1cm*sin 30), y=(0:1cm), z=(90:1cm),
%
% h: half the height of the cube
% r: half the diagonal of the square sides that touch the cube
declare function={h=2; r=1;},
%
bg/.style={densely dotted},
fg/.style={solid},
axis bg/.style={densely dashed},
axis fg/.style={solid,-{Stealth[round,/tikz/fill opacity=1]}},
%
square fill/.style={fill=blue!50},
hex fill/.style ={fill=green!50},
cube fill/.style ={fill=red!50},
%
% debug/.style={shape=rectangle,font=\tiny,text=gray,inner sep=0pt, fill=white},
%
fill opacity=.5,
%
insert dia/.style={
insert path={(#1n) -- (#1e) -- (#1s) -- (#1w) -- cycle}},
]
% corners of the cube
\foreach \side/\sign in {back/-, front/+}
\coordinate (\side-nw) at (\sign h, -h, h)
coordinate (\side-ne) at (\sign h, h, h)
coordinate (\side-se) at (\sign h, h, -h)
coordinate (\side-sw) at (\sign h, -h, -h);
% corners of the squares/diamonds that touch the cube
\foreach \sign in {+,-}
\foreach \x/\y/\z in {x/y/z, y/x/z, z/y/x}
\coordinate[label={[shape=coordinate,debug/.try]:{\x\sign n}}]
(\x\sign n) at (xyz cs: \x=\sign h, \y= 0, \z= r)
coordinate[label={[shape=coordinate,debug/.try]:{\x\sign e}}]
(\x\sign e) at (xyz cs: \x=\sign h, \y= r, \z= 0)
coordinate[label={[shape=coordinate,debug/.try]:{\x\sign s}}]
(\x\sign s) at (xyz cs: \x=\sign h, \y= 0, \z=-r)
coordinate[label={[shape=coordinate,debug/.try]:{\x\sign w}}]
(\x\sign w) at (xyz cs: \x=\sign h, \y=-r, \z= 0)
coordinate[label={[shape=coordinate,debug/.try]:{\x\sign m}}]
(\x\sign m) at (xyz cs: \x=\sign h, \y= 0, \z= 0);
% 1: fill the cube's sides in the bg
\path[cube fill, bg] (back-nw) rectangle (back-se) [insert dia=x-]
{[canvas is xz plane at y=-h] (back-nw) rectangle (front-sw) [insert dia=y-]}
{[canvas is xy plane at z=-h] (back-sw) rectangle (front-se) [insert dia=z-]};
% 2: draw the lines of the cube in the bg
\draw[bg] (back-sw) -- (back-nw)
(back-sw) -- (back-se)
(back-sw) -- (front-sw);
% 3: fill the diamonds in the bg (no draw because this will done by the hexagons)
\foreach \x in {x,y,z}
\path[square fill, bg] [insert dia=\x-];
% 4: draw and fill the hexagons in the bg
\draw[hex fill, bg] (x-n) -- (z+s) -- (z+w) -- (y-n) -- (y-w) -- (x-w) -- cycle
(x-s) -- (z-s) -- (z-w) -- (y-s) -- (y-w) -- (x-w) -- cycle
(z+s) -- (z+e) -- (y+n) -- (y+w) -- (x-e) -- (x-n) -- cycle
(x-e) -- (x-s) -- (z-s) -- (z-e) -- (y+s) -- (y+w) -- cycle;
% 5: axes inside the cube
\foreach \x/\y/\z in {x/y/z, y/x/z, z/x/y}
\draw[axis bg] (0,0,0) -- (xyz cs: \x=h, \y=0, \z=0);
% 6: hexagons in the fg
\draw[hex fill, fg] (x+n) -- (z+n) -- (z+w) -- (y-n) -- (y-e) -- (x+w) -- cycle
(y-e) -- (y-s) -- (z-w) -- (z-n) -- (x+s) -- (x+w) -- cycle
(z+e) -- (y+n) -- (y+e) -- (x+e) -- (x+n) -- (z+n) -- cycle
(y+s) -- (z-e) -- (z-n) -- (x+s) -- (x+e) -- (y+e) -- cycle;
% 7: diamonds in the fg
\foreach \x in {x,y,z}
\draw[square fill, fg] [insert dia=\x+];
\draw[fg] (front-nw) rectangle (front-se) [insert dia=x+]
{[canvas is xz plane at y=h] (front-ne) rectangle (back-se) [insert dia=y+]}
{[canvas is xy plane at z=h] (back-nw) rectangle (front-ne) [insert dia=z+]};
% 9: the axes outside of the cube
\foreach \x/\y/\z in {x/y/z, y/x/z, z/x/y}
\draw[axis fg] (xyz cs: \x=h, \y=0, \z=0) -- (xyz cs: \x=h + 1.5, \y=0, \z=0);
\end{tikzpicture}
\end{document}