Sierpinsky node?
Hey beautiful brains,
I had this sudden urge to try and make a Sierpinsky fractal node that generates essentially:
https://mathigon.org/course/fractals/sierpinski#:~:text=The%20Sierpinski%20triangle%20is%20a,removed%20from%20its%20remaining%20area.&text=It%20can%20be%20created%20by,triangles%20out%20of%20its%20center.
Where you can feed it an integer for #generations of using a resample node subdividing points on a polygon (triangle) node.
Each generation effectively subdividing each line with a new point which then becomes coordinates for a new polygon (triangle)...
I failed.
Any advice greatly appreciated!
Nodebox file attached... (sad)
- sierpinski_original.ndbx.zip 1.92 KB
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by john on 17 Feb, 2022 06:28 AM
Julian,
Thanks for this fun challenge and for having the courage to post your valiant attempt.
The reason you had so much trouble is that Nodebox doesn't do recursion. It's an odd and frustrating lapse we have to live with. But in software, where there's a will there's almost always a way.
You can do recursion manually by creating a subnetwork that takes in one triangle and spits out three subtriangles. You can then simply chain together as many of these as you want. See attached screenshot and demo file.
The demo goes nine levels deep by simply chaining together nine Sierpinkski nodes. The result is a Sierpinski triangle with 19,683 subtriangles. If you want to go deeper, just add more Sierpinski nodes.
If you really want a parameter driven solution that lets you simply set a depth you can do that too, though it's a bit of hack. You need to chain together enough Sierpinski nodes to achieve a maximum depth - which will have to be fairly low (as you get north of about 100,000 subtriangles, NodeBox will slow to a crawl). You can then feed lines from each possible depth into some switch nodes, use a depth parameter to choose the depth, and wrap the whole thing in a bigger subnetwork.
You could do that, but I doubt it would be worth the bother. It's quite easy to chain these subnetworks together by hand (just copy paste to get more).
Great question! Keep them coming!
Happy Nodeboxing!
John
Support Staff 2 Posted by john on 17 Feb, 2022 06:52 AM
And here, just for fun, is what happens if you colorize those 19,683 subtriangles in hues ranging from red to violet. This shows the order in which the subtriangles get drawn.
Pretty!
John
3 Posted by Julian Gosper on 17 Feb, 2022 07:35 AM
Fantastic John! That explains it!
BTW - could you share that cool colour nodebox???
Support Staff 4 Posted by john on 17 Feb, 2022 08:31 AM
And here, for even more fun, is the triangle animated.
Incidentally, to do this animation I did end up making a parameterized version of the Sierpinski node. Code available upon request.
John
Support Staff 5 Posted by john on 17 Feb, 2022 08:35 AM
Hey Julian,
Just saw your note above. I guess you did request the code. So here it is (attached).
Perhaps you could take this basic concept and attack some other cool fractals.
John
Support Staff 6 Posted by john on 17 Feb, 2022 08:37 AM
Oops. Forgot to attach the animation. Here it is.