Here’s some code to draw a simple fractal tree using a recursive function.
The sparki doesn’t seem to back up exactly where it should, so I had to tune it by trial and error to add an extra 0.5 degree (left 30, right 60, left 30 doesn’t end up where it started). With the recursive nature, it adds up to quite a bit by the end. Yours may or may not need tweaking…
Also, anyone know how big the stack is on sparki? I’m sure it would fail at some point, but I did run it with MAX_LEV set to 7 and all was fine. Would you get some indication or would it just go wacko?
[code]
#include <Sparki.h> // include the sparki library
int MAX_LEV = 6;
float extra = -0.5; //try to correct for angle error
void setup()
{
drawTree(0);
sparki.beep();
}
void loop()
{
}
void drawTree(int l)
{
int limb=5-l;
if(limb<1) limb=1;
sparki.moveForward(limb);
if(l<MAX_LEV) //gotta stop recursing eventually…
{
sparki.moveLeft(30);
drawTree(l+1);
sparki.moveRight(60);
drawTree(l+1);
sparki.moveLeft(30+extra);
}
sparki.moveBackward(limb);
}[/code]