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 example.

Code

% pdflatex -shell-escape test.tex

\documentclass{article}

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

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

\def\OuterRadius{\calc{\SideLength/(2*sin(pi/\NoSides))}}
\def\RelativeAngle{\calc{360/\NoSides}}
\def\Rotation{\calc{\RelativeAngle/2}}
\def\Area{\calc{1/4*\NoSides*\SideLength^2*cot(pi/\NoSides)}}

\begin{document}

\begin{figure}
% Parameters
\def\NoSides{8}
\def\SideLength{60}
\psset{unit=0.05cm}
\begin{pspicture}(-\OuterRadius,-\OuterRadius)(\OuterRadius,\OuterRadius)
  \pscircle(0,0){\OuterRadius}
  \rput(0,0){%
    \PstPolygon[
      PolyNbSides=\NoSides,
      PolyRotation=\Rotation,
      unit=\OuterRadius,
      linewidth=2\pslinewidth
    ]
  }
  \multido{\rA=0+\RelativeAngle,\iA=1+1}{\NoSides}{%
    \psRelNode[angle=\rA](0,0)%
      (!180 \NoSides\space div cos \OuterRadius\space mul
        180 \NoSides\space div sin \OuterRadius\space mul){1}{P\iA}
  }
 \psset{linewidth=0.5\pslinewidth,RightAngleSize=5}
  \rput(0,0){$A \approx \SI[round-mode=places,round-precision=0]{\Area}{\square\m}$}
  % The (really) bad code starts here.
  \pcline[linestyle=dashed,offset=-8pt]{|-|}(P2)(P1)
  \ncput*[nrot=:U]{\SI{\SideLength}{\m}}
  \psRelLine[linestyle=none](P8)(P1){2}{S'}
  \psRelLine[linestyle=none](P3)(P2){2}{S''}
  \psIntersectionPoint(P1)(S')(P2)(S''){S}
  \pstRightAngle[linestyle=dotted]{P1}{S}{P2}
  \pcline[linestyle=dotted](P1)(S)
  \pcline[linestyle=dashed,offset=-8pt]{|-|}(P1)(S)
  \ncput*{$x$}
  \pcline[linestyle=dotted](P2)(S)
  \pcline[linestyle=dashed,offset=8pt]{|-|}(P2)(S)
  \ncput*{$x$}
  \psRelLine[linestyle=none](P2)(P3){2}{T'}
  \psRelLine[linestyle=none](P5)(P4){2}{T''}
  \psIntersectionPoint(P3)(T')(P4)(T''){T}
  \pstRightAngle[linestyle=dotted]{P3}{T}{P4}
  \psline[linestyle=dotted](P3)(T)(P4)
  \psRelLine[linestyle=none](P4)(P5){2}{U'}
  \psRelLine[linestyle=none](P7)(P6){2}{U''}
  \psIntersectionPoint(P5)(U')(P6)(U''){U}
  \pstRightAngle[linestyle=dotted]{P5}{U}{P6}
  \psline[linestyle=dotted](P5)(U)(P6)
  \psRelLine[linestyle=none](P6)(P7){2}{V'}
  \psRelLine[linestyle=none](P1)(P8){2}{V''}
  \psIntersectionPoint(P7)(V')(P8)(V''){V}
  \pstRightAngle[linestyle=dotted]{P7}{V}{P8}
  \psline[linestyle=dotted](P7)(V)(P8)
\end{pspicture}
\end{figure}

\end{document}

Output

enter image description here

Question

I have the desired output but the code is not very elegant. The problem is (especially) to simplify the code giving the dotted lines and the right angle marks around the octagon.

Update

Code:

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage{auto-pst-pdf,pst-node,multido}
\usepackage[locale=DE]{siunitx}

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

\begin{document}

\begin{figure}[htbp]
\centering
% Parameters.
\def\NoSides{5 }
\def\SideLength{60 }
% Help.
\def\OR{\calc{\SideLength/(2*sin(pi/\NoSides))}}
\def\Area{\calc{1/4*\NoSides*\SideLength^2*cot(pi/\NoSides)}}
\edef\dAngle{\the\numexpr360/\NoSides}
\def\IR{\calc{1/2*\SideLength*cot(pi/\NoSides)}}
\def\LabelLine(#1)(#2)#3#4#5{%
  \pcline[linestyle=dashed,offset=#3]{|-|}(#1)(#2)
  \ncput*[nrot=#4]{#5}
}
\psset{unit=0.5mm}
\begin{pspicture}(-\OR,-\OR)(\OR,\OR)
 \pstVerb{/R \SideLength 180 \NoSides div sin 2 mul div def}
  \pscircle(0,0){!R }
  \multido{\iA=\numexpr\dAngle/2+\dAngle,\iB=0+1}{\NoSides}{%
    \pcline[linewidth=2pt](!R \iA\space PtoC)(!R \iA\space \dAngle\space add PtoC)
    \ifnum\iB=0 
      \LabelLine(!R \iA\space \dAngle\space add PtoC)%
                (!R \iA\space PtoC)%
                {-8pt}{:U}{\SI{\SideLength}{\m}}
      \LabelLine(!R \iA\space \dAngle\space add PtoC)%
                (!R \iA\space \dAngle\space add PtoC exch pop \OR\space exch)%
                {8pt}{0}{$x$}
      \LabelLine(!R \iA\space PtoC)%
                (!R \iA\space PtoC pop \OR)%
                {-8pt}{0}{$y$}
    \fi
  }
 \psset{linestyle=dotted}
  \psframe(-\IR,-\IR)(\IR,\IR)
  \rput(-\IR,-\IR){\psframe(5,5)}
  \rput(!-\IR\space \IR\space 5 sub){\psframe(5,5)}
  \rput(!\IR\space 5 sub -\IR){\psframe(5,5)}
  \rput(!\IR\space 5 sub \IR \space 5 sub){\psframe(5,5)}
  \rput(0,0){$A \approx \SI[round-mode=places,round-precision=0]{\Area}{\square\m}$}
\end{pspicture}
\end{figure}

\end{document}

Output:

enter image description here

Problems:

I would like

  • the polygon (here, pentagon) to be completely inside the dotted square,
  • the dashed lines with the x and y labels to be placed as on Jubobs' TikZ solution.

Update 2

After Herbert's changes:

enter image description here

  • I would like the "upper" dashed line to be horizontal,
  • The polygon is still not completey inside the square.

If the problem in the second bullet is fixed, the problem in the first one will be fixed automatically (I think).

Update 3

Here is how I would like the square to be

enter image description here

(In the picture above, the square is drawn manually.)

share|improve this question
    
Is it only about octagon? If it is for any polygon then constructing the outer square with 2 parallel sides is not generally possible. –  stalking is prohibited Apr 24 '13 at 5:07
    
@Bugbusters I had hoped for a general polygon. Can you then simplify the code giving the dotted lines and the right angle marks around the octagon specifically? –  Svend Tveskæg Apr 24 '13 at 6:15
    
It is difficult to synchronize the dotted lines of frame with the ones of right mark angle. –  stalking is prohibited Apr 24 '13 at 6:25

3 Answers 3

up vote 4 down vote accepted

A solution without defining nodes. Works only for n=8:

\documentclass{article}
\usepackage[pdf]{pstricks}% for pdflatex --shell-escape
\usepackage{pst-node,multido}
\usepackage[locale=DE]{siunitx}
\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

\def\NoSides{8 }  \def\SideLength{60 }

\def\OR{\calc{\SideLength/(2*sin(pi/\NoSides))}}
\def\Area{\calc{1/4*\NoSides*\SideLength^2*cot(pi/\NoSides)}}
\edef\dAngle{\the\numexpr360/\NoSides}
\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
\begin{document}

\psset{unit=0.5mm}
\begin{pspicture}(-\OR,-\OR)(\OR,\OR)
  \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{\iA=\numexpr\dAngle/2+\dAngle,\iB=0+1}{\NoSides}{%
    \pcline[linewidth=2pt](!R \iA\space PtoC)(!R \iA\space \dAngle\space add PtoC)
    \ifnum\iB=0 
      \LabelLine[:U](!R \iA\space \dAngle\space add PtoC)(!R \iA\space PtoC){-6pt}{\SideLength m}
      \LabelLine(!R \iA\space \dAngle\space add PtoC)(!Q dup){10pt}{$x$}
      \LabelLine(!R \iA\space PtoC)(!Q dup){-10pt}{$x$}
    \fi}
  \psframe[linestyle=dotted](!Q neg dup)(!Q dup)
  \rput(!Q neg dup)    {\psframe[linestyle=dotted](5,5)}
  \rput(!Q neg Q 5 sub){\psframe[linestyle=dotted](5,5)}
  \rput(!Q 5 sub Q neg){\psframe[linestyle=dotted](5,5)}
  \rput(!Q 5 sub dup)  {\psframe[linestyle=dotted](5,5)}
  \rput(0,0){$A \approx \SI[round-mode=places,round-precision=0]{\Area}{\square\m}$}
\end{pspicture}

\end{document}

enter image description here

share|improve this answer
    
By the way, your outer square is not similar to the questioner's one. How do you construct the outer square for n=5 (for example)? –  stalking is prohibited Apr 24 '13 at 9:21
    
(1) I cannot compile using pdflatex (even if I add \usepackage[pdf]{pstricks}) -- should I upload the log file somewhere? (2) The outer square is not placed like in my drawing. (3) I would like to use nrot=:U for only the side length and one of the x's –  Svend Tveskæg Apr 24 '13 at 10:33
    
@Herbert I have fixed (2) and (3) myself, but not (1). –  Svend Tveskæg Apr 24 '13 at 11:21
    
for 1 I have to investigate and for 3 use \def\LabelLine(#1)(#2)#3#4#5{%and then [nrot=#5] and for the \LabelLine calls extend it with {:U} or {0} –  Herbert Apr 24 '13 at 11:34
    
for 1 move the line \pstVerb{/R ... into the pspicture environment –  Herbert Apr 24 '13 at 11:37

I really don't understand the purpose of presenting the outer square especially for odd-side regular polygons. In this answer I omit the outer square.

Single:

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-node,siunitx}
\usepackage[nomessages]{fp}

% constants declarator
\def\LoadConstants{}
\newcommand\const[3][6]{%
    \edef\temporary{trunc(#3}%
    \expandafter\FPeval\csname#2\expandafter\endcsname
        \expandafter{\temporary:#1)}%
        \edef\LoadConstants{\LoadConstants
            \noexpand\pstVerb{/#2 \csname#2\endcsname\space def}}%
                        \ignorespaces}

\psset{unit=1mm}

\def\polygon#1#2{%
    \def\points{}%
    \const[0]{NoSides}{#1}
    \const{SideLength}{#2}
    \const{OffsetAngle}{pi/NoSides}
    \const{OuterRadius}{SideLength/2/sin(OffsetAngle)}
    \const{Area}{1/4*NoSides*pow(2,SideLength)/tan(OffsetAngle)}
    \begin{pspicture}(-\OuterRadius,-\OuterRadius)(\OuterRadius,\OuterRadius)
        \LoadConstants
        \rput{!OffsetAngle RadtoDeg}(0,0){%
            \curvepnodes[plotpoints=\numexpr\NoSides+1\relax]{0}{360}{OuterRadius t PtoC}{P}
            \multido{\i=0+1}{\Pnodecount}{\xdef\points{\points (P\i)}}
            \expandafter\pspolygon\points}
        \pscircle[dimen=money-oriented-programmer](0,0){!OuterRadius}
        \psset{arrows=|*-|*,nrot=:U,linestyle=dashed,tbarsize=2pt 2}
        \scriptsize
        \pcline[offset=-6pt](P1)(P0)\ncput*{\SI{#2}{\cm}}
        \pcline[offset=5pt](P1)(P0|P1)\ncput*{$x$}
        \pcline[offset=5pt](P0|P1)(P0)\ncput*[nrot=0]{$y$}
        \rput(0,0){$A \approx \SI[round-mode=places,round-precision=0]{\Area}{\square\m}$}
    \end{pspicture}}

\begin{document}
\polygon{8}{60}
\end{document}

Multiple:

enter image description here

\documentclass[border=12pt]{standalone}
\usepackage{pst-plot,pst-node,siunitx}
\usepackage[nomessages]{fp}

% constants declarator
\def\LoadConstants{}
\newcommand\const[3][6]{%
    \edef\temporary{trunc(#3}%
    \expandafter\FPeval\csname#2\expandafter\endcsname
        \expandafter{\temporary:#1)}%
        \edef\LoadConstants{\LoadConstants
            \noexpand\pstVerb{/#2 \csname#2\endcsname\space def}}%
                        \ignorespaces}

\psset{unit=1mm}

\def\polygon#1#2{%
    \def\points{}%
    \const[0]{NoSides}{#1}
    \const{SideLength}{#2}
    \const{OffsetAngle}{pi/NoSides}
    \const{OuterRadius}{SideLength/2/sin(OffsetAngle)}
    \const{Area}{1/4*NoSides*pow(2,SideLength)/tan(OffsetAngle)}
    \begin{pspicture}(-\OuterRadius,-\OuterRadius)(\OuterRadius,\OuterRadius)
        \LoadConstants
        \rput{!OffsetAngle RadtoDeg}(0,0){%
            \curvepnodes[plotpoints=\numexpr\NoSides+1\relax]{0}{360}{OuterRadius t PtoC}{P}
            \multido{\i=0+1}{\Pnodecount}{\xdef\points{\points (P\i)}}
            \expandafter\pspolygon\points}
        \pscircle[dimen=money-oriented-programmer](0,0){!OuterRadius}
        \psset{arrows=|*-|*,nrot=:U,linestyle=dashed,tbarsize=2pt 2}
        \scriptsize
        \pcline[offset=-6pt](P1)(P0)\ncput*{\SI{#2}{\cm}}
        \pcline[offset=5pt](P1)(P0|P1)\ncput*{$x$}
        \pcline[offset=5pt](P0|P1)(P0)\ncput*[nrot=0]{$y$}
        \rput(0,0){$A \approx \SI[round-mode=places,round-precision=0]{\Area}{\square\m}$}
    \end{pspicture}}

\begin{document}
\multido{\i=5+1}{4}{\polygon{\i}{30}}
\end{document}
share|improve this answer
    
No need to understand it. :) There is meaning behind the madness. –  Svend Tveskæg Apr 24 '13 at 10:42
    
Btw. your code looks really nice. –  Svend Tveskæg Apr 24 '13 at 10:55
    
@SvendTveskæg: My philosophy is to avoid using \space because it makes the code look long, verbose and dirty. –  stalking is prohibited Apr 24 '13 at 11:01

An incomplete TikZ solution but which is flexible in terms of the number of sides:

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

% Disclaimer: I use \pgfmathsetmacro instead of \def
% to avoid conflicts with existing macros

% parameters for the regular polygon
\pgfmathsetmacro{\R}{4}                         % radius of circumcycle
\pgfmathtruncatemacro\N{5}                      % number of sides (best if >= 5)
\pgfmathsetmacro\a{360/\N}                      % twice the interior angle
\pgfmathsetmacro\area{.5*\N*\R*\R*sin(360/\N)}  % area of polygon
\pgfmathsetmacro{\side}{2*\R*sin(180/\N)}       % side length

% quantities relevant for drawing
\pgfmathsetmacro\firstangle{90+.5*\a}           
\pgfmathsetmacro\secondangle{\firstangle-\a}
\pgfmathsetmacro\thirdangle{\secondangle-\a}    
\pgfmathsetmacro\lastangle{-270+.5*\a}
\pgfmathtruncatemacro\myshift{8}

\begin{tikzpicture}[%
    thick,%
    mylinestyle/.style={%
        |-|,%
        black!50,
        dashed,%
    },%
    mynodestyle/.style={%
        fill=white,%
        sloped,%
        pos=.5,
    }
]

    \coordinate (A) at (\secondangle:\R);
    \coordinate (B) at (\thirdangle:\R);

    % draw x line
    \draw[mylinestyle]
        ([yshift=\myshift] A) -- 
        ([yshift=\myshift] A-| B) node[mynodestyle] {$x$};

    % draw y line       
    \draw[mylinestyle]
        ([xshift=\myshift] A-| B) 
        -- ([xshift=\myshift] B) node[mynodestyle,rotate=90] {$y$};

    % draw side line
    \draw[mylinestyle]
        ([xshift={-\myshift*sin(360/\N)},   yshift={-\myshift*cos(360/\N)},] A) --
        ([xshift={-\myshift*sin(360/\N)},   yshift={-\myshift*cos(360/\N)},] B)     
        node[mynodestyle] {$\side \,\text{cm}$};

    % draw the circumcircle   
    \draw[thin,gray] circle (\R)
        node[black] {$A \approx \area \, \text{cm\textsuperscript2}$};

    % Draw the regular polygon
    \draw (\firstangle:\R)
        \foreach \x in {\secondangle,\thirdangle,...,\lastangle} {%
            -- (\x:\R)
        } -- cycle;

\end{tikzpicture}

\end{document}
share|improve this answer
    
Really nice! If no PSTricks solution appears, I'll accept this answer (unless a better TikZ solution is presented). –  Svend Tveskæg Apr 23 '13 at 23:17

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.