# Triangles inside Triangles, Fractals to an arbitrary depth

What I am trying to do is to create an image such as that below, that is, triangles inside triangles. I can get a single triangle created and I commented out the parts that create circles (for a Venn diagram, the original code was found here), but I am not sure how to create midpoints on each of the sides and connect those midpoints.

While I am only looking for depth two (the image is depth 3), I figure it would be useful to figure out how to do it programmatically so as to create something such as a fractal. I am also wanting to label the sides - at least for the first and second depth guessing that there would have to be some kind of if structure. Any ideas?

 \documentclass[tikz,border=10pt]{standalone}
\begin{tikzpicture}
\node (tri) [regular polygon, regular polygon sides=3, draw, densely dashed, minimum width=50mm] {};
%    \foreach \i/\j in {1/2,2/3,3/1}
%    {
%      \node [draw] at (tri.corner \i) [circle through={($(tri.corner \i)!1/2!(tri.corner \j)$)}, draw] {};
%      \path [fill] ($(tri.corner \i)!1/2!(tri.corner \j)$) circle (2.5pt);
%    }
\end{document}


EDIT! Using a small piece (not employing the arbitrary depth) of Tom Bombadil's extra-ordinary answer I added the labels for the sides in which I sought. The reason I added it is note that each of the three 'sides' of the largest (outer) triangle is really 6 sides - that is, instead of for example (A) -- (B) there is a coordinate (D) half way in between (A) and (B), so each of the three sides is halved to make 6 sides. While my problem is solved in this manner, I thought that someone might be able to modify Tom's code for labeling to support this ability. My labeling of a simple triangle inside triangle from Tom's code is:

\documentclass[tikz,border=10pt]{standalone}
\begin{figure}
\begin{tikzpicture}
\coordinate [label=right:$Y \wedge \neg (X \vee Z)$] (A) at (-30:5);
\coordinate [label=above:$X \wedge \neg (Y \vee Z)$] (B) at (90:5);
\coordinate [label=left:$Z \wedge \neg (X \vee Y)$] (C) at (210:5);
start color=red!10,
end color=blue!10,
iterations=3,
label options/.style={thick,violet,draw, rounded corners, outer sep=2pt},
draw options/.style={red, thick, densely dashed},
side fraction=0.47,
label position=0.57,
}
\end{tikzpicture}

\end{document}


## Output

• Everybody is complaining about my handwriting these days – percusse Nov 5 '15 at 9:46
• @tom bombadil how exactly will I be able to produce this animation? – Reinhard Neuwirth Nov 10 '15 at 22:16
• @ReinhardNeuwirth: The above code produces a PDF with 72 pages which act as frames for the animation. For the conversion to GIF you'll need ImageMagick. Then you can use the command line tool of your choice to navigate to the directory of the PDF and type the following: convert -loop 0 -delay 5 -dispose previous -density 50. There are many more options available, have a look on the website! – Tom Bombadil Nov 11 '15 at 7:17
• Any idea how to pick out the first two triangles (iterations) and label the sides and points? – Relative0 Nov 19 '15 at 2:19
• @Relative0: Does the naming follow some pattern, or should it be completely customizable? – Tom Bombadil Nov 19 '15 at 12:12

You can do recursion with any initial triangle with node names below. I didn't test it but it should be roughly around this code.

\documentclass[border=5pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
% initial path
\draw (0,0)--coordinate(t1)++(0:10cm)--coordinate(t2)++(120:10cm)-- coordinate (t3) cycle;
% define the recursion
\def\tricurse#1{%
\ifnum#1=0\else% If zero stop
\draw (t1)--coordinate(t1)(t2)--coordinate(t2)(t3)--coordinate(t3) cycle;%draw and rename
\tricurse{\number\numexpr#1-1\relax}%Recurse with counter value - 1
\fi% endif
}
\tricurse{7}
\end{tikzpicture}
\end{document}


• +1 Screenshot added ;) Are you happy with the result? – Gonzalo Medina Nov 4 '15 at 23:56
• @GonzaloMedina Much appreciated. Not so much about my ridiculous choice of triangle though :) – percusse Nov 4 '15 at 23:56
• I liked the initial triangle :) what is most important is the solution and not the initial triangle; in any case, I produced a boring equilateral triangle now. I hope you don't mind. – Gonzalo Medina Nov 5 '15 at 0:03
• @GonzaloMedina Of course not. – percusse Nov 5 '15 at 0:09
• "...Are you happy with the result" ... "of course not" ... Those comments are confusing if you don't pay attention to the time stamps =). – Chris Chudzicki Nov 5 '15 at 0:33

While this answer does not even approach Tom Bombadil's, I can't help but point out that L-systems offer a nice, compact way to draw many sorts of fractals to arbitrary depth.

\documentclass[border=5pt,tikz,png]{standalone}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{inner triangle}{
\symbol{h}{\tikzset{scale=0.5}}
\rule{T -> F++F++F++hf+T}  % F draw forward
% + rotate 60 degrees counterclockwise (angle=60 specified below)
% h (defined above) scale all subsequent down by two
% f move forward without drawing
% T recurse (draw another triangle)
}
\begin{document}
\tikz \draw [line join=round, line cap=round,
l-system={inner triangle, axiom=T, step=10cm, angle=60, order=8}]
l-system;
\end{document}


To demonstrate the versatility of this approach for drawing fractals, here's another favorite of mine:

\documentclass[border=5pt,tikz,png]{standalone}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{snowflake}{
\rule{F -> [+F--F]-F++F-}
}
\begin{document}
\tikz \draw [line join=round, line cap=round, ultra thick,
l-system={snowflake,
axiom=[F]++++[F]++++[F]++++[F]++++[F]++++F,
step=0.5cm, angle=15, order=5}]
l-system;
\end{document}

• Wow, very nice, L-Systems was the first thing I did, until I realized that this question was not asking for a Sierpinski-Triangle. Unfortunately, I wasn't able to come up with a solution for this. How did you find it? – Tom Bombadil Nov 27 '15 at 2:32
• The same way you "found" your solution and percusse "found" his--a touch of creativity and a large dollop of familiarity with the tools. Long before I heard of Tex, I had a hobby of drawing fractals using L-systems. The requested image is no harder to draw this way than a Pythagorean tree, for instance. – Charles Staats Nov 27 '15 at 5:23
• I only heard of L-Systems a few months back through TikZ. While I did some of the common ones (like Koch's snow flake or the dragon curve), I'm not yet familiar with the stuff. But thanks to you, I now know of a new concept, scaling, and thus having lines of different lengths! Thanks for that! – Tom Bombadil Nov 27 '15 at 13:58
• Oh, and by "how did you find it", I was hoping for something like this Wikipedia list, so some long list for finding nice new stuff ;-) – Tom Bombadil Nov 27 '15 at 14:01
• I suggest you check out this tutorial. It's designed for the software FRACTINT, but you should be able to adapt the examples to TikZ by defining appropriate symbols where necessary. – Charles Staats Nov 29 '15 at 18:00