-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft-drawing.js
267 lines (243 loc) · 7.82 KB
/
ft-drawing.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
var sketch_sawtooth = function (sketch) {
let time = 0;
let path = { x: [], y: [] };
let slider, canvas;
sketch.setup = function () {
canvas = sketch.createCanvas(
0.69 * sketch.windowWidth,
0.69 * sketch.windowHeight
);
slider = sketch.createSlider(1, 10, 1);
html_slider_value = document.getElementById("slider-value");
html_slider_value.style.position = "relative";
html_slider_value.style.left = 0.4 * canvas.width + "px";
html_slider_value.style.top = -0.1 * canvas.height + "px";
html_slider = document.getElementById("slider");
html_slider.style.position = "relative";
html_slider.style.left = 0.2 * canvas.width + "px";
html_slider.style.top = -0.1 * canvas.height + "px";
// attach to DOM with id=sawtooth
canvas.parent("sawtooth");
slider.parent("slider");
};
sketch.sawtooth = function (n) {
// returns fourier transform of (1/1)*sin(1*t) + (1/2)*sin(2*t) + ... + (1/n)*sin(n*t)
let t = 0;
let scale = 100;
if (n < 5) {
t = linspace(0, 2 * Math.PI, 100);
} else {
t = linspace(0, 2 * Math.PI, 25 * n);
}
let inp = [];
for (let i = 0; i < t.length; i++) {
inp[i] = new ComplexNumber(0, 0);
for (let j = 1; j <= n; j++) {
inp[i] = inp[i].add(
new ComplexNumber((scale / j) * Math.sin(j * t[i]), 0)
);
}
}
return discreteFourierTransform(inp);
};
sketch.draw = function () {
html_slider_value.innerHTML = "N = " + slider.value();
sketch.background(28, 28, 29);
const x_translate = canvas.width / 3;
const y_translate = canvas.height / 2;
sketch.translate(x_translate, y_translate);
let x = 0;
let y = 0;
let fourier = sketch.sawtooth(slider.value());
let V = epiCycles(x, y, Math.PI / 2, fourier, time, sketch);
path.x.unshift(V.x);
path.y.unshift(V.y);
sketch.translate(canvas.width / 4, 0);
sketch.beginShape();
sketch.noFill();
sketch.stroke(255);
for (let i = 0; i < path.y.length; i++) {
sketch.vertex(i, path.y[i]);
}
sketch.line(V.x - canvas.width / 4, V.y, 0, V.y);
sketch.endShape();
const dt = (2 * Math.PI) / fourier.length;
time += dt;
slider.changed(() => {
time = 0;
path = { x: [], y: [] };
});
if (path.x.length > 500) {
path.x.pop();
path.y.pop();
}
};
sketch.windowResized = function () {
sketch.resizeCanvas(
0.69 * sketch.windowWidth,
0.69 * sketch.windowHeight
);
html_slider_value = document.getElementById("slider-value");
html_slider_value.style.position = "relative";
html_slider_value.style.left = 0.2 * canvas.width + "px";
};
};
var sketch_batman = function (sketch) {
let time = 0;
let path = { x: [], y: [] };
let x_vals = [];
let y_vals = [];
let fourierX, fourierY, canvas;
sketch.setup = function () {
canvas = sketch.createCanvas(
0.69 * sketch.windowWidth,
0.69 * sketch.windowHeight
);
// attach to DOM with id=batman
canvas.parent("batman");
for (let i = 0; i < batman.length; i += 10) {
x_vals.push(new ComplexNumber(batman[i].x, 0));
y_vals.push(new ComplexNumber(batman[i].y, 0));
}
fourierX = discreteFourierTransform(x_vals);
fourierY = discreteFourierTransform(y_vals);
};
sketch.draw = function () {
sketch.background(28, 28, 29);
const x_translate = canvas.width / 3;
const y_translate = canvas.height / 2;
sketch.translate(x_translate, y_translate);
let Vx = epiCycles(
canvas.width / 10,
canvas.height / 5,
0,
fourierX,
time,
sketch
);
let Vy = epiCycles(
canvas.width / 2,
-canvas.height / 5,
Math.PI / 2,
fourierY,
time,
sketch
);
path.x.unshift(Vx.x);
path.y.unshift(Vy.y);
sketch.line(Vx.x, Vx.y, Vx.x, Vy.y);
sketch.line(Vy.x, Vy.y, Vx.x, Vy.y);
sketch.beginShape();
sketch.noFill();
sketch.stroke(255);
for (let i = 0; i < path.x.length; i++) {
sketch.vertex(path.x[i], path.y[i]);
}
sketch.endShape();
const dt = (2 * Math.PI) / fourierX.length;
time += dt;
if (path.x.length > 500) {
path.x.pop();
path.y.pop();
}
};
sketch.windowResized = function () {
sketch.resizeCanvas(
0.69 * sketch.windowWidth,
0.69 * sketch.windowHeight
);
};
};
var sketch_user = function (sketch) {
let USERINPUT = [];
let FOURIER = false;
let path = { x: [], y: [] };
let time = 0;
let fourier, canvas;
sketch.validMouse = function () {
if (
sketch.mouseX > 0 &&
sketch.mouseX < canvas.width &&
sketch.mouseY > 0 &&
sketch.mouseY < canvas.height
) {
return true;
}
return false;
};
sketch.setup = function () {
canvas = sketch.createCanvas(
0.69 * sketch.windowWidth,
0.69 * sketch.windowHeight
);
sketch.background(28, 28, 29);
sketch.textAlign(sketch.CENTER);
sketch.textSize(64);
sketch.fill(255);
sketch.textFont("inherit");
sketch.text("draw something", canvas.width / 2, canvas.height / 2);
// attach to DOM with id=user
canvas.parent("user");
};
sketch.draw = function () {
if (sketch.mouseIsPressed == true && sketch.validMouse()) {
sketch.background(28, 28, 29);
path = { x: [], y: [] };
time = 0;
USERINPUT.push(
new ComplexNumber(
sketch.mouseX - canvas.width / 2,
sketch.mouseY - canvas.height / 2
)
);
FOURIER = true;
sketch.stroke(255, 0, 255);
sketch.noFill();
sketch.beginShape();
for (let i = 0; i < USERINPUT.length; i++) {
sketch.vertex(
USERINPUT[i].real + canvas.width / 2,
USERINPUT[i].imaginary + canvas.height / 2
);
}
sketch.endShape();
fourier = discreteFourierTransform(USERINPUT);
}
if (FOURIER == true && sketch.mouseIsPressed == false) {
sketch.background(28, 28, 29);
USERINPUT = [];
let V = epiCycles(
canvas.width / 2,
canvas.height / 2,
0,
fourier,
time,
sketch
);
path.x.unshift(V.x);
path.y.unshift(V.y);
sketch.beginShape();
sketch.stroke(255);
sketch.noFill();
for (let i = 0; i < path.x.length; i++) {
sketch.vertex(path.x[i], path.y[i]);
}
sketch.endShape();
const dt = (2 * Math.PI) / fourier.length;
time += dt;
if (path.x.length > 1000) {
path.x.pop();
path.y.pop();
}
}
};
sketch.windowResized = function () {
sketch.resizeCanvas(
0.69 * sketch.windowWidth,
0.69 * sketch.windowHeight
);
};
};
new p5(sketch_sawtooth, "sawtooth");
new p5(sketch_batman, "batman");
new p5(sketch_user, "user");