# How do I integrate a For loop to generate iterations of the Sierpinski's Triangle?

I'm sorry if this is a basic question—I don't know anything about programming. I know you can create a graphic of Sierpinski's Triangle with a single command, but I'd like to know how to create one with 8 iterations by "integrating a for loop" in the code I was given in my linear algebra class (for the first two iterations):

• SierpinskiMesh Commented Nov 18, 2020 at 7:34

The following code is from the help content of the ref/AffineTransform entry:

    IFS[{T__TransformationFunction}][pl_List] := Join @@ Through[{T}[pl]]
TransformIFS[g_, IFS[l_List]] :=
Module[{prim = First[g], h = Head[g]},
t = Table[GeometricTransformation[prim, l[[i]]], {i, Length[l]}];
h[t]];
TransformIFS[g_, ifs_IFS, 0] := g;
TransformIFS[g_, ifs_IFS, 1] := TransformIFS[g, ifs];
TransformIFS[g_, ifs_IFS, n_Integer?Positive] :=
TransformIFS[TransformIFS[g, ifs], ifs, n - 1];
With[{\[ScriptCapitalD] = DiagonalMatrix[{1, 1}/2]}(*可将原图形缩小1/2*),
IFS[{AffineTransform[{\[ScriptCapitalD]}],
AffineTransform[{\[ScriptCapitalD], {1/2, 0}}],
AffineTransform[{\[ScriptCapitalD], {1/4, Sqrt[3]/4}}]}]];
Table[TransformIFS[
Graphics[Polygon[{{1/2, 0}, {0, Sqrt[3]/2}, {-1/2,
0}}]](*这里需要边长为1的三角形*), SierpinskiGasket, n], {n, 0, 5}]


Or

ruleRectangleNest[{xl_, yl_}, {xr_, yr_}, {xu_, yu_}, k_ /; k >= 1] :=
Module[{cl = {(xl + xu)/2, (yl + yu)/2},
cr = {(xr + xu)/2, (yr + yu)/2}, cd = {(xl + xr)/2, (yl + yr)/2},
intI = k},
If[intI <= 1,
Graphics@
Triangle[{{{xu, yu}, cl, cr}, {cl, {xl, yl}, cd}, {cr,
cd, {xr, yr}}}], intI--;
Show[{ruleRectangleNest[{xu, yu}, cl, cr, intI],
ruleRectangleNest[cl, {xl, yl}, cd, intI],
ruleRectangleNest[cr, cd, {xr, yr}, intI]}]]]
ruleRectangleNest[{-1, 0}, {1, 0}, {0, Sqrt[3]}, 3]


You can also search the community for entry Sierpinski to see other people's code.

If you do not want to use SierpinskiMesh in the comment but prefer to build it from scratch ...

First, let us implement the process from one triangle to three, triplize in the below codes. One triangle can be represented by a $$(1\times3\times2)$$-shaped data, so we need to turn it into a $$(3\times3\times2)$$-shaped data, with a half scaling included. Then one needs to repeat this process $$n$$ times, by using Nest, which I think is more suitable for iterations, and finally obtain a $$(3^n\times3\times2)$$-shaped data, where $$n = 0, 1, 2, ...$$.

Clear[sierpinski2d]
sierpinski2d[tripts_ /; Dimensions[tripts] == {1, 3, 2}, n_Integer /; n >= 0] :=
Module[{triplize, mats},
mats = {{{1, 0, 0}, {1/2, 1/2, 0}, {1/2, 0, 1/2}},
{{1/2, 1/2, 0}, {0, 1, 0}, {0, 1/2, 1/2}},
{{1/2, 0, 1/2}, {0, 1/2, 1/2}, {0, 0, 1}}};
triplize = Flatten[mats.# & /@ #, {{1, 2}}] &;
Nest[triplize, tripts, n]
]
sierpinski2d[{CirclePoints[3]} // N, 10];
Graphics[Triangle[%]]


Update

Here I detail what triplize can do. Given the starting single group of three points $$\{\{\vec p_1, \vec p_2, \vec p_3\}\}$$, which is $$(1\times3\times2)$$-shaped, and note the deliberately kept first dimension. After the application, once, of triplize one gets three groups of three points $$\{\{\vec p_1, (\vec p_1 + \vec p_2)/2, (\vec p_3 + \vec p_1)/2\}, \\ \{(\vec p_1 + \vec p_2)/2, \vec p_2, (\vec p_2 + \vec p_3)/2\}, \\ \{(\vec p_3 + \vec p_1)/2, (\vec p_2 + \vec p_3)/2, \vec p_3\}\},$$ $$(3\times3\times2)$$-shaped. So it generates middle points and properly group them up.

Then for every group of three points, repeat this process.

• Strange, yesterday your code can run perfectly, but today to execute your code, reported an error: 坐标\$CellContextsierpinski2d[{{{0.8660254037844386, -0.5}, {0., 1.}, {-0.8660254037844386, -0.5}}}, 10]应该是一对数值，或是Scaled或 Offset的形式. We should use n_NonNegativeIntegers instead of n_?MemberQ[NonNegativeIntegers] to avoid reporting errors. Commented Nov 19, 2020 at 3:40
• @Alittlemouseonthepampas Please check this version. Commented Nov 19, 2020 at 3:50
• This algorithm is very simple, I still don't understand the working principle, thank you. Commented Nov 19, 2020 at 3:59
• @Alittlemouseonthepampas I hope my update can facilitate understanding. Commented Nov 19, 2020 at 7:10
• @Alittlemouseonthepampas I believe AffineTransform can do the work but I do not dig into it. Also, Paul Wellin 2013 mentions Scale and Translate for a similar problem. Commented Nov 19, 2020 at 7:27