CGI trees

cg_trees

Tonight, I got inspired by a post on reddit about the canvas widget that modern browsers support and also, by some stuff I’ve seen over the last year or so at Cambrian games (here is the cool canvas javascript app: Clicky. It’s pretty neat, check it out.)

Anyway, I decided I should try to make a “tree generating algorithm.” The usual gdk line drawing suspects were a bit too weak for this, so I started looking into cairo, and ended up writing a little GTK + cairo app that generated the image you see above.

Here’s how the app works: I created this notion of “cells.” Each cell is a square, which has a position (x, y coordinate), direction (angle) size, “turning,” and age. In the beginning of the program, there are just three cells, with size = 1, direction = up, (in radians), age = 0. With each iteration of the simulation, I scan each cell. Thee is some probability that each cell will spawn a new cell, with some limits based on the cells age (cells too young, or too old can’t make new cells.) The new cells inherit the parent cell’s direction with some small modification, partly random, partly the parent’s “turning.” The coloration is based on the cells age, so younger cells are lighter and older cells are darker. As cells age, they become bigger, to some limit.

That’s pretty much the whole algorithm, and that alone is enough to generate a picture like you see above.

This is my favorite kind of programming project. Small enough to do in one night, but big enough to get some really cool looking output. Damn computers are so fucking cool sometimes. This is one of those times.

~ by scaryreasoner on October 13, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: