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
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:
Problems:
I would like
- the polygon (here, pentagon) to be completely inside the dotted square,
- the dashed lines with the
x
andy
labels to be placed as on Jubobs' TikZ solution.
Update 2
After Herbert's changes:
- 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
(In the picture above, the square is drawn manually.)