How about grow a
binary tree Code:
BTree btree;
void setup(){
size(400, 400);
btree = new BTree(5, height / 2, 10);
smooth();
ellipseMode(CENTER);
}
void draw(){
background(255);
btree.draw();
}
class BTree{
int depth;
Vec2 [] node;
BTree(float x, float y, int depth){
this.depth = depth;
node = new Vec2[(int)pow(2,depth + 1)];
node[1] = new Vec2(x, y);
for(int i = 1; i < node.length - (int)pow(2, depth); i++){
float xn = node[i].x + cos(random(1.0)) * (50.0);
float yn = node[i].y + sin(random(1.0)) * (50.0);
node[2 * i] = new Vec2(xn, yn);
xn = node[i].x + cos(-random(1.0)) * (50.0);
yn = node[i].y + sin(-random(1.0)) * (50.0);
node[1 + 2 * i] = new Vec2(xn, yn);
}
}
void draw(){
for(int i = 1; i < node.length - (int)pow(2, depth); i++){
ellipse(node[i].x, node[i].y, 10, 10);
line(node[i].x, node[i].y, node[2 * i].x, node[2 * i].y);
line(node[i].x, node[i].y, node[1 + 2 * i].x, node[1 + 2 * i].y);
}
}
}
static class Vec2{
float x,y;
Vec2(float x, float y){
this.x = x;
this.y = y;
}
}
I think the
Complete Binary Tree has scope for being polished up into some nice and to the point little applets and Flash objects. I haven't quite figured out how to calculate the depth of a node, but I thought perhaps that's something for other people to expand on.
I don't mind if someone posts a more compact or easier to read version of this code. I'm not exactly a poet when it comes to syntax.