-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
168 lines (131 loc) · 3.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var r = 128; // radius of circle
var angle, step;
var steps;
var points = []; // this stores the points on the circle [x][y]
var opacity = 255; // opacity for lines
// html elements
var rangeSlider;
var alphaSlider;
var spanMod;
var spanOpacity;
var pickBg, pickLine;
var canvasA;
var cBackground, cLine; // colors
var maxNums = 32; // maxNums to calculate
var fibonacciNums = []; // array to hold fibonacci sequence
var modulo; // current mod (comes from slider)
var modFibNums = []; // array to hold fibonacci sequence modulos
var showPoints = true;
const fibonacci = n => { // fibonacci function: fibonacci(i) returns the number
let a = 0, b = 1, c = n;
for(let i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
};
function setup() {
canvasA = createCanvas(700, 500);
canvasA.parent('#canvasA');
// set colors
cBackground = color('#ebe600');
cLine = color('#140f00');
// get html
spanMod = select("#spanMod");
rangeSlider = select("#rangeSlider");
alphaSlider = select("#alphaSlider");
spanOpacity = select("#spanOpacity")
pickBg = select("#pickBg");
pickLine = select("#pickLine");
// set which functions the html elements trigger
rangeSlider.input(setPoints);
alphaSlider.input(setOpacity);
pickBg.input(setColors);
pickLine.input(setColors);
pickBg.value(rgbToHex(cBackground));
pickLine.value(rgbToHex(cLine));
// set sides/mod
setPoints();
// populate the fibonacciNums array with the numbers
for(i=0;i<maxNums;i++) {
append(fibonacciNums, fibonacci(i));
}
setOpacity();
console.log(fibonacciNums);
}
function draw() {
background(cBackground);
fill(cLine);
noStroke();
text(fibonacciNums,4,16);
text(modFibNums,4,42);
text("Pisano period: " + pisano(modulo),4,68)
fill(0,100);
text("Griff#1998 | github.com/im-griff", 4, 400);
// move 0,0 to the center of the screen
translate(width/2, height/2);
// set the colors
cLine.setAlpha(opacity);
stroke(cLine);
fill(cBackground);
strokeWeight(5);
// draw circle
ellipse(0,0,r*2);
// loop the length of the pisano period
for(i=0; i<pisano(modulo); i++) {
// the point used is the modulo (modFibNums[i]) and access x = [0] or y = [1]
if (modFibNums.length > 0) {
var xm = points[modFibNums[i]][0];
var ym = points[modFibNums[i]][1];
var xm2, ym2;
if (i != pisano(modulo) - 1) {
xm2 = points[modFibNums[i+1]][0];
ym2 = points[modFibNums[i+1]][1];
} else {
xm2 = points[0][0];
ym2 = points[0][1];
}
// draw the line
line(xm,ym, xm2, ym2);
}
}
cLine.setAlpha(255);
}
function setMod() {
// set the modulo based on that html slider
modFibNums = [];
modulo = rangeSlider.value();
spanMod.html(modulo);
for(i=0; i<pisano(modulo); i++) {
// populate the array modFibNums
append(modFibNums, fibonacci(i) % modulo)
}
}
function setPoints() {
setMod();
steps = rangeSlider.value();
// initialize variables
points = [];
angle = 0;
step = TWO_PI/steps; // in radians equivalent of 360/steps in degrees
for(i=0; i<steps; i++) {
// get points and translate them to x and y values
var x = r * sin(angle);
var y = r * cos(angle);
append(points, [x, y]);
angle = angle + step;
}
}
// helper functions
function setOpacity() {
opacity = alphaSlider.value();
spanOpacity.html(opacity);
}
function setColors() {
cBackground = color('' + pickBg.value());
cLine = color('' + pickLine.value());
}
function rgbToHex(_color) {
return "#" + hex(_color.levels[0],2) + hex(_color.levels[1],2) + hex(_color.levels[2],2);
}