-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.js
102 lines (96 loc) · 1.89 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
// var mouse;
var w = window.innerWidth;
var h = window.innerHeight;
var axiom = "FBFBFBFB";
var lf = 40;
var lb=20/Math.sqrt(2);
var sentence = axiom;
var angle;
// variables: FAB
// axiom: FBFBFBFB
// rules: A->AFBFA
// B->AFBFBFBFA
var rules = [];
rules[0] = {
a: "A",
b: "AFBFA"
}
rules[1] = {
a: "B",
b: "AFBFBFBFA"
}
function setup() {
createCanvas(w,h);
background('#800000');
angle = radians(45);
}
function draw(){
for (var layers = 0; layers < 3; layers++){
turtle(sentence);
// console.log(layers);
sentence = generate(sentence);
}
}
function turtle(sentence){
// console.log('turtle');
resetMatrix();
translate(width/2, height/2);
for (var i = 0; i < sentence.length; i++){
var current = sentence.charAt(i);
if (current == "F"){
stroke("white");
line(mouseX,mouseY, mouseX, mouseY-lf);
strokeWeight(4);
translate(mouseX, mouseY-lf);
}
else if (current == "A"){
stroke("white");
for(var a=0;a>radians(-90);a-=0.05){
var r=lf;
var x=r*cos(a);
var y=r*sin(a);
}
strokeWeight(4);
translate(x-r,y);
rotate(radians(-90));
}
else if (current == "B"){
stroke("white");
line(mouseX,mouseY, mouseX, mouseY-lb);
strokeWeight(4);
translate(mouseX, mouseY-lb);
for(var a=0;a>radians(-270);a-=0.05){
var r=lb;
var x=r*cos(a);
var y=r*sin(a);
point(x-r,y);
}
translate(x,y);
rotate(radians(-270));
line(x,y, x, y-lb);
strokeWeight(4);
translate(x, y-lb);
}
}
}
function generate(){
// console.log('generate');
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;
}
}
// createP(sentence);
// turtle();
return nextSentence;
}