0
$\begingroup$

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):

enter image description here

$\endgroup$
1

2 Answers 2

2
$\begingroup$

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];
SierpinskiGasket = 
  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.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
7
  • $\begingroup$ Strange, yesterday your code can run perfectly, but today to execute your code, reported an error: 坐标$CellContext``sierpinski2d[{{{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. $\endgroup$ Commented Nov 19, 2020 at 3:40
  • 1
    $\begingroup$ @Alittlemouseonthepampas Please check this version. $\endgroup$ Commented Nov 19, 2020 at 3:50
  • $\begingroup$ This algorithm is very simple, I still don't understand the working principle, thank you. $\endgroup$ Commented Nov 19, 2020 at 3:59
  • 1
    $\begingroup$ @Alittlemouseonthepampas I hope my update can facilitate understanding. $\endgroup$ Commented Nov 19, 2020 at 7:10
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Nov 19, 2020 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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