优化(或至少改进)PSTricks 代码以绘制蜂巢

优化(或至少改进)PSTricks 代码以绘制蜂巢

考虑以下示例。

代码

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage{
  auto-pst-pdf,
  pst-eucl
}
\usepackage{siunitx}

\begin{document}

\begin{figure}
\def\indryk{5}
\def\bredde{60}
\pstFPdiv\konstA{2}{3}
\pstFPmul\Bb{\konstA}{\bredde}
\pstFPdiv\konstB{6}{5}
\pstFPmul\Ha{\konstB}{\bredde}
\pstFPdiv\konstC{13}{20}
\pstFPmul\Hb{\konstC}{\bredde}
\pstFPdiv\konstD{7}{5}
\pstFPmul\Hc{\konstD}{\bredde}
\pstFPdiv\radiusA{\bredde}{20}
\pstFPdiv\LabelSeparation{\bredde}{12}
 \centering
  \sisetup{
    round-mode=places,
    round-precision=0
  }
  \psset{
    unit=0.13cm
  }
  \begin{pspicture}(8,10)
   % Bistadet.
   \pnode(\indryk,\indryk){A}
   \pnode(!\indryk\space \indryk\space 13 20 div \bredde\space mul add){B1}
   \pnode(!\indryk\space \indryk\space 16 20 div \bredde\space mul add){B2}
   \pnode(!\indryk\space \indryk\space 19 20 div \bredde\space mul add){B3}
   \pnode(!\indryk\space \indryk\space 22 20 div \bredde\space mul add){B4}
   \pnode(!\indryk\space \indryk\space 25 20 div \bredde\space mul add){B5}
   \pnode(!\indryk\space \indryk\space 28 20 div \bredde\space mul add){B6}
   \pnode(!\indryk\space \bredde\space 2 div add \indryk\space 24 5 3 sqrt mul sub 10 div \bredde\space mul add){C}
   \pnode(!\indryk\space \bredde\space add \indryk\space 28 20 div \bredde\space mul add){D6}
   \pnode(!\indryk\space \bredde\space add \indryk\space 25 20 div \bredde\space mul add){D5}
   \pnode(!\indryk\space \bredde\space add \indryk\space 22 20 div \bredde\space mul add){D4}
   \pnode(!\indryk\space \bredde\space add \indryk\space 19 20 div \bredde\space mul add){D3}
   \pnode(!\indryk\space \bredde\space add \indryk\space 16 20 div \bredde\space mul add){D2}
   \pnode(!\indryk\space \bredde\space add \indryk\space 13 20 div \bredde\space mul add){D1}
   \pnode(!\indryk\space \bredde\space add \indryk\space){E}
   \pnode(!\indryk\space 5 6 div \bredde\space mul add \indryk\space){F}
   \pnode(!\indryk\space 1 6 div \bredde\space mul add \indryk\space){G}
   \pnode(!\indryk\space 1 6 div \bredde\space mul add \indryk\space  4 20 div \bredde\space mul add){H1}
   \pnode(!\indryk\space 1 6 div \bredde\space mul add \indryk\space  7 20 div \bredde\space mul add){H2}
   \pnode(!\indryk\space 1 6 div \bredde\space mul add \indryk\space 10 20 div \bredde\space mul add){H3}
   \pnode(!\indryk\space 1 6 div \bredde\space mul add \indryk\space 13 20 div \bredde\space mul add){H4}
   \pnode(!\indryk\space 5 6 div \bredde\space mul add \indryk\space  4 20 div \bredde\space mul add){I1}
   \pnode(!\indryk\space 5 6 div \bredde\space mul add \indryk\space  7 20 div \bredde\space mul add){I2}
   \pnode(!\indryk\space 5 6 div \bredde\space mul add \indryk\space 10 20 div \bredde\space mul add){I3}
   \pnode(!\indryk\space 5 6 div \bredde\space mul add \indryk\space 13 20 div \bredde\space mul add){I4}
   \pspolygon(A)(B6)(C)(D6)(E)
   \multido{\iB=1+1,\iD=1+1}{6}{\pcline(B\iB)(D\iD)}
   \pcline(G)(H4)
   \pcline(F)(I4)
   \multido{\iH=1+1,\iI=1+1}{4}{\pcline(H\iH)(I\iI)}
   % Betegnelser.
  \psset{
    nrot=:U
  }
   \pnode(!0 1 5 div \bredde\space mul \indryk\space add){J}
   \pnode(!0 7 5 div \bredde\space mul \indryk\space add){K}
   \pcline{|-|}(J)(K)
   \ncput*{\SI{\Ha}{\cm}}
   \pnode(\indryk,0){L}
   \pnode(!\indryk\space \bredde\space add 0){M}
   \pcline{|-|}(L)(M)
   \ncput*{\SI{\bredde}{\cm}}
   \pcline{|<->|}(G)(F)
   \ncput*{\SI{\Bb}{\cm}}
   \pnode(!\indryk\space 2 mul \bredde\space add \indryk\space){N}
   \pnode(!\indryk\space 2 mul \bredde\space add \indryk\space 13 20 div \bredde\space mul add){O}
   \pcline{|-|}(N)(O)
   \ncput*{\SI{\Hb}{\cm}}
   \pnode(!\indryk\space 3 mul \bredde\space add \indryk\space){P}
   \pnode(!\indryk\space 3 mul \bredde\space add \indryk\space 7 5 div \bredde\space mul add){Q}
   \pcline{|-|}(P)(Q)
   \ncput*{\SI{\Hc}{\cm}}
   \pstMarkAngle[
     MarkAngleRadius=\radiusA,
     LabelSep=\LabelSeparation
   ]{B6}{C}{D6}{\footnotesize{\SI{150}{\degree}}}
  \end{pspicture}
\end{figure}

\end{document}

输出

在此处输入图片描述

问题

我有想要的输出,但是代码相当混乱;有人能帮我简化一下吗?

答案1

如果您需要宽度/高度作为变量,则请更改它并用于\numexpr计算。

\documentclass{article}
\usepackage[pdf]{pstricks}% pdflatex -shell-escape test.tex
\usepackage{pst-node,multido}
\begin{document}

\psset{unit=1mm,dimen=middle}
\begin{pspicture}(-10,-10)(80,100)
\psframe(60,84)
\psframe(10,0)(50,39)
\multido{\iA=12+9}{3}{\psline(10,\iA)(50,\iA)}
\multido{\iA=39+9}{5}{\psline(0,\iA)(60,\iA)}
\psline(0,84)(!30 15 Tan 30 mul 84 add)(60,84) 
\psarc(!30 15 Tan 30 mul 84 add){3}{195}{345}
\uput{5}[-90](!30 15 Tan 30 mul 84 add){$150^\circ$}
\pcline             {<->}  (10,0)(50,0)\ncput*{40\,cm}
\pcline[offset=-5 ]{|<->|}(0,0)(60,0)  \ncput*{60\,cm}
\pcline[offset=-5 ]{|<->|}(60,0)(60,39)\ncput*[nrot=:U]{39\,cm}
\pcline[offset=-10]{|<->|}(60,0)(60,84)\ncput*[nrot=:U]{84\,cm}
\pcline[offset=5  ]{|<->|}(0,12)(0,84) \ncput*[nrot=:U]{72\,cm}
\end{pspicture}

\end{document}

在此处输入图片描述

答案2

简化现有答案,在可读性不变的情况下,节省 115 次击键。使用siunitx使我们的生活保持一致。

在此处输入图片描述

\documentclass[pstricks]{standalone}
\usepackage{pst-node,multido,siunitx}
\psset{unit=1mm,dimen=middle,linejoin=1}

\def\Label#1{\ncput*{\SI{#1}{\cm}}}

\begin{document}
\begin{pspicture}(-10,-10)(80,100)
\multips(0,75)(0,-9){8}{\psframe(60,9)}
\multips(0,0)(50,0){2}{\psframe[fillstyle=solid](10,39)}
\pnode[!0 15 Tan 30 mul](30,84){T}
\pspolygon(0,84)(T)(60,84)
\psarcn(T){3}{-15}{-165}
\uput{4}[-90](T){\ang{150}}
\pcline{<->}(10,0)(50,0)\Label{40}
\psset{arrows=|<*->|*}
\pcline[offset=-5 ](0,0)(60,0)\Label{60}
\psset{nrot=:U}
\pcline[offset=-5 ](60,0)(60,39)\Label{39}
\pcline[offset=-10](60,0)(60,84)\Label{84}
\pcline[offset=5  ](0,12)(0,84) \Label{72}
\end{pspicture}
\end{document}

相关内容