4

Consider the following code:

\documentclass{article}

\usepackage{pstricks-add}
\usepackage[locale=DE]{siunitx}

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

\def\ORb{\calculate{\SideLength/(2*sin(pi/\NoSides))-2.7}}
\def\ORh{\calculate{\SideLength/(2*sin(pi/\NoSides))-1.1}}
\def\area{\calculate{\NoSides*\SideLength^2*cot(pi/\NoSides)/4}}
\makeatletter
  \def\LabelLine{\@ifnextchar[\LabelLine@i{\LabelLine@i[0]}}
  \def\LabelLine@i[#1](#2)(#3)#4#5{%
    \pcline[linestyle=dashed,offset=#4]{|*-|*}(#2)(#3)
    \ncput*[nrot=#1]{#5}
  }
\makeatother

\def\polygon#1#2{%
 \def\NoSides{#1 }
 \def\SideLength{#2 }
 \psset{unit=0.75mm}
  \begin{pspicture}(-\ORb,-\ORh)(\ORb,\ORh)
   \edef\dAngle{\the\numexpr360/\NoSides}
   \pstVerb{%
     /R \SideLength 180 \NoSides div sin 2 mul div def
     /Q R \dAngle\space 2 div tx@Dict begin PtoC end pop def
   }
    \pscircle(0,0){!R}
    \multido{\rA=\numexpr\dAngle/2+\dAngle,\iB=0+1}{\NoSides}{%
      \pcline[linewidth=2pt](!R \rA\space PtoC)(!R \rA\space \dAngle\space add PtoC)
      \ifnum\iB=0 
        \LabelLine[:U](!R \rA\space \dAngle\space add PtoC)(!R \rA\space PtoC)%
          {-8pt}{\SI{\SideLength}{\m}}
        \LabelLine(!R \rA\space \dAngle\space add PtoC)(!Q dup){8pt}{$x$}
        \LabelLine(!R \rA\space PtoC)(!Q dup){-8pt}{$x$}
      \fi
    }
   \psset{linestyle=dotted}
    \psframe(!Q neg dup)(!Q dup)
    \rput(!Q neg dup)    {\psframe(5,5)}
    \rput(!Q neg Q 5 sub){\psframe(5,5)}
    \rput(!Q 5 sub Q neg){\psframe(5,5)}
    \rput(!Q 5 sub dup)  {\psframe(5,5)}
    \rput(0,0){$A \approx \SI[round-mode=places,round-precision=0]{\area}{\square\m}$}
  \end{pspicture}
}

\begin{document}

\polygon{8}{60}

\end{document}

output

As can be seen in the picture, the line segment in the octagon is not completely horizontal; why and how do I fix it?

Furthermore, how do I get a smooth connection between the line segments making up the octagon, i.e., the sides?

0

1 Answer 1

3
  1. The octagon is not completely in sync with the dotted frame because the placement of your coordinates are out by half a degree at every step. Here's why:

    Within the \multido, you start the real number \rA with \numexpr\dAngle/2 which, given your input as \polygon{8}{60} should result in 22.5. However, since \numexpr evaluate to an integer, the result is 23. Everything is off by half a degree from there. You could use something like

    \edef\sAngle{\calculate{\dAngle/2}}
    

    to define the starting Angle and define \rA=\sAngle+\dAngle.

  2. You're drawing line segments that have nothing to do with one another. In order to join these, contain these "open curves" inside a \pscustom, which will "close" them. However, the first and last segment will still not be closed fully, since you're not drawing a polygon per se. To avoid this, add an extra line segment at the end (duplicating the first one):

    \pscustom[linewidth=2pt]{
      \multido{\rA=\sAngle+\dAngle,\iB=0+1}{\numexpr\NoSides+1}{
        \psline(!R \rA\space PtoC)(!R \rA\space \dAngle\space add PtoC)
        % <some more stuff that should be removed (see below)>
      }
    }
    

Here is the complete MWE:

enter image description here

\documentclass{article}

\usepackage{pstricks-add}
\usepackage[locale=DE]{siunitx}

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

\def\ORb{\calculate{\SideLength/(2*sin(pi/\NoSides))-2.7}}
\def\ORh{\calculate{\SideLength/(2*sin(pi/\NoSides))-1.1}}
\def\area{\calculate{\NoSides*\SideLength^2*cot(pi/\NoSides)/4}}
\makeatletter
  \def\LabelLine{\@ifnextchar[\LabelLine@i{\LabelLine@i[0]}}
  \def\LabelLine@i[#1](#2)(#3)#4#5{%
    \pcline[linestyle=dashed,offset=#4]{|*-|*}(#2)(#3)
    \ncput*[nrot=#1]{#5}
  }
\makeatother

\def\polygon#1#2{%
  \def\NoSides{#1 }
  \def\SideLength{#2 }
  \psset{unit=0.75mm}
  \begin{pspicture}(-\ORb,-\ORh)(\ORb,\ORh)
    \edef\dAngle{\the\numexpr360/\NoSides}%
    \pstVerb{%
      /R \SideLength 180 \NoSides div sin 2 mul div def
      /Q R \dAngle\space 2 div tx@Dict begin PtoC end pop def
    }%
    \pscircle(0,0){!R}%
    \edef\sAngle{\calculate{\dAngle/2}}% Starting angle
    \pscustom[linewidth=2pt]{
      \multido{\rA=\sAngle+\dAngle,\iB=0+1}{\numexpr\NoSides+1}{
        \psline(!R \rA\space PtoC)(!R \rA\space \dAngle\space add PtoC)
      }
    }
    \multido{\rA=\sAngle+\dAngle,\iB=0+1}{\numexpr\NoSides+1}{
      \ifnum\iB=0%
        \LabelLine[:U](!R \rA\space \dAngle\space add PtoC)(!R \rA\space PtoC)%
          {-8pt}{\SI{\SideLength}{\m}}%
        \LabelLine(!R \rA\space \dAngle\space add PtoC)(!Q dup){8pt}{$x$}
        \LabelLine(!R \rA\space PtoC)(!Q dup){-8pt}{$x$}%
      \fi%
    }
    \psset{linestyle=dotted}
    \psframe(!Q neg dup)(!Q dup)
    \rput(!Q neg dup)    {\psframe(5,5)}
    \rput(!Q neg Q 5 sub){\psframe(5,5)}
    \rput(!Q 5 sub Q neg){\psframe(5,5)}
    \rput(!Q 5 sub dup)  {\psframe(5,5)}
    \rput(0,0){$A \approx \SI[round-mode=places,round-precision=0]{\area}{\square\m}$}
  \end{pspicture}
}

\begin{document}

\polygon{8}{60}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .