Message Boards Message Boards

Sierpinski triangle code

Posted 8 years ago

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]
 ]

enter image description here

POSTED BY: Jason Biggs
6 Replies

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

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

enter image description here

POSTED BY: Vitaliy Kaurov

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]

enter image description here

Cheers,

Marco

POSTED BY: Marco Thiel

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? ;-)

enter image description here

POSTED BY: Vitaliy Kaurov

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?

POSTED BY: Jason Biggs

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!

POSTED BY: Moderation Team
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract