# Creating a Sierpinski gasket with the missing triangles filled in

I'm creating some questions for my math classes. The one below refers to the Sierpinski gasket. I found some great demonstrations to create the gasket. So that's good. Solving the problem is easier to do if the diagram has all the triangles shown, eg. as below... I'm wondering if someone could create a function to create the gasket and then switch to a version where all the "little" triangles are shown? I'd like to be able to ask similar questions with the gasket at different levels.

The reason for having ALL the triangles is it lets the student find the fraction of the shaded triangles compared to the total number of small triangles.

• Tom, please use the built-in image uploader to host your images instead of on dropbox. This is so that the images will remain even after you delete the file from your dropbox (without them, it's hard to follow the question) – rm -rf Jun 25 '12 at 4:37
• Sorry, didn't know. I need to learn more about how to use StackExchange. Thanks for telling me, I'll edit that. – Tom De Vries Jun 26 '12 at 9:02

Here's an interactive version.

Manipulate[
Graphics[{Nest[
Translate[Scale[#, 1/2, {0, 0}], pts/2] &, {Polygon[pts]},
depth],
{Brown,
If[triangles && depth >= 2,
Nest[Translate[Scale[#, 1/2, Mean[pts]],
Flatten[{#/2, #/8, -#/4} &@(# - Mean[pts] & /@ pts),
1]] &, {Scale[Polygon[pts], 1/4, Mean[pts]]}, depth - 2]]}},
PlotRange -> {{0, 1}, {0, 1}}, PlotRangePadding -> .2],
{{pts, {{0, 0}, {1, 0}, {1/2, 1/2 Sqrt}}}, Locator},
{{depth, 4}, Range},
{{triangles, False, "Show triangles"}, {True, False}, Checkbox}] • Wonderfullll +1 – Rojo Jun 23 '12 at 17:18
• Thank you, that's fantastic, and helps to move this from "just a question" to something visually appealing and interactive. Much appreciated! – Tom De Vries Jun 23 '12 at 17:26
• +1 Excellent usage of geometric transformations. – Vitaliy Kaurov Jun 24 '12 at 6:34

Here's a quick way to produce a Sierpinski gasket:

With[{n = 4}, (* nesting level *)
Nest[(# /. poly : Polygon[pts_, ___] :>
Map[Function[p, Translate[Scale[poly, 1/2, {0, 0}], p/2]], pts]) &,
Graphics[{Polygon[{{Sqrt/2, -1/2}, {0, 1}, {-Sqrt/2, -1/2}}]}],
n]] Here's a nice triangular grid:

With[{n = 7},
Show[Graphics[
Table[Polygon[TranslationTransform[{Sqrt (i - j/2), 3 j/2}] /@
{{Sqrt/2, -1/2}, {0, 1}, {-Sqrt/2, -1/2}}],
{i, n}, {j, i}]]]] • Thanks, that was fast! So, for a given level, what would I have to change to create NOT the gasket , but the triangle with alternating black and white triangles of the smallest size...? – Tom De Vries Jun 23 '12 at 16:25
• That's surprisingly trickier to do, @Tom. Let me think about it... – J. M. will be back soon Jun 23 '12 at 16:30
• Excellent, much appreciated! – Tom De Vries Jun 23 '12 at 17:23
• I prefer subdividing a triangle than building it out — avoids having to fiddle with the plot range and keeps it the same size. So the function in Nest would be something like # /. Polygon[pts_, ___] :> Polygon /@ (Thread[#1 + Transpose@{##}]/2 & @@@ NestList[RotateRight, #, Length@#] &@pts) & – rm -rf Jun 23 '12 at 17:28
• I choose Heike's answer as "the answer", but I will use this function as well. I'd select both if possible. Thanks for the help, this is a great addition to the question and will be helpful to the students in visualizing the answer. – Tom De Vries Jun 23 '12 at 17:30

To create the figure with all the triangles you might use this:

n = 7;

Table[{2 j - i, Sqrt i}, {i, 0, n}, {j, i, n}];

Graphics[Polygon /@ Riffle @@@ Partition[%, 2, 1]] • Thanks for that, nice to see different methods for the same question! – Tom De Vries Jun 24 '12 at 13:59
• Wow,your code is always so clean,+1.By the way,how can you find the most proper function every time? – withparadox2 Jun 26 '12 at 12:57
• @paradox2 Thanks. :-) Experience combined with a desire for concise code I suppose. I certainly don't always have the right/cleanest code but I try to learn when I see it, and this site is great resource for learning. It is helpful to think through and see different methods to achieve something as they often have different strengths and weaknesses when approaching a new problem; this is one reason I post as many answers as I do: the exercise of finding a different way benefits me, and I hope the variety of answers benefits others. – Mr.Wizard Jun 26 '12 at 20:47