-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.pde
73 lines (66 loc) · 2.15 KB
/
Tree.pde
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Tree implements TileObject {
PVector pos;
float age;
float treeAngle;
int treeHeight = 75;
PVector topPoint;
Tree(int x, int y, int z) {
this(x, y, z, random(.25, 1));
}
Tree(int x, int y, float z, float age) {
this(new PVector(x, y, z), age);
}
Tree(PVector v) {
this(v, random(.25, 1));
}
Tree(PVector v, float age) {
this.age = age;
this.pos = v;
this.treeAngle = PI/((int) random(1, 6));
this.topPoint = new PVector(this.pos.x, this.pos.y, this.pos.z+(treeHeight*this.age));
}
void update() {
// wind
float angle = (millis()/3+(this.pos.x))%360;
float strength = map((second()+this.pos.y)%100, 0, 100, -1, 1);
this.topPoint.x = this.pos.x + ((2*this.age * cos(radians(angle)))*strength);
this.topPoint.y = this.pos.y + ((2*this.age * sin(radians(angle)))*strength);
}
PVector getPosition() {
return this.pos;
}
void display2D() {
fill(20, 175, 10); //green
ellipse(map(this.pos.x, 0, GRID_WIDTH-CELL_SCALE, 0, width), map(this.pos.y, 0, GRID_HEIGHT-CELL_SCALE, 0, height), width*(((4*this.age)*2)/((float)GRID_WIDTH-CELL_SCALE))*2, height*(((4*this.age)*2)/((float)GRID_HEIGHT-CELL_SCALE))*2);
}
void display() {
fill(112, 74, 12);
// add cylindar
// draw body
int sides = 6;
float angle = 360 / sides;
float r = 4*this.age;
beginShape(TRIANGLE_STRIP);
for (int i = 0; i < sides + 1; i++) {
float x = cos( radians( i * angle ) ) * r;
float y = sin( radians( i * angle ) ) * r;
vertex( this.pos.x+x, this.pos.y+y, this.pos.z+(treeHeight*age)/3.75);
vertex( this.pos.x+x, this.pos.y+y, this.pos.z-(treeHeight*age)/10);
}
endShape(CLOSE);
sides = 10;
angle = 360 / sides;
lights();
// add leaves/branches
fill(20, 175, 10); //green
beginShape(TRIANGLE_STRIP);
for (int i = 0; i < sides + 1; i++) {
float x = cos( radians( i * angle ) ) * r*4.3;
float y = sin( radians( i * angle ) ) * r*4.3;
vertex( this.topPoint.x, this.topPoint.y, this.topPoint.z);
vertex( this.pos.x+x, this.pos.y+y, this.pos.z+(treeHeight*age)/4);
}
endShape(CLOSE);
noLights();
}
}