Nice code for Sierpinski

This is not a question but I just wanted to share some nice code I was able
to write to create the Sierpinski carpet.
Nothing new probably but it seemed pretty cool to me,
I also formatted it in a fancy way to match the graph result:
https://www.desmos.com/geometry/wrr3yzgz90

(This is for a lesson I’m gonna be giving about fractals …)

wow :).
(not because I saw you had no replies and replied something stupid to you, definitely not)

2 Likes

Kind of necro-posting but how did you do that?

so, we use recurrence:
s(0) is a triangle T

g(n, k) shrinks the set s towards the k-th vertex of triangle T
g(n,k) = dilate(s(n-1), T.vertices[k], .5)

s(n) joins what you get from shrinking s towards the three vertices:
s(n) = (g(n,1), g(n,2), gt(n,3))

finally you want to build s(5).

For a more clear explanation see: ifs.

In other words:
start with a triangle, shrink it towards one vertex, then towards the other vertex and then towards the other vertex.
You get a new set.
Now repeat the process with this new set.

1 Like

Yep! I have absolutely no idea what any of that means, especially dilate.

It’s probably because I never use the geometry tool (I know it’s very powerful thought)

ok, just to get you started:
dilate(X, p, 1/2) means something very easy:
for any point of the set X, connect it to p, and take the point half way in that line.

So you get a copy of X scaled by a factor 1/2 …

Oh! I see. very cool! I think I get it. It’s like midpoint in the calculator, but you can also do 1/3, 1/4 ...

Yes … and you can also do times 2 …

Ohhh, and the g(n, k) = {...} , .5) just does it half way each time!

1 Like