问题
我正在寻找在 tikz 环境中实现以下结果的基本方法。目前我有下图的树部分。一般来说,如何将下图中的高亮区域(蓝色和绿色)添加到 tikz 树中?
奖励:我是否应该使用 tikz-tree?
采取的措施
我已经发现突出显示 tikz 树中的一组节点和突出显示/标记 tikz 树中的区域,但它们似乎不适用于我的问题。
目标输出
现在的进展
如果你想通过一个例子来展示这种方法,这就是我迄今为止的进展。
来源(如果需要)
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{trees, shapes}
\begin{document}
\pagestyle{empty}
\tikzstyle{level 1}=[sibling angle=90, shape = rectangle, minimum size=1.5em]
\tikzstyle{level 2}=[sibling angle=45, shape = rectangle, minimum size=1em]
\tikzstyle{level 3}=[sibling angle=22.5, shape = rectangle, minimum size=0.5em]
\tikzstyle{every node}=[fill]
\tikzstyle{edge from parent}=[draw, very thin]
\begin{tikzpicture}[
grow cyclic,
shape = circle,
level distance=3em,
triangle/.style = {fill=blue!20, regular polygon, regular polygon sides=3 },
border rotated/.style = {shape border rotate=180}
]
\node[triangle, border rotated, minimum size=3em] {} child [color=\A] foreach \A in {black, black, black, black}
{ node {} child [color=\B] foreach \B in {black, black, black}
{ node {} child [color=\C] foreach \C in {black, black, black}
{ node {} }
}
};
\end{tikzpicture}
\end{document}
答案1
如果你接受我之前的回答(基本上是从https://tex.stackexchange.com/a/539395),那么就很容易了。
\documentclass[tikz,border=9]{standalone}
\usetikzlibrary{trees,decorations,shapes.geometric,backgrounds}
\makeatletter
\newcount\tikzcountchildi
\newcount\tikzcountchildii
\newcount\tikzcountchildiii
\newcount\tikzcountchildiv
\newcount\tikzcountchildv
\newcount\tikzcountchildvi
\tikzset{garlic growth/.style={growth function=\tikz@grow@garlic,
/tikz/mmap/name=#1,mmap/#1/.is family,
mmap/initialize counts,
execute at end scope={%
\tikz@mmap@store@aux{n-i}{\the\tikzcountchildi}%
\tikz@mmap@store@aux{n-ii}{\the\tikzcountchildii}%
\tikz@mmap@store@aux{n-iii}{\the\tikzcountchildiii}%
\tikz@mmap@store@aux{n-iv}{\the\tikzcountchildiv}%
\tikz@mmap@store@aux{n-v}{\the\tikzcountchildv}%
\tikz@mmap@store@aux{n-vi}{\the\tikzcountchildvi}}},
mmap/.cd,initialize counts/.code={\tikzcountchildi=0%
\tikzcountchildii=0%
\tikzcountchildiii=0%
\tikzcountchildiv=0%
\tikzcountchildv=0%
\tikzcountchildvi=0%
}
}
\def\tikz@mmap@store@aux#1#2{%
\immediate\write\@mainaux{\string\expandafter\xdef\noexpand\csname pgfk@/tikz/mmap/\pgfkeysvalueof{/tikz/mmap/name}/#1\string\endcsname{#2}}}%
\def\tikz@mmap@get@from@aux#1#2{%
\ifcsname pgfk@/tikz/mmap/\pgfkeysvalueof{/tikz/mmap/name}/#1\endcsname
\edef#2{\csname pgfk@/tikz/mmap/\pgfkeysvalueof{/tikz/mmap/name}/#1\endcsname}%
\else
\edef#2{0}%
\fi
}
\newcount\tikzcountgrandchild
\def\tikz@grow@garlic{%
\pgfmathsetmacro{\pgfutil@tempa}{\pgfkeysvalueof{/tikz/mmap/overall rotation}}%
\ifcase\tikztreelevel
\or
\global\advance\tikzcountchildi by1\relax%
\tikz@mmap@get@from@aux{n-i}{\myni}%
\tikz@mmap@get@from@aux{n-ii}{\mynii}%
\tikz@mmap@get@from@aux{n-1-\the\tikzcountchildi}{\mynall}%
\tikz@mmap@get@from@aux{p-1-\the\tikzcountchildi}{\mypi}%
\ifnum\mynii>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}%
*(\pgfkeysvalueof{/tikz/mmap/child weight}*(\mypi)*360/\mynii+%
(1-\pgfkeysvalueof{/tikz/mmap/child weight})*(\tikzcountchildi-1)*360/\myni)}%
\fi
\or
\global\advance\tikzcountchildii by1\relax%
\tikz@mmap@get@from@aux{n-ii}{\mynii}%
\tikz@mmap@get@from@aux{n-1-1}{\mynall}%
\ifnum\mynii>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}%
*(\tikzcountchildii-1-\mynall/2)*360/\mynii}%
\fi
\ifnum\tikznumberofcurrentchild=1\relax
\tikz@mmap@store@aux{n-1-\the\tikzcountchildi}{\the\tikznumberofchildren}%
\tikz@mmap@store@aux{p-1-\the\tikzcountchildi}{\the\numexpr\tikzcountchildii-1}%
\tikz@mmap@store@aux{a-1-\the\tikzcountchildi-\the\tikzcountchildii}{\pgfutil@tempa}%
\fi
\or
\global\advance\tikzcountchildiii by1\relax%
\ifnum\tikznumberofcurrentchild=1\relax
\tikz@mmap@store@aux{n-1-\the\tikzcountchildi-\the\tikzcountchildii}{\the\tikznumberofchildren}%
\tikz@mmap@store@aux{p-1-\the\tikzcountchildi-\the\tikzcountchildii}{\the\numexpr\tikzcountchildiii-1}%
\fi
\tikz@mmap@get@from@aux{n-iii}{\myniii}%
\tikz@mmap@get@from@aux{a-1-1-1}{\bettera}%
\tikz@mmap@get@from@aux{n-1-1-1}{\mynall}%
\ifdim\bettera pt=0pt\relax
\else
\pgfmathsetmacro{\pgfutil@tempa}{\bettera}%
\fi
\ifnum\myniii>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}%
*(\tikzcountchildiii-1-\mynall/2)*360/\myniii}%
\fi
\or
\global\advance\tikzcountchildiv by1\relax%
\tikz@mmap@get@from@aux{n-iv}{\myniv}%
\ifnum\myniv>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}*(\tikzcountchildiv-1)*360/\myniv}%
\fi
\or
\tikz@mmap@get@from@aux{n-v}{\mynv}%
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}*(\tikzcountchildv-1)*360/\mynv}%
\ifnum\mynv>0
\global\advance\tikzcountchildv by1\relax%
\fi
\or
\global\advance\tikzcountchildvi by1\relax%
\tikz@mmap@get@from@aux{n-vi}{\mynvi}%
\ifnum\myvi>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+(\tikzcountchildvi-1)*360/\mynvi}%
\fi
\fi
\pgftransformreset%
\pgftransformshift{\pgfpoint{\pgfkeysvalueof{/tikz/mmap/overall xshift}}%
{\pgfkeysvalueof{/tikz/mmap/overall yshift}}}%
\pgftransformrotate{\pgfutil@tempa}%
\pgftransformxshift{\the\tikzleveldistance}%
}
\makeatother
\begin{document}
\tikzset{mmap/.cd,
name/.initial=undef,
overall rotation/.initial=0,
overall xshift/.initial=0pt,
overall yshift/.initial=0pt,
sign/.initial=1,
child weight/.initial=0.5,
/tikz/.cd,
Xshift/.style={xshift=#1,mmap/overall xshift=#1},
Yshift/.style={yshift=#1,mmap/overall yshift=#1},
branch color/.style={
concept color=#1!80,ball color=#1!50,
every child/.append style={concept color=#1!50},
}
}
\begin{tikzpicture}[garlic growth=A,mmap/child weight=0,
mmap/overall rotation=15,mmap/sign=-1,
shape = circle,
level 1/.style={shape = rectangle, minimum size=1.5em,
level distance=1.2cm,
},
level 2/.style={shape = rectangle, minimum size=1em,
level distance=2.4cm,
},
level 3/.style={shape = rectangle, minimum size=0.5em,
level distance=3.6cm,sibling angle=40,
},
edge from parent/.style={draw, very thin},nodes=fill,
level distance=3em,
triangle/.style = {fill=blue!50, regular polygon, regular polygon sides=3 },
border rotated/.style = {shape border rotate=180}
]
\node[triangle, border rotated, minimum size=3em] (M){} child [color=\A] foreach \A in {black, black, black, black}
{ node {} child [color=\B] foreach \B in {black, black, black}
{ node {} child [color=\C] foreach \C in {black, black, black}
{ node {} }
}
};
\begin{scope}[on background layer]
\fill[blue!20] (M.center) circle[radius=4.2cm];
\draw[yellow!50,line width=3.2em,line cap=round] (M-1.center) -- (M-1-1.center);
\draw[yellow!50,line width=2.2em,line cap=round] (M-1-1.center) -- (M-1-1-2.center);
\end{scope}
\end{tikzpicture}
\end{document}
如果您命名了节点,则无需猜测它们的索引。当然,您可以编写宏或样式,使线宽取决于级别,等等。