考虑以下代码:
\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}
从图中可以看出,八边形中的线段不是完全水平的;为什么?我该如何解决这个问题?
此外,如何使组成八边形的线段(即边)之间建立平滑的连接?
答案1
八边形与虚线框并不完全同步,因为坐标位置每一步都会偏离半度。原因如下:
在 中
\multido
,你以 开始r
eal 数字,假设你的输入\rA
为,结果应该是。但是,由于求值为整数,结果为。一切都偏离了半度。你可以使用类似\numexpr\dAngle/2
\polygon{8}{60}
22.5
\numexpr
23
\edef\sAngle{\calculate{\dAngle/2}}
定义
s
开始Angle
并定义\rA=\sAngle+\dAngle
。你画的线段彼此之间毫无关联。为了连接这些线段,需要将这些“开放曲线”包含在
\pscustom
,这将“闭合”它们。但是,由于你没有绘制多边形,因此第一段和最后一段仍不会完全闭合本身。为了避免这种情况,请在末尾添加一个额外的线段(复制第一个线段):\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)> } }
以下是完整的 MWE:
\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}