diff --git a/forest/main.c b/forest/main.c new file mode 100644 index 0000000..52cf90b --- /dev/null +++ b/forest/main.c @@ -0,0 +1,53 @@ + +#include + +#include "tree.h" + + +#define COUNT 10 + +double time; +Tree trees[COUNT]; + + +void generate() { + for(int i = 0; i < COUNT; ++i) + treeGenerate(&trees[i]); +} + + +void init() { + generate(); +} + + +void draw() { + time += worldGetFrameTime(); + int w = worldGetWidth(); + int h = worldGetHeight(); + + if (keyWentDown("space")) generate(); + + saveState(); + translate(0.5*w, 0.5*h); + zoom(10); + scale(1, -1); + + for(int i = 0; i < COUNT; ++i) + treeDraw(&trees[i], time, TRUE, FALSE); + for(int i = 0; i < COUNT; ++i) + treeDraw(&trees[i], time, FALSE, TRUE); + + restoreState(); +} + + +int main() { + worldSetVariableFrameRate(); + worldSetResizable(TRUE); + worldSetInit(&init); + worldSetDraw(&draw); + worldRun(); + return 0; +} + diff --git a/forest/tree.c b/forest/tree.c index 053356d..8339fd9 100644 --- a/forest/tree.c +++ b/forest/tree.c @@ -1,34 +1,34 @@ -#include #include -#include - +#include +#include +#include +#include "tree.h" -int seed; -double t; -const double segmentLength = 5; -const double segmentWidth = 0.05; -const double minWidth = 0.5; -const double branchesPerSegment = 0.2; -const double leafSize = 200; -const double leafPerBranch = 1; -const double leafThreshold = 1 + 200/5*0.05/4*0.5; +static const double segmentLength = 0.2; +static const double segmentWidth = 0.004; +static const double minWidth = 0.02; +static const double rootWidth = 0.8; +static const double branchesPerSegment = 0.1; +static const double leafSize = 8; +static const double leafPerBranch = 1; +static const double leafThreshold = 0.02 + 8/4/0.2*0.004; // minWidth + leafSize/4/segmentLength*segmentWidth -double randomRange(double min, double max) +static double randomRange(double min, double max) { return randomFloat()*(max - min) + min; } -double randomIncrement(double deviation) +static double randomIncrement(double deviation) { return randomRange(-deviation, deviation); } -double randomAmplifier(double deviation) +static double randomAmplifier(double deviation) { return randomRange(1 - deviation, 1 + deviation); } -void leaf(double x, double y, double size) { +static void leaf(double x, double y, double size) { double r = 0.5*size;//*randomAmplifier(0.5); saveState(); noStroke(); @@ -37,8 +37,8 @@ void leaf(double x, double y, double size) { } -void branch(double x, double y, double angle, double width) { - double da = sin(t*randomRange(0.5, 1) + randomIncrement(PI)); +static void branch(double x, double y, double angle, double width, double time) { + double da = sin(time*randomRange(0.5, 1) + randomIncrement(PI)); saveState(); while(TRUE) { @@ -78,7 +78,7 @@ void branch(double x, double y, double angle, double width) { width = sqrt(width*width*(1-k)); double a = randomNumber(0, 1) ? angle - 90 + randomRange(0, 45) : angle + 90 - randomRange(0, 45); - branch(x, y, a, w); + branch(x, y, a, w, time); } } } @@ -86,54 +86,34 @@ void branch(double x, double y, double angle, double width) { } -void tree(int seed, double x, double y, double width) { - srand(seed); - double angle = 90 + randomIncrement(30); - width *= randomAmplifier(0.5); - int s = randomNumber(0, 1000000); - - saveState(); - noStroke(); - srand(s); - branch(x, y, angle, width); - restoreState(); - - saveState(); - noFill(); - srand(s); - branch(x, y, angle, width); - restoreState(); -} - - -void init() { - seed = randomNumber(0, 1000000); +void treeGenerate(Tree *t) { + memset(t, 0, sizeof(*t)); + t->seed = rand(); + t->x = randomIncrement(20); + t->y = randomIncrement(20); + t->angle = 90 + randomIncrement(30); + t->width = rootWidth*randomAmplifier(0.5); } -void draw() { - double w = worldGetWidth(); - double h = worldGetHeight(); - t += worldGetFrameTime(); - - if (keyWentDown("space")) seed = randomNumber(0, 1000000); - +void treeDraw(const Tree *t, double time, int leafs, int branches) { saveState(); - translate(w/2, h*0.9); - scale(1, -1); + translate(t->x, t->y); - fill(colorByName("green")); - stroke(colorByName("black")); - tree(seed, 0, 0, 20); + if (leafs) { + fill(colorByName("green")); + noStroke(); + srand(t->seed); + branch(0, 0, t->angle, t->width, time); + } + + if (branches) { + noFill(); + stroke(colorByName("black")); + srand(t->seed); + branch(0, 0, t->angle, t->width, time); + } restoreState(); } - -int main() { - worldSetVariableFrameRate(); - worldSetResizable(TRUE); - worldSetInit(&init); - worldSetDraw(&draw); - worldRun(); -} diff --git a/forest/tree.h b/forest/tree.h index e69de29..00e96e5 100644 --- a/forest/tree.h +++ b/forest/tree.h @@ -0,0 +1,17 @@ +#ifndef TREE_H +#define TREE_H + + +typedef struct { + int seed; + double x, y; + double angle; + double width; +} Tree; + + +void treeGenerate(Tree *t); +void treeDraw(const Tree *t, double time, int leafs, int branches); + + +#endif