4

I found the following picture of snowflakes:

\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}

Can I use it to replace circles (nodes) of a graph drawn in tikz? This is my graph:

\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}
4

According to what I find, the perhaps most straightforward way of achieving this is to employ pics.

\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}

enter image description here

The cleanest way would probably be to define a new shape. Given the complexity of the shape, this might not be straightforward, but it is conceivable that it is not impossible. Another option would be to use a path picture of a node, but like in the pics above that would "only" lead to background pictures and in particular not define the boundary of the nodes, meaning that the gaps in the lines connecting the snow flakes would be similar to above. (They may become slightly smaller if you base the node on a circle shape, but path pictures have other subtleties which is why I removed this part of my earlier answer.)

Note that, if you are concerned about the gaps in the lines connecting the nodes, there is a simple fix.

\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}

enter image description here

I am, of course, not claiming that the lines hit the "nodes" precisely in the same way as \pgfpointshapeborder would, but since these snow flakes are sort of spherical, it almost works.

ADDENDUM: as requested in the comments, a version with 3 arguments, where the 3rd argument are the node options such as color, font etc.

\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}

enter image description here

6
  • just a last: can I change color of line, size of snowflake and numbers? Nov 29 '18 at 16:25
  • @DrimadesBoy The size and color can be changed easily: in \pic[local bounding box=n6,scale=0.3] at (31,20) {snowflake={fill=yellow}{6}}; change scale=0.3 to scale=0.2, say, to make it smaller, and use snowflake={fill=yellow,draw=red} to change the draw color to red. The last point is more difficult. You basically have to change the code of the picture, here are some examples. I do not think that it is easy to find a parametrization in which you can simply change the number of corners.
    – user121799
    Nov 29 '18 at 16:31
  • That worked! Sorry for the confusion, but I just need to change color of text of numbers printed in the nodes. Nov 29 '18 at 16:40
  • @DrimadesBoy I see. The number is the second argument. That is, if you change in \pic[local bounding box=n6,scale=0.3] at (31,20) {snowflake={fill=yellow}{6}}; 6 to 11, the 6 will become 11.
    – user121799
    Nov 29 '18 at 16:42
  • 1
    @DrimadesBoy I added a version in which you can adjust the node options.
    – user121799
    Nov 29 '18 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.