# Tikz Adjustment of selfsimilar sets [closed]

I have found open source figures of fractals in a PDF document and I like the presentation and style of these fractals. Now I wanted to create similar fractals, maybe the Koch curve or the Cantor set in a similar style and my question is:

Where and how can I create such figures? Is it tikz or what program and has the author justed colored the fractal in a certain green color? Any advice on how I can achieve similar results would be appreciated.

Thanks to muzimuzhi Z! I found the almost perfect color shading:

\path[top color=green!10!black!60, bottom color=green!20, shading angle=-120]

• First unless you want to type out 364 lines one per triangle you want some way to automate. That means either learning to program in TeX/TikZ (hard!) or use some Python/Lua/etc. script to generate list of coordinate/TikZ statement to be executed Jul 4, 2022 at 18:06
• Then search for e.g. "gradient fill TikZ" Make a color gradient with tikz - TeX - LaTeX Stack Exchange has a few examples Jul 4, 2022 at 18:07
• There are a few Sierpinski triangle/carpet questions on the site 1 2 3 4, but needs to be adapted for the gradient background fill. Jul 4, 2022 at 18:10
• You may take a look at the pst-fractal package. Jul 4, 2022 at 21:18

To have each of the triangles or rectangles filled with independent gradient background, each of them have to be filled independently. This crosses out solutions based on nested decorations and lindenmayer systems.

Here is an attempt using nested pics. Not so automatic nor elegant, but it works. And hope it's a bit faster than node based solutions, like trees and graphs.

\documentclass[margin=5pt, tikz]{standalone}
%\usepackage{tikz}

\newcount\sierlevel \sierlevel=4
\newdimen\siersize  \siersize=2cm

\tikzset{
sierpinski tri/.pic={
\ifnum\sierlevel>0
\shade (30:+\siersize) -- (150:+\siersize) -- (-90:+\siersize) -- cycle;
\divide\siersize by 2
\path ( 90:+2\siersize) pic {sierpinski tri}
(210:+2\siersize) pic {sierpinski tri}
(-30:+2\siersize) pic {sierpinski tri};
\fi
},
sierpinski rect/.pic={
\ifnum\sierlevel>0
\divide\siersize by 3
\path[x=\siersize,y=\siersize]
foreach \x in {-1,0,1} {
foreach \y in {-1,0,1} {
\ifnum\numexpr\x*\x+\y*\y>0
(7*\x,7*\y) pic {sierpinski rect}
\fi
}
};
\fi
}
}

\begin{document}

\begin{tikzpicture}
\siersize=2cm
\foreach \i in {1,...,4} {
\sierlevel=\i
\begin{scope}[xshift=\i*8cm]
\fill[black]
( 90:+2\siersize) -- (210:+2\siersize) -- (-30:+2\siersize) -- cycle;
\path[top color=green!50!black!30, bottom color=green!50, shading angle=-120]
pic {sierpinski tri};
\end{scope}
}
\end{tikzpicture}

\begin{tikzpicture}
\siersize=1cm
\foreach \i in {1,...,4} {
\sierlevel=\i
\begin{scope}[xshift=\i*8cm]
\fill (-3.5\siersize, -3.5\siersize) rectangle (3.5\siersize, 3.5\siersize);
\path[top color=green!50!black!30, bottom color=green!50, shading angle=-120]
pic {sierpinski rect};
\end{scope}
}
\end{tikzpicture}
\end{document}


Update: In comment @calculatormathematical suggested a much better color shading config:

% old
\path[top color=green!50!black!30, bottom color=green!50, shading angle=-120]
% new
\path[top color=green!10!black!60, bottom color=green!20, shading angle=-120]


• \path[top color=green!10!black!60, bottom color=green!20, shading angle=-120] This color shading gives almost the exact color Jul 5, 2022 at 13:55
• @calculatormathematical Cool!! Updated my answer and many thanks. Jul 5, 2022 at 14:37