Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted
  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}
share|improve this answer
    
Great; thank you, Werner! –  Svend Tveskæg Aug 20 '13 at 6:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.