改进 PSTricks 代码以绘制奥林匹克旗帜

改进 PSTricks 代码以绘制奥林匹克旗帜

考虑以下示例。

代码

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage{auto-pst-pdf,pstricks-add}
\usepackage[locale=DE]{siunitx}

\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

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

\begin{document}

\def\bredde{18 }
\def\laengde{\calc{1.5*\bredde} }
\def\radius{3.0 }
\def\afstand{6.5 }
\def\afstandB{\calc{\afstand/2}}
\centering
\psset{unit=0.44cm}
\begin{pspicture}(\laengde,\bredde)
  \psframe(0,0)(\laengde,\bredde)
  \pnodes{P}
    (!\laengde 2 div \afstand sub \bredde \radius add 2 div)%
    (!\laengde \afstand sub 2 div \bredde \radius sub 2 div)%
    (!\laengde 2 div \bredde \radius add 2 div)%
    (!\laengde \afstand add 2 div \bredde \radius sub 2 div)%
    (!\laengde 2 div \afstand add \bredde \radius add 2 div)
  \multido{\iA=0+1,\iB=-2+1}{5}{%
    \psdot(P\iA)
    \pscircle(P\iA){\radius}
    \psline[linestyle=dotted]%
      (!\laengde \iB\space \afstand mul add 2 div \bredde 2 div 5 3 div \radius mul sub)%
      (!\laengde \iB\space \afstand mul add 2 div \bredde 2 div 5 3 div \radius mul add)
  }
  \multido{\iD=-1+1}{2}{%
    \psline[linestyle=dotted]%
      (!\laengde 2 div 10 3 div \radius mul sub
        \bredde \radius add 2 div \iD\space \radius mul add)%
      (!\laengde 2 div 10 3 div \radius mul add
        \bredde \radius add 2 div \iD\space \radius mul add)
  }
 \psset{arrows=|*-|*,nrot=:U}
  \pcline[offset=-6pt]%
    (!\laengde 2 div \afstand sub \bredde 2 div 5 3 div \radius mul sub)%
    (!\laengde 2 div \bredde 2 div 5 3 div \radius mul sub)
  \Label{\afstand}
  \pcline[offset=6pt]%
    (!\laengde 2 div \afstand sub \bredde 2 div 5 3 div \radius mul add)%
    (!\laengde \afstand sub 2 div \bredde 2 div 5 3 div \radius mul add)
  \Label{\afstandB}
  \pcline[offset=6pt]%
    (!\laengde 2 div 10 3 div \radius mul sub \bredde \radius sub 2 div)%
    (!\laengde 2 div 10 3 div \radius mul sub \bredde \radius add 2 div)
  \Label{\radius}
 \psset{linestyle=none,arrows=none}
  \pcline[offset=-9pt](0,0)(0,\bredde)
  \Label{\bredde}
  \pcline[offset=9pt](0,0)(\laengde,0)
  \Label{\laengde}
\end{pspicture}

\end{document}

输出

在此处输入图片描述

问题

我得到了想要的输出,但代码不太优雅。我已经对代码进行了一些改进,但我认为它可以更好。

答案1

\documentclass{article}

\usepackage{auto-pst-pdf,pstricks-add}
\usepackage[locale=DE]{siunitx}
\newcommand\Label[2][:U]{\ncput*[nrot=#1]{\small\SI{#2}{\cm}}}
\begin{document}

\def\bredde{18 }
\edef\laengde{\the\numexpr3*\bredde/2\relax\space}
\def\radius{3.0 }
\def\afstand{6.5 }
\def\afstandB{3.25 }
\centering
\psset{unit=0.44cm}
\begin{pspicture}(\laengde,\bredde)
  \psframe(0,0)(\laengde,\bredde)
  \multido{\iA=1+1,\iB=-2+1}{5}{%
    \rput(!\laengde 2 div \bredde 2 div){% all relative to the center
      \pnode(!\iB\space \afstandB mul \radius 2 div \iA\space 2 mod 0 eq { neg } if ){P\iA}
      \psdot(P\iA)\pscircle(P\iA){\radius}
      \psline[linestyle=dotted](!\psGetNodeCenter{P\iA} P\iA.x \radius 1.5 mul 1 add)
                               (!\psGetNodeCenter{P\iA} P\iA.x \radius -1.5 mul 1 sub) }
  }
  \pcline[linestyle=dotted,nodesep=-\radius](P1)(P5)
  \pcline[linestyle=dotted,nodesep=-\radius,offset=-\radius](P1)(P5)

 \psset{arrows=|*-|*}
  \pcline(!\psGetNodeCenter{P1} P1.x P1.y \radius 2 mul sub 1 sub)%
         (!\psGetNodeCenter{P3} P3.x P3.y \radius 2 mul sub 1 sub)\Label{\afstand}
  \pcline(!\psGetNodeCenter{P1} P1.x P1.y \radius add 1 add)%
         (!\psGetNodeCenter{P2} P2.x P2.y \radius 2 mul add 1 add)\Label{\afstandB}
  \pcline(!\psGetNodeCenter{P2} P2.x \radius sub \afstandB sub 1 sub P2.y)%
         (!\psGetNodeCenter{P1} P1.x \radius sub 1 sub P1.y )\Label{\radius}
  \uput[90](!\laengde 2 div 0){\small\SI{\laengde}{\cm}}
  \uput[0]{90}(!0 \bredde 2 div){\small\SI{\bredde}{\cm}}
\end{pspicture}

\end{document}

如果您不想手动进行除法,请使用以下命令:

\psset{unit=0.44cm}
\def\bredde{18 }
\edef\laengde{\the\numexpr3*\bredde/2\relax\space}
\def\radius{3.0 }
\def\afstand{6.5 }
\makeatletter
\newlength\Lafstand \Lafstand=\afstand\p@
\newlength\LafstandB \LafstandB=0.5\Lafstand
\edef\afstand{\strip@pt\Lafstand\noexpand\space}
\edef\afstandB{\strip@pt\LafstandB\noexpand\space}
\makeatother

答案2

我相信这个解决方案比现有的解决方案简单得多。无需声明节点!:-)

在此处输入图片描述

\documentclass[pstricks]{standalone}
\usepackage{pst-node,multido}
\usepackage[locale=DE]{siunitx}


\def\Label#1{\ncput*[nrot=:U]{\large\SI{#1}{\cm}}}

\def\Inner{{%
\psset{xunit=3.25cm,yunit=3cm}
\begin{pspicture}(8,6)
    \bgroup
        \psset{linewidth=3pt}
        \multips(2,3.5)(2,0){3}{\qdisk(0,0){3pt}\pscircle{3cm}}
        \multips(3,2.5)(2,0){2}{\qdisk(0,0){3pt}\pscircle{3cm}}
    \egroup
    \bgroup
        \psset{linestyle=dotted}
        \multips(1,2.5)(0,1){2}{\psline(6,0)}
        \multirput(2,4.5)(1,0){5}{\pcline[nodesep=-.25cm](0,0)(0,-3)}
    \egroup
    \psset{arrows=|*-|*}
    \pcline[offset=-.6cm](2,1.5)(4,1.5)\Label{6.5}
    \pcline[offset=.6cm](2,4.5)(3,4.5)\Label{3.25}
    \pcline[offset=.35cm](1,2.5)(1,3.5)\Label{3.0}
\end{pspicture}}}

\begin{document}
\pspicture(27,18)
    \rput[bl](.5,0){\Inner}
    \psframe[linewidth=3pt](27,18)
    \psset{linestyle=none}
    \pcline[offset=-12pt](0,0)(0,18)\Label{18}
    \pcline[offset=12pt](0,0)(27,0)\Label{27}
\endpspicture
\end{document}

答案3

这是一个 TikZ 解决方案。

首先,从尺寸上看:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
 \definecolor{r1}{RGB}{0,129,188}
 \definecolor{r2}{RGB}{252,177,49}
 \definecolor{r3}{RGB}{35,34,35}
 \definecolor{r4}{RGB}{0,157,87}
 \definecolor{r5}{RGB}{218,30,38}
 \coordinate (r1) at (-6.5,1.5);
 \coordinate (r2) at (-3.25,-1.5);
 \coordinate (r3) at (0,1.5);
 \coordinate (r4) at (3.25,-1.5);
 \coordinate (r5) at (6.5,1.5);
 \pgfmathsetmacro\whitemargin{1mm}
 \pgfmathsetmacro\radiusout{3cm + .5*\whitemargin pt}
 \pgfmathsetmacro\radiusin{2.5cm - .5*\whitemargin pt}
 \begin{scope}
   \clip (-13.5,0) rectangle (13.5,9);
   \foreach \col in {r5,r4,r3,r2,r1}{
     \path[draw=white,line width=\whitemargin pt,fill=\col,even odd rule]
     (\col) circle (\radiusout pt) circle (\radiusin pt);
   }
 \end{scope}
 \begin{scope}
   \clip (-13.5,-9) rectangle (13.5,0);
   \foreach \col in {r1,r2,r3,r4,r5}{
     \path[draw=white,line width=\whitemargin pt,fill=\col,even odd rule]
     (\col) circle (\radiusout pt) circle (\radiusin pt);
   }
 \end{scope}

 \begin{scope}[on background layer]
   \begin{scope}[font=\Large,line width=.6mm,draw=gray,text=gray,fill=gray]
     \tikzset{
       dim/.style={fill=white,sloped},
       line/.style={|-|,shorten >=-.5\pgflinewidth,shorten <=-.5\pgflinewidth},
     }
     % flag dimensions
     \draw[<->] (-13.5, 8.5) -- node[dim]{27,0~cm} (13.5, 8.5);
     \draw[<->] (-13, -9) -- node[dim]{18,0~cm} (-13, 9);
     % ring centers
     \foreach \center in {r1,r2,r3,r4,r5}{\fill (\center) circle (1mm);}
     % ring dimensions
     \draw[dashed,line width=.3mm]
     (-10.5,0 |- r1) -- (10.5,0 |- r1)
     (-10.5,0 |- r2) -- (10.5,0 |- r2);
     \draw[line] (-10.5,0 |- r2) -- node[dim]{3,0~cm} (-10.5,0 |- r1);
     \foreach \center in {r1,r2,r3,r4,r5}{
       \draw[dashed,line width=.3mm] (\center |- 0,5) -- (\center |- 0,-5);
     }
     \draw[line] (r1 |- 0,5) -- node[dim]{3,25~cm} (r2 |- 0,5);
     \draw[line] (r1 |- 0,-5) -- node[dim]{6,5~cm} (r3 |- 0,-5);
   \end{scope}
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

无维度的第二个:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
 \definecolor{r1}{RGB}{0,129,188}
 \definecolor{r2}{RGB}{252,177,49}
 \definecolor{r3}{RGB}{35,34,35}
 \definecolor{r4}{RGB}{0,157,87}
 \definecolor{r5}{RGB}{218,30,38}
 \coordinate (r1) at (-6.5,1.5);
 \coordinate (r2) at (-3.25,-1.5);
 \coordinate (r3) at (0,1.5);
 \coordinate (r4) at (3.25,-1.5);
 \coordinate (r5) at (6.5,1.5);
 \pgfmathsetmacro\whitemargin{1mm}
 \pgfmathsetmacro\radiusout{3cm + .5*\whitemargin pt}
 \pgfmathsetmacro\radiusin{2.5cm - .5*\whitemargin pt}
 \begin{scope}
   \clip (-13.5,0) rectangle (13.5,9);
   \foreach \col in {r5,r4,r3,r2,r1}{
     \path[draw=white,line width=\whitemargin pt,fill=\col,even odd rule]
     (\col) circle (\radiusout pt) circle (\radiusin pt);
   }
 \end{scope}
 \begin{scope}
   \clip (-13.5,-9) rectangle (13.5,0);
   \foreach \col in {r1,r2,r3,r4,r5}{
     \path[draw=white,line width=\whitemargin pt,fill=\col,even odd rule]
     (\col) circle (\radiusout pt) circle (\radiusin pt);
   }
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容