-
Notifications
You must be signed in to change notification settings - Fork 0
/
SKETCH 1 CODE
147 lines (97 loc) · 2.06 KB
/
SKETCH 1 CODE
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//L SYSTEM
//tree color
var r = 200
var g = 200
var b = 255
// change backgrund color
let slider;
//axiom: F
//Rules: F=C0FF-[C1-F+F+F]+[C2+F-F-F]
var angle;
var axiom = "F";
var sentence = axiom;
var len = 100;
let pressedtime = 0
var rules = [];
rules[0] = {
a: "F",
b: "FF+[+F-F-F]-[-F+F+F]"
}
function generate() {
pressedtime += 1
print(pressedtime)
len *= 0.55;
var nextSentence = "";
for (var i = 0; i < sentence.length; i++) {
var current = sentence.charAt(i);
var found = false;
for (var j = 0; j < rules.length; j++) {
if (current == rules[j].a) {
found = true;
nextSentence += rules[j].b;
break;
}
}
if (!found) {
nextSentence += current;
}
}
sentence = nextSentence;
drawing();
}
function drawing() {
//background(51);
resetMatrix();
translate(width / 2, height);
for (var i = 0; i < sentence.length; i++) {
var current = sentence.charAt(i);
if (current == "F") {
noFill();
stroke(r, g, b,40);
line(0, 0, 0, -len);
translate(0, -len);
} else if (current == "+") {
rotate(angle);
} else if (current == "-") {
rotate(-angle);
} else if (current == "[") {
push();
} else if (current == "]") {
pop();
}
}
}
function setup() {
createCanvas(400, 400);
pixelDensity(1);
noiseDetail(5);
noFill();
//Slider
slider = createSlider(0, 255, 100);
slider.position(450, 10);
slider.style("width", "110px");
//
drawing();
angle = radians(30);
var button = createButton("GROW TREE");
button.mousePressed(generate);
}
function draw() {
let scale = 0.01
let val = slider.value()
background(50,110,val);
loadPixels();
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
col = 255 * noise(scale * x, scale * y, 5 * scale * frameCount);
var index = (x + y * width) * 4
pixels[index] = col;
}
}
updatePixels();
drawing();
textSize(20)
stroke(0)
fill("black")
text('"WORLD TREE"', -70,-len);
}