@Gonzalo Medina 提供了一个解决方案均等划分多边形线段
用正方形尝试了同样的原理。没有用。参见下面的代码。tikzMiddleLeft
BottomSide
中有关键字吗?
\begin{tikzpicture}
[scale=1.5]
\draw (0,0) coordinate (a) --
(6,0) coordinate (b) --
(6,6.0) coordinate (c) --
(0,6) coordinate (d) -- cycle;
\draw (1.0,1.0) coordinate (a') --
(5,1.0) coordinate (b') --
(5,5) coordinate (c') --
(1.0,5) coordinate (d') -- cycle;
\draw (a) -- (a') (b) -- (b') (c) -- (c') (d) -- (d');
(a) -- coordinate (MiddleLeft) (a')
(b) -- coordinate (MiddleRight) (b')
\DivideSide{5}{right}{left}{BottomSide}
% Labels for the bottom side
\node at
( $ (MiddleRight)!0.5!(BottomSide1) $ )
{h};
\node at
( $ (BottomSide1)!0.5!(BottomSide2) $ )
{i};
\node at
( $ (BottomSide2)!0.5!(BottomSide3) $ )
{j};
\node at
( $ (BottomSide3)!0.5!(BottomSide4) $ )
{k};
\node at
( $ (BottomSide4)!0.5!(MiddleLeft) $ )
{l};
\end{tikzpicture}
答案1
代码:
\documentclass[varwidth=100cm,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric}
\tikzset{
myshape/.style 2 args={
inner sep=0pt,
regular polygon,
regular polygon sides=#1,
minimum size=#2
}
}
\newcommand\DrawShape[3]{
\node[draw,myshape={#1}{#2}] (inner) {};
\node[draw,myshape={#1}{#3}] (outer) {};
\node[myshape={#1}{0.5*#3+0.5*#2}] (in) {};
\node[myshape={#1}{#3+40pt}] (outside) {};
\node[myshape={#1}{#2-40pt}] (inside) {};
\foreach \Value in {1,...,#1}
{
\draw (inner.corner \Value) -- (outer.corner \Value);
}
}
\newcommand\LabelsIn[3]{
\foreach \Text [count=\Total] in {#1} {}
\foreach \Value [count=\Num,evaluate=\Value as \Parts using \Value/\Total] in {1,...,\numexpr\Total-1\relax}
{
\draw
( $ (inner.corner #2)!\Parts!(inner.corner #3) $ ) --
( $ (outer.corner #2)!($(inner.corner #2)!\Parts!(inner.corner #3)$)!(outer.corner #3) $ );
}
\foreach \Text [count=\Num,evaluate=\Text as \Parts using (\Num/\Total)-1/(2*\Total)] in {#1}
{
\path[]
( $ (inner.corner #2)!\Parts!(inner.corner #3) $ ) -- node {\Text}
( $ (outer.corner #2)!($(inner.corner #2)!\Parts!(inner.corner #3)$)!(outer.corner #3) $ );
}
}
\newcommand\LabelsInside[3]{
\foreach \Text [count=\Total] in {#1} {}
\foreach \Text [count=\Num,evaluate=\Text as \Parts using (\Num/\Total)-1/(2*\Total)] in {#1}
{
\path[]
( $ (inner.corner #2)!\Parts!(inner.corner #3) $ ) -- node[pos=-0.4] {\Text}
( $ (outer.corner #2)!($(inner.corner #2)!\Parts!(inner.corner #3)$)!(outer.corner #3) $ );
}
}
\newcommand\LabelsOutside[3]{
\foreach \Text [count=\Total] in {#1} {}
\foreach \Text [count=\Num,evaluate=\Text as \Parts using (\Num/\Total)-1/(2*\Total)] in {#1}
{
\path[]
( $ (inner.corner #2)!\Parts!(inner.corner #3) $ ) -- node[pos=1.4] {\Text}
( $ (outer.corner #2)!($(inner.corner #2)!\Parts!(inner.corner #3)$)!(outer.corner #3) $ );
}
}
\begin{document}
\begin{tikzpicture}
\DrawShape{3}{7cm}{10cm}
\LabelsIn{1,2,3,4}{1}{2}
\LabelsInside{A,B,C,D}{1}{2}
\LabelsOutside{a,b,c,d}{1}{2}
\LabelsIn{5,6}{2}{3}
\LabelsInside{E,F}{2}{3}
\LabelsOutside{e,f}{2}{3}
\LabelsIn{7,8,9}{3}{1}
\LabelsInside{G,H,I}{3}{1}
\LabelsOutside{g,h,i}{3}{1}
\end{tikzpicture}\qquad
\begin{tikzpicture}
\DrawShape{4}{8cm}{12cm}
\LabelsIn{A,B,C}{1}{2}
\LabelsInside{1,2,3}{1}{2}
\LabelsOutside{a,b,c}{1}{2}
\LabelsIn{D,E}{2}{3}
\LabelsInside{4,5}{2}{3}
\LabelsOutside{d,e}{2}{3}
\LabelsIn{F,G}{3}{4}
\LabelsInside{6,7}{3}{4}
\LabelsOutside{f,g}{3}{4}
\LabelsIn{H,I,J,K,L}{4}{1}
\LabelsInside{8,9,10,11,\raisebox{-30pt}{12}}{4}{1}
\LabelsOutside{h,i,j,k,l}{4}{1}
\end{tikzpicture}\par\bigskip
\begin{tikzpicture}
\DrawShape{5}{8cm}{12cm}
\LabelsIn{7,8,9}{1}{2}
\LabelsIn{e,f,g,h,i}{2}{3}
\LabelsIn{p,q,r}{3}{4}
\LabelsIn{1,2,3}{4}{5}
\LabelsIn{$\alpha$,$\beta$}{5}{1}
\LabelsOutside{A,B}{5}{1}
\LabelsOutside{M,N,O,P,Q}{2}{3}
\LabelsInside{x,y,z}{1}{2}
\LabelsInside{G,H,I}{3}{4}
\end{tikzpicture}
\end{document}
解释
基本命令是
\DrawShape{<number of sides>}{<inner length>}{<outer length>}
例如,
\begin{tikzpicture} \DrawShape{3}{7cm}{10cm} \end{tikzpicture}
生产
该图形的各个角都会被自动编号
1,...,<number of sides>
。要将标签放在您使用的一侧
\LabelsIn{<list of labels>}{<start corner>}{<end corner>}
例如,
\begin{tikzpicture} \DrawShape{3}{7cm}{10cm} \LabelsIn{1,2,3,4}{1}{2} \end{tikzpicture}
生产
命令
\LabelsInside
和\LabelsOutside
具有类似的语法,用于将标签放置在侧面的外部或内部。例如:\begin{tikzpicture} \DrawShape{3}{7cm}{10cm} \LabelsIn{1,2,3,4}{1}{2} \LabelsIn{$\alpha$,$\beta$}{2}{3} \LabelsIn{A,B,C,D}{3}{1} \LabelsInside{p,q,r,s}{3}{1} \end{tikzpicture}
结果是
答案2
需要进行一些调整(例如定位),但大部分工作已经完成:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{calc}
\def\empty{}
\tikzset{pics/divided polygon/.style args={#1#2#3#4}{code={%
\def\N{#1}\def\R{#2}\def\r{#3}\def\Labels{#4}
\begin{scope}[rotate=270-180/\N-360/\N]
\path \foreach \i in {1,...,\N} { (360/\N*\i:\R) coordinate (n-\i-1) };
\path \foreach \i in {1,...,\N} { (360/\N*\i:{\R+\r/cos(180/\N)})
coordinate (n-\i-2) };
\foreach \i in {1,2}
\draw (n-1-\i) \foreach \j in {2,...,\N}{ -- (n-\j-\i) } -- cycle;
\foreach \l in {1,...,\N} \draw (n-\l-1) -- (n-\l-2);
\end{scope}
\foreach \labels [count=\i, evaluate={\j=int(mod(\i,\N)+1);}] in \Labels{
\foreach \a [count=\nlabels, remember=\nlabels] in \labels{}
\ifnum\nlabels>1
\foreach \k in {2,...,\nlabels}
\draw ($(n-\i-1)!{(\k-1)/\nlabels}!(n-\j-1)$)
coordinate (@) -- ($(n-\i-2)!(@)!(n-\j-2)$);
\fi
\foreach \l [count=\k] in \labels
\path
($(n-\i-1)!{(\k-.5)/\nlabels}!(n-\j-1)$)
coordinate (side-\i-inner label-\k)
($(n-\i-2)!(side-\i-inner label-\k)!(n-\j-2)$)
coordinate (side-\i-outer label-\k)
($(side-\i-inner label-\k)!0.5!(side-\i-outer label-\k)$)
coordinate (side-\i-label-\k);
\pgfpointadd{\pgfpointanchor{n-\i-1}{center}}%
{\pgfpointanchor{n-\j-1}{center}}%
\pgfgetlastxy\px\py%
\pgfmathparse{Mod(atan2(\py/2,\px/2),360)}%
\let\sideangle=\pgfmathresult
\pgfmathparse{Mod(\pgfmathresult,180)-90}%
\let\textangle=\pgfmathresult%
\foreach \l [count=\k] in \labels{
\tikzset{get labels/.expand once=\l}
\node [rotate=\textangle, text height=1em, text depth=.5em,
anchor=center, every main label/.try]
at (side-\i-label-\k) {\mainlabel};
\node [rotate=\textangle, text height=1em, text depth=.5em,
anchor={(\sideangle < 180) ? 270 : 90}, every outer label/.try]
at (side-\i-outer label-\k) {\outerlabel};
\node [rotate=\textangle, text height=1em, text depth=.5em,
anchor={(\sideangle >= 180) ? 270 : 90}, every inner label/.try]
at (side-\i-inner label-\k) {\innerlabel};
}}}},
get labels/.style={.. get labels=#1///@},
.. get labels/.code args={#1/#2/#3/#4@}{%
\def\mainlabel{#1}\def\outerlabel{#3}\def\innerlabel{#2}%
},
every outer label/.style={inner sep=0 },
every inner label/.style={inner sep=0 }}
\begin{document}
\begin{tikzpicture}
\pic at (0,0) {divided polygon={3}{2}{0.5}%
{{a/A/$\alpha$,b//$\beta$,c/C},{d//$\delta$,e//$\epsilon$},{f/F}}};
\pic at (8,0) {divided polygon={4}{2}{0.5}%
{{a/A, b/B/$\beta$, c},{d,e},{f},{g,h,i,j}}};
\pic at (0,8) {divided polygon={5}{2}{1}%
{{a/A,b/B,c},{d//$\delta$,e},{f/F},{g//$\gamma$,h,i,j/J},{k}}};
\pic at (8,8) {divided polygon={6}{3}{1}%
{{a/A/$\alpha$,b,c},{d,e},{f},{g,h,i//$\iota$},{j/J,k/K/$\kappa$},{l}}};
\end{tikzpicture}
\end{document}