-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtree.c
50 lines (41 loc) · 1008 Bytes
/
tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "opencscad.h"
double r(void)
{
return (double) rand() / (double) RAND_MAX;
}
#define TILTANGLE (25.0 + r() * 10.0)
#define ZANGLE (r() * 360.0)
#define HEIGHTFACT (r() * 0.3 + 0.6)
void tree(int level, double height);
void branch(int level, double tilt, double height, double x, double y, double z)
{
xlate(0, 0, height);
rotate(tilt, x, y, z);
rotate(ZANGLE, 0, 0, 1);
tree(level, height * HEIGHTFACT);
endrotate();
endrotate();
endxlate();
}
void tree(int level, double height)
{
cylinder(height, height / 18.0, height / 20.0);
if (level > 5)
return;
branch(level + 1, TILTANGLE, height, 1, 0, 0);
branch(level + 1, -TILTANGLE, height, 1, 0, 0);
branch(level + 1, TILTANGLE, height, 0, 1, 0);
branch(level + 1, -TILTANGLE, height, 0, 1, 0);
}
int main(int argc, char *argv[])
{
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
opencscad_init();
tree(0, 20.0);
finalize();
}