我怎样才能减少代码来绘制这个多面体?

我怎样才能减少代码来绘制这个多面体?

我正在尝试画这幅画 在此处输入图片描述 我的代码

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz,tikz-3dplot} 
\usepackage{fouriernc}
\usetikzlibrary{folding}
\begin{document}
\tdplotsetmaincoords{60}{135}
    \begin{tikzpicture}[tdplot_main_coords,scale=1,line join = round, line cap = round]
\pgfmathsetmacro\a{4}
\pgfmathsetmacro{\b}{\a/2}
\path
coordinate (A) at (\b,0,-\a) 
coordinate (B) at (0,\b,-\a) 
coordinate (C) at (-\b,0,-\a)
coordinate (D) at (0,-\b,-\a)
coordinate (I) at (\b,\a,0) 
coordinate (J) at (0,\a,-\b)
coordinate (K) at (-\b,\a,0) 
coordinate (L) at (0,\a,\b)
coordinate (P) at (\a,-\b,0) 
coordinate (Q) at (\a,0,-\b)
coordinate (R) at (\a,\b,0) 
coordinate (S) at (\a,0,\b)

coordinate (A') at ($(A)+ (0,0,2*\a)$) 
coordinate (B') at ($(B)+ (0,0,2*\a)$)  
coordinate (C') at ($(C)+ (0,0,2*\a)$) 
coordinate (D') at ($(D)+ (0,0,2*\a)$)

coordinate (I') at ($(I)+ (0,-2*\a,0)$) 
coordinate (J') at ($(J)+ (0,-2*\a,0)$)  
coordinate (K') at ($(K)+ (0,-2*\a,0)$) 
coordinate (L') at ($(L)+ (0,-2*\a,0)$)

coordinate (P') at ($(P)+ (-2*\a,0,0)$)
coordinate (Q') at ($(Q)+ (-2*\a,0,0)$)
coordinate (R') at ($(R)+ (-2*\a,0,0)$)
coordinate (S') at ($(S)+ (-2*\a,0,0)$);

\draw [ultra thick] 
(A) --(B)--(J)--(I) -- (R) -- (Q) --cycle
(I) --(J)--(K) --(L)--cycle
(K) --(L)--(B') --(C')--(S')--(R')--cycle 
(A') --(B')--(C')--(D') --cycle
(P) --(Q)--(R)--(S) --cycle
(S) --(P)--(I')--(L')--(D') --(A')--cycle
;

\draw [dashed]  (A) --(D) -- (C) --(B)
(C) -- (Q') -- (P') --(K')--(J') --(D)
(J') --(I') (K')-- (L') (P') --(S') (Q') --(R')

;
\foreach \point/\position in {A/below,B/below,C/below,D/left,I/below,J/below,K/right,L/above,P/right,Q/above,R/left,S/below,I'/below,J'/below,K'/right,L'/above,P'/right,Q'/above,R'/left,S'/below,A'/below,B'/below,C'/below,D'/left}
{\fill (\point) circle (0.0pt);
    %\node[\position=2pt] at (\point) {$\point$};
}

\end{tikzpicture}
\end{document}

我怎样才能减少我的代码?

答案1

您可以使用围绕垂直轴的四次旋转和一个来减少坐标定义的代码\foreach。(为了节省两行,我直接将正弦和余弦值提供给 foreach,因为它们只有 -1、0 和 +1。)

不幸的是,我不知道一种简单(且自动)的方法来判断边缘是否应该是虚线,所以我手工画出了每一条边缘......

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz,tikz-3dplot} 
\begin{document}

\tdplotsetmaincoords{60}{135}
\pgfmathsetmacro\a{4}
\pgfmathsetmacro{\b}{\a/2}    
\begin{tikzpicture}[tdplot_main_coords]

%% Coordinate definitions
\foreach \n/\vcos/\vsin in { 0/1/0, 1/0/1, 2/-1/0, 3/0/-1 }{
  \coordinate (A\n) at (\b*\vcos          , \b*\vsin          , -\a);
  \coordinate (B\n) at (\a*\vcos          , \a*\vsin          , -\b);
  \coordinate (C\n) at (\a                ,       \b          , 0  );
  \coordinate (C\n) at (\a*\vcos-\b*\vsin , \vcos*\b+\a*\vsin , 0  );
  \coordinate (D\n) at (\a*\vcos+\vsin*\b , \a*\vsin-\vcos*\b , 0  );
  \coordinate (E\n) at (\a*\vcos          , \vsin*\a          , \b );
  \coordinate (F\n) at (\b*\vcos          , \b*\vsin          , \a );
}

%% Edge with thick and dashed
\draw[ultra thick] (F0) edge(E0) -- (F1) edge (E1) -- (F2) edge (E2)
-- (F3) edge (E3) -- (F0) (E0) edge (D0) -- (C0) edge (D1) -- (B0) -- (A0)
-- (A1) -- (B1) -- (D1) -- (E1) -- (C1) edge (B1) -- (D2) -- (E2)
(E3) -- (C3) -- (D0) -- (B0);
\draw[dashed] (A1) -- (A2) -- (A3) edge (A0) -- (B3) edge (C3)
-- (D3) edge (E3) -- (C2) edge (E2)--(B2) edge (D2)--(A2);

\end{tikzpicture}
\end{document}

答案2

只是为了好玩。这会在各种旋转坐标中绘制六边形。如果没有讨厌的虚线,这在原则上就足够了。要使某些边缘不 (!) 虚线,这需要一些额外的努力(代码的最后一部分)。

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{tikz-3dplot} 
\usetikzlibrary{calc}
\tikzset{hexagon defined by/.style args={#1 and #2 with label #3}{insert path={
 -- ++ ($-1*#2$) coordinate (#3-2) 
-- ++ #1 coordinate (#3-3) --++ ($#1+#2$) coordinate (#3-4) 
-- ++ #2 coordinate (#3-5) -- ++ ($-1*#1$) coordinate (#3-6) 
-- ++ ($-1*#1-1*#2$) coordinate (#3-1)
}}}
\begin{document}
\tdplotsetmaincoords{60}{135}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line join = round, line cap = round]
 \pgfmathsetmacro\a{4}
 \pgfmathsetmacro{\b}{\a/2}
 \foreach \X/\Y/\Z [count=\NN] in {0/0/0,%back
 90/90/-90,% back bottom
 -30/0/-60,%back upper left
 30/0/60,%back upper right
 0/00/180,% top (first visible)
 90/90/0,%front lower right
 -90/90/00,%front lower left
 90/180/0%front
 }
 {\tdplotsetrotatedcoords{\X}{\Y}{\Z}
 \begin{scope}[tdplot_rotated_coords]
  \ifnum\NN<5
   \draw[very thick,dashed,fill opacity=0.5] (\a,0,\b) 
   [hexagon defined by={(\b-\a,\a-\b,0)} and {(0,-\b,\b)} with label H\NN];
  \else
   \draw[very thick,fill=white,fill opacity=0.5] (\a,0,\b) 
   [hexagon defined by={(\b-\a,\a-\b,0)} and {(0,-\b,\b)} with label H\NN];
  \fi 
 \end{scope}}
 \draw[very thick,fill=white,fill opacity=0.5] (H5-4) -- (H7-5) -- (H7-6) -- (H5-3) --cycle
 (H5-1) -- (H6-4) -- (H6-3) -- (H5-2) --cycle
 (H7-1) -- (H6-2) -- (H6-1) -- (H7-2) --cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

而且由于虚线图案不一定匹配,遗憾的是有些虚线不太好看,因为有些线被画了两次。

如果你不坚持使用虚线(但对隐藏线被不透明表面覆盖感到满意),那么你可以这样做

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{tikz-3dplot} 
\usetikzlibrary{calc}
\tikzset{hexagon defined by/.style args={#1 and #2 with label #3}{insert path={
 -- ++ ($-1*#2$) coordinate (#3-2) 
-- ++ #1 coordinate (#3-3) --++ ($#1+#2$) coordinate (#3-4) 
-- ++ #2 coordinate (#3-5) -- ++ ($-1*#1$) coordinate (#3-6) 
-- ++ ($-1*#1-1*#2$) coordinate (#3-1)
}}}
\begin{document}
\tdplotsetmaincoords{60}{135}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line join = round, line cap = round]
 \pgfmathsetmacro\a{4}
 \pgfmathsetmacro{\b}{\a/2}
 \foreach \X/\Y/\Z [count=\NN] in {0/0/0,%back
 90/90/-90,% back bottom
 -30/0/-60,%back upper left
 30/0/60,%back upper right
 0/00/180,% top (first visible)
 90/90/0,%front lower right
 -90/90/00,%front lower left
 90/180/0%front
 }
 {\tdplotsetrotatedcoords{\X}{\Y}{\Z}
 \begin{scope}[tdplot_rotated_coords]
   \draw[very thick,fill=white,fill opacity=0.7] (\a,0,\b) 
   [hexagon defined by={(\b-\a,\a-\b,0)} and {(0,-\b,\b)} with label H\NN];
 \end{scope}}
  \draw[very thick,fill=white,fill opacity=0.7] (H5-4) -- (H7-5) -- (H7-6) -- (H5-3) --cycle
 (H5-1) -- (H6-4) -- (H6-3) -- (H5-2) --cycle
 (H7-1) -- (H6-2) -- (H6-1) -- (H7-2) --cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

原则上,这种方法可以自动确定哪些行是隐藏的,我不想在这里继续讨论,因为这篇文章有一个很好的被接受的答案,值得保留为被接受的答案。

相关内容