我找到了以下雪花的图片:
\begin{tikzpicture}[decoration=Koch snowflake]
\draw decorate{decorate{decorate{decorate{(0,0) -- (3,0)}}}};
\draw decorate{decorate{decorate{decorate{(3,0) -- (1.5,-3)}}}};
\draw decorate{decorate{decorate{decorate{(1.5,-3) -- (0,0)}}}};
\end{tikzpicture}
我可以用它来替换在 tikz 中绘制的图形的圆圈(节点)吗?这是我的图表:
\begin{tikzpicture}
[scale=.6,auto=right,every node/.style={circle,fill=yellow!70}]
\node (n6) at (31,20) {6};
\node (n4) at (34,18) {4};
\node (n5) at (38,19) {5};
\node (n1) at (41,18) {1};
\node (n2) at (39,16) {2};
\node (n3) at (35,15) {3};
\foreach \from/\to in {n6/n4,n4/n5,n5/n1,n1/n2,n2/n5,n2/n3,n3/n4}
\draw (\from) -- (\to);
\end{tikzpicture}
答案1
根据我的发现,实现此目标最直接的方法可能是采用pic
s。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.fractals}
\tikzset{pics/.cd,
snowflake/.style n args={2}{code={%
\begin{scope}[decoration=Koch snowflake]
\draw[#1] decorate{decorate{decorate{decorate{(-1.5,1) -- (1.5,1)}}}};
\draw[#1] decorate{decorate{decorate{decorate{(1.5,1) -- (0,-2)}}}};
\draw[#1] decorate{decorate{decorate{decorate{(0,-2) -- (-1.5,1)}}}};
\path[#1] (-1.5,1) -- (1.5,1) -- (0,-2);
\node at (0,0) {#2};
\end{scope}}}}
\begin{document}
\begin{tikzpicture}[scale=.6,auto=right]
\pic[local bounding box=n6,scale=0.3] at (31,20) {snowflake={fill=yellow}{6}};
\pic[local bounding box=n4,scale=0.3] at (34,18) {snowflake={fill=yellow}{4}};
\pic[local bounding box=n5,scale=0.3] at (38,19) {snowflake={fill=yellow}{5}};
\pic[local bounding box=n1,scale=0.3] at (41,18) {snowflake={fill=yellow}{1}};
\pic[local bounding box=n2,scale=0.3] at (39,16) {snowflake={fill=yellow}{2}};
\pic[local bounding box=n3,scale=0.3] at (35,15) {snowflake={fill=yellow}{3}};
\foreach \from/\to in {n6/n4,n4/n5,n5/n1,n1/n2,n2/n5,n2/n3,n3/n4}
\draw (\from) -- (\to);
\end{tikzpicture}
\end{document}
最简洁的方法可能是定义一个新形状。考虑到形状的复杂性,这可能并不简单,但可以想象这并非不可能。另一种选择是使用节点的路径图片,但就像pic
上面的 s 一样,这“仅”会导致背景图片,特别是不会定义节点的边界,这意味着连接雪花的线条之间的间隙将与上面的类似。(如果将节点基于圆形,它们可能会变得稍微小一些,但路径图片有其他微妙之处,这就是我删除之前答案的这部分的原因。)
请注意,如果您担心连接节点的线之间的间隙,有一个简单的解决方法。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.fractals,backgrounds}
\tikzset{pics/.cd,
snowflake/.style n args={2}{code={%
\begin{scope}[decoration=Koch snowflake]
\draw[#1] decorate{decorate{decorate{decorate{(-1.5,1) -- (1.5,1)}}}};
\draw[#1] decorate{decorate{decorate{decorate{(1.5,1) -- (0,-2)}}}};
\draw[#1] decorate{decorate{decorate{decorate{(0,-2) -- (-1.5,1)}}}};
\path[#1] (-1.5,1) -- (1.5,1) -- (0,-2);
\node at (0,0) {#2};
\end{scope}}}}
\begin{document}
\begin{tikzpicture}[scale=.6,auto=right]
\pic[local bounding box=n6,scale=0.3] at (31,20) {snowflake={fill=yellow}{6}};
\pic[local bounding box=n4,scale=0.3] at (34,18) {snowflake={fill=yellow}{4}};
\pic[local bounding box=n5,scale=0.3] at (38,19) {snowflake={fill=yellow}{5}};
\pic[local bounding box=n1,scale=0.3] at (41,18) {snowflake={fill=yellow}{1}};
\pic[local bounding box=n2,scale=0.3] at (39,16) {snowflake={fill=yellow}{2}};
\pic[local bounding box=n3,scale=0.3] at (35,15) {snowflake={fill=yellow}{3}};
\begin{scope}[on background layer]
\foreach \from/\to in {n6/n4,n4/n5,n5/n1,n1/n2,n2/n5,n2/n3,n3/n4}
\draw (\from.center) -- (\to.center);
\end{scope}
\end{tikzpicture}
\end{document}
当然,我并不是说这些线会以同样的方式精确地击中“节点” \pgfpointshapeborder
,但由于这些雪花是球形的,所以它几乎可以起作用。
附录:根据评论的要求,一个具有 3 个参数的版本,其中第 3 个参数是节点选项,例如颜色、字体等。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.fractals,backgrounds}
\tikzset{pics/.cd,
snowflake/.style n args={3}{code={%
\begin{scope}[decoration=Koch snowflake]
\draw[#1] decorate{decorate{decorate{decorate{(-1.5,1) -- (1.5,1)}}}};
\draw[#1] decorate{decorate{decorate{decorate{(1.5,1) -- (0,-2)}}}};
\draw[#1] decorate{decorate{decorate{decorate{(0,-2) -- (-1.5,1)}}}};
\path[#1] (-1.5,1) -- (1.5,1) -- (0,-2);
\node[#3] at (0,0) {#2};
\end{scope}}}}
\begin{document}
\begin{tikzpicture}[scale=.6,auto=right]
\pic[local bounding box=n6,scale=0.3] at (31,20) {snowflake={fill=yellow}{6}{text=red}};
\pic[local bounding box=n4,scale=0.3] at (34,18) {snowflake={fill=yellow}{4}{font=\sffamily,text=blue}};
\pic[local bounding box=n5,scale=0.3] at (38,19) {snowflake={fill=yellow}{5}{rotate=45}};
\pic[local bounding box=n1,scale=0.3] at (41,18) {snowflake={fill=yellow}{1}{}};
\pic[local bounding box=n2,scale=0.3] at (39,16) {snowflake={fill=yellow}{2}{}};
\pic[local bounding box=n3,scale=0.3] at (35,15) {snowflake={fill=yellow}{3}{}};
\begin{scope}[on background layer]
\foreach \from/\to in {n6/n4,n4/n5,n5/n1,n1/n2,n2/n5,n2/n3,n3/n4}
\draw (\from.center) -- (\to.center);
\end{scope}
\end{tikzpicture}
\end{document}