-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
151 lines (135 loc) · 3.04 KB
/
sketch.js
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
148
149
150
151
var angle;
var axiom;
var sentence = "";
var len = 100;
var type;
var w=1440;
var h=800;
function generate() {
rules[type]["len"]*=rules[type]["scl"];
console.log(type);
var nextSentence = "";
for (var i = 0; i < sentence.length; i++) {
var current = sentence.charAt(i);
var found = false;
for (var j = 0; j < rules[type]["system"].length; j++) {
// console.log(type,rules[type]["system"][j]);
if (current == rules[type]["system"][j].a) {
found = true;
nextSentence += rules[type]["system"][j].b;
break;
}
}
if (!found) {
nextSentence += current;
}
}
sentence = nextSentence;
// createP(sentence);
turtle();
}
function turtle() {
background(0);
resetMatrix();
translate(rules[type]["transX"], rules[type]["transY"]);
stroke(255);
strokeWeight(2);
var r=random(255);
var g=random(255);
var b=random(255);
for (var i = 0; i < sentence.length; i++) {
stroke((r++)%255,(g++)%255,(b++)%255, 100);
var current = sentence.charAt(i);
if (current == "F" || current == "G") {
line(0, 0, 0, -rules[type]["len"]);
translate(0, -rules[type]["len"]);
} else if (current == "+") {
rotate(angle);
} else if (current == "-") {
rotate(-angle)
} else if (current == "[") {
push();
} else if (current == "]") {
pop();
}
}
}
function setup() {
var button = createButton("generate");
button.mousePressed(generate);
var b1 = createButton("clear");
b1.mousePressed(function(){
background(0);
});
var b2 = createButton("Tree");
b2.mousePressed(function(){
changeType("Tree");
});
var b3 = createButton("Hilbert");
b3.mousePressed(function(){
changeType("Hilbert");
});
var b4 = createButton("Sierpinski");
b4.mousePressed(function(){
changeType("Sierpinski");
});
var b5 = createButton("Koch");
b5.mousePressed(function(){
changeType("Koch");
});
var b6 = createButton("Dragon");
b6.mousePressed(function(){
changeType("Dragon");
});
var b7 = createButton("Quadratic");
b7.mousePressed(function(){
changeType("Quadratic");
});
var b8 = createButton("32-Segment");
b8.mousePressed(function(){
changeType("32-Segment");
});
var b9 = createButton("Arrowhead");
b9.mousePressed(function(){
changeType("Arrowhead");
});
var b10 = createButton("Peano-Gosper");
b10.mousePressed(function(){
changeType("Peano-Gosper");
});
var b11 = createButton("Peano");
b11.mousePressed(function(){
changeType("Peano");
});
button.position(0, 19);
b1.position(70, 19);
b2.position(110, 19);
b3.position(150, 19);
b4.position(200, 19);
b5.position(270, 19);
b6.position(320, 19);
b7.position(380, 19);
b8.position(450, 19);
b9.position(530, 19);
b10.position(600, 19);
b11.position(690, 19);
createCanvas(w, h);
type="Tree";
angle = radians(rules[type]["angle"]) ;
axiom = rules[type]["axiom"];
sentence=axiom;
len = rules[type]["len"];
background(0);
// createP(axiom);
turtle();
}
function changeType(t){
type=t;
background(0);
console.log(type);
angle = radians(rules[type]["angle"]) ;
axiom = rules[type]["axiom"];
sentence=axiom;
len = rules[type]["len"];
turtle();
}