This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
136 lines (123 loc) · 3.18 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
// Barnsley Fern for Processing 3.4
let x = 0, y = 0, times = 0;
function mapit(number, type) {
//mapit(number, loworig, highorig, lowconversion, highconversion)
let loworig, highorig, lowconversion, highconversion;
if(type === "x"){
loworig = -2.1820, highorig = 2.6558;
lowconversion = 0, highconversion = width;
return lowconversion + (highconversion - lowconversion) * ((number - loworig) / (highorig - loworig));
}
else{
loworig = 0, highorig = 9.9983;
lowconversion = height, highconversion = 0;
return lowconversion + (highconversion - lowconversion) * ((number - loworig) / (highorig - loworig));
}
}
// creating canvas
function setup() {
//createCanvas(windowWidth-10, windowWidth-10);
createCanvas(windowHeight-10, windowHeight-10);
background(240);
stroke(34, 139, 34);
strokeWeight(5);
}
// setting stroke, mapping canvas and then plotting the points
function drawPoint() {
let px = mapit(x, "x");
let py = mapit(y, "y");
point(px, py);
}
// algorithm for calculating value of (n+1)th term of x and y based on the transformation matrices
function nextPoint() {
let nextX, nextY;
let r = random(1);
if (r < 0.01) {
nextX = 0;
nextY = 0.16 * y;
} else if (r < 0.86) {
nextX = 0.8 * x + 0.04 * y;
nextY = -0.04 * x + 0.85 * y + 1.54;
} else if (r < 0.93) {
nextX = 0.20 * x - 0.26 * y;
nextY = 0.23 * x + 0.22 * y + 0.9;
} else {
nextX = -0.15 * x + 0.28 * y;
nextY = 0.26 * x + 0.24 * y + 1.5;
}
/*
if (r < 0.01) {
nextX = 0;
nextY = 0.16 * y;
} else if (r < 0.86) {
nextX = 0.85 * x + 0.04 * y;
nextY = -0.04 * x + 0.85 * y + 1.6;
} else if (r < 0.93) {
nextX = 0.20 * x - 0.26 * y;
nextY = 0.23 * x + 0.22 * y + 1.6;
} else {
nextX = -0.15 * x + 0.28 * y;
nextY = 0.26 * x + 0.24 * y + 0.44;
}
*/
x = nextX;
y = nextY;
}
// iterate the plotting and calculation functions over a loop
function draw() {
if(times < 10000){
for (let i = 0; i < 100; i++) {
drawPoint();
nextPoint();
}
times++;
}
}
/*
Barnsley Fern for Processing 3.4
// declaring variables x and y
float x, y;
// creating canvas
void setup() {
size(600, 600);
background(255);
}
setting stroke, mapping canvas and then
plotting the points
void drawPoint() {
stroke(34, 139, 34);
strokeWeight(1);
float px = map(x, -2.1820, 2.6558, 0, width);
float py = map(y, 0, 9.9983, height, 0);
point(px, py);
}
algorithm for calculating value of (n+1)th
term of x and y based on the transformation
matrices
void nextPoint() {
float nextX, nextY;
float r = random(1);
if (r < 0.01) {
nextX = 0;
nextY = 0.16 * y;
} else if (r < 0.86) {
nextX = 0.85 * x + 0.04 * y;
nextY = -0.04 * x + 0.85 * y + 1.6;
} else if (r < 0.93) {
nextX = 0.20 * x - 0.26 * y;
nextY = 0.23 * x + 0.22 * y + 1.6;
} else {
nextX = -0.15 * x + 0.28 * y;
nextY = 0.26 * x + 0.24 * y + 0.44;
}
x = nextX;
y = nextY;
}
iterate the plotting and calculation
functions over a loop
void draw() {
for (int i = 0; i < 100; i++) {
drawPoint();
nextPoint();
}
}*/