我正在尝试通过 2D 创建一个六角锥体,不是使用3d-plot
库。
我希望实现以下目标:
到目前为止我已取得以下成果:
我发现连接角落时有很多间隙。我该如何填补/消除间隙。
hex1
另外,我该如何制作虚线的前半部分呢?
这是我的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\tikzset{hexagon/.style = {
regular polygon,
regular polygon sides = 6,
minimum width = 2cm,
draw
}
}
\begin{document}
\begin{tikzpicture}
\node[hexagon,xscale=4,fill=gray!50] (hex1) at (0,0) {};
\node[above=5cm of hex1,hexagon,xscale=3,fill=gray!10,ultra thick,draw=gray!40] (hex2) {};
\draw (hex1.corner 3) -- (hex2.corner 3);
\draw (hex1.corner 4) -- (hex2.corner 4);
\end{tikzpicture}
\end{document}
答案1
只需添加outer sep=0pt
到六边形样式定义:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,
shapes.geometric}
\tikzset{hexagon/.style = {
regular polygon,
regular polygon sides = 6,
minimum width = 2cm,
outer sep=0pt, % <---
fill=#1} %% <---
}
\begin{document}
\begin{tikzpicture}
\node[hexagon=gray!50, xscale=4] (hex1) at (0,0) {};
\node[hexagon=gray!10, xscale=3, draw=gray, thick,
above=5cm of hex1] (hex2) {};
\foreach \i in {1,2}
\draw[dashed] (hex1.corner \i) -- (hex2.corner \i); %% <---
\path[dashed,thick,draw=gray] %% <---
(hex1.corner 6) -- (hex1.corner 1) --
(hex1.corner 2) -- (hex1.corner 3);
\foreach \i in {3,...,6}
\draw (hex1.corner \i) -- (hex2.corner \i); %% <---
\path[thick,draw=gray] %% <---
(hex1.corner 3) -- (hex1.corner 4) --
(hex1.corner 5) -- (hex1.corner 6);
\end{tikzpicture}
\end{document}
编辑:添加了第二个子问题的答案......
答案2
这是另一种简单的方法元帖子,仅供比较。
我认为稍微旋转一下看起来更专业一些,但如果你愿意的话也可以不旋转。请注意,在这个图中,填充和绘制的顺序很重要。如果你想要真正的 3D,我建议渐近线。
这是源代码。(用 编译lualatex
)。
\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
path base, roof;
base = (
for i=0 upto 5:
89 right rotated 60i --
endfor cycle
) rotated 8 yscaled 13/34;
roof = base scaled 5/8 shifted 89 up;
fill base withcolor 7/8 white;
draw subpath (0, 3) of base dashed evenly;
draw subpath (3, 6) of base;
fill roof withcolor 15/16 white;
for i=1 upto 2:
draw point i of base -- point i of roof dashed evenly;
endfor
for i=3 upto 6:
draw point i of base -- point i of roof;
endfor
draw roof;
endfig;
\end{mplibcode}
\end{document}
答案3
这是一种替代方法,您可以调整视角等。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{30}{00}
\begin{tikzpicture}[tdplot_main_coords,declare function={r1=3;r2=2.25;h=4;}]
\path[fill=gray!50] plot[variable=\x,samples at={0,60,...,300}]
({r1*cos(\x)},0,{r1*sin(\x)});
\draw[thick,dashed] plot[variable=\x,samples at={0,60,120,180}] ({r1*cos(\x)},0,{r1*sin(\x)});
\draw[thick] plot[variable=\x,samples at={180,240,300,0}] ({r1*cos(\x)},0,{r1*sin(\x)});
\foreach \X in {60,120,...,360}
{\draw[thick] \ifnum\X<180 [dashed] \fi ({r1*cos(\X)},0,{r1*sin(\X)}) -- ({r2*cos(\X)},h,{r2*sin(\X)});}
\path[fill=gray!25,fill opacity=0.8] plot[variable=\x,samples at={0,60,...,300}]
({r2*cos(\x)},h,{r2*sin(\x)});
\draw[thick,dashed,gray!40] plot[variable=\x,samples at={0,60,120,180}]
({r2*cos(\x)},h,{r2*sin(\x)});
\draw[thick,gray!40] plot[variable=\x,samples at={180,240,300,0}]
({r2*cos(\x)},h,{r2*sin(\x)});
\end{tikzpicture}
\end{document}