22 August 2010

Sierpinski triangles, oh my

<barrucadu> I recommend a fractal generator. It's nontrivial (even if you only do mandelbrot sets), and generates pretty output :)


Circadian rhythms are delicate things.

Mess with your circadian rhythm, and you may end up unable to sleep at night and unable to stay awake during the day.

Really mess with your circadian rhythm, and you could end up doing something like this.

It's a lightweight and quite cruddy Sierpinski triangle renderer I wrote for the express purpose of fulfilling the above life goal.

It's one letter away from being called "strangle"!
It uses C implementations of the Wikipedia pseudocode for Bresenham's line-drawing algorithm and an optimised boundary fill to draw and then fill in the triangles. Only integers are used directly - no floating point logic in the main code file. The x86_64 binary when compiled with clang is also only 14kB in size. Uselessness has never been so compact!

Unfortunately, it's not a terribly accurate renderer - the result is horrendously aliased (and with integer division always rounding to zero, holes are a bit further up sometimes than they should be) - and it's specifically coded to only work if the triangle bases are parallel to the X axis (this assumption allowed me to completely skip using trigonometry to draw the triangles). But hey! It draws graphics! It comes with a default colour set of seafoam on navy! What more could you possibly want!?

Possibly coming soon: something we can all use.


Also, yes, I use XFce.


EDIT: I realised somewhat late that there's little point to the renderer drawing the seafoam triangles over and over again when it's really the "hole" triangles that get changed each iteration, so I simplified the code somewhat. The renderer is now even smaller than ever (11kb stripped x86_64 clang), but the render quality has dropped a bit (since the holes are now eating mercilessly into the triangle without the triangle being able to defend itself with redraws):




The optimised (or not? you decide) renderer is here.

No comments: