Sierpinski triangle code

Sometimes you can tell a question is probably going to be deleted, but it’s fun to look for an answer anyway… This is one of those cases, the question was deleted but here is the answer:

This link should cover your needs. J.M. wrote a nice compact function to create the Sierpinski triangle:

With[{n = 8}, 
 NestList[(# /. 
     poly : Polygon[pts_, ___] :> 
      Map[Function[p, Translate[Scale[poly, 1/2, {0, 0}], p/2]], 
       pts]) &, 
  Graphics[{Polygon[{{Sqrt[3]/2, -1/2}, {0, 
       1}, {-Sqrt[3]/2, -1/2}}]}], n]
 ]

4 Likes

One of my favorite Sierpinski triangle codes I got if I recollect correctly from @Andrew Moylan and @Taliesin Beynon. It goes like:

Sierpinski[r_][p_] := Map[Sierpinski[r - 1], Outer[Plus, p, p, 1]];
Sierpinski[0] = Polygon;
Graphics[Sierpinski[6][{{-1, 0}, {0, Sqrt[3]}, {1, 0}}]]

Now who can explain how the magic of the Wolfram Language code works? :wink:

4 Likes

Also take a look at the legendary the sierpinski triangle page to end most sierpinski triangle pages and some stats on it.

3 Likes

That’s great, it could win a code-golf competition.

BTW, I had posted this as a reply to a question, but now the question appears to have disappeared - was it deleted by the user?

Weak question with no effort sometimes getting removed. We sent a message to OP with recommendations and links. But you already have posted an answer and it is great so we keep it. Thanks!

You can also use stochastic approaches like here .

M = 40000; 
randompoints = Table[RandomChoice[{{0., 0.}, {1., 0.}, {0.5, Sqrt[3]/2 // N}}], {i, 1, M}]; results = {RandomReal[1, {2}]}; Table[ 
 AppendTo[results, 0.5*(randompoints[[i]] - results[[-1]]) + results[[-1]]], {i, 1, M}];
ListPlot[results, ImageSize -> Large, AspectRatio -> 1, Axes -> False]

Cheers,

Marco

3 Likes

Nice one @Marco Thiel ! Now I have to mention this one:

ArrayPlot[CellularAutomaton[90, {{1}, 0}, 2^6]]

2 Likes