-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsketch.js
75 lines (60 loc) · 1.58 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
// Coding Challenge 130.1: Drawing with Fourier Transform and Epicycles
// Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/130-fourier-transform-drawing.html
// https://youtu.be/MY4luNgGfms
let x = [];
let y = [];
let fourierX;
let fourierY;
let time = 0;
let path = [];
function setup() {
createCanvas(1000, 1200);
const skip = 1;
for (let i = 0; i < drawing.length; i += skip) {
x.push(drawing[i].x/5);
y.push(drawing[i].y/6);
}
fourierX = dft(x);
fourierY = dft(y);
fourierX.sort((a, b) => b.amp - a.amp);
fourierY.sort((a, b) => b.amp - a.amp);
}
function epiCycles(x, y, rotation, fourier) {
for (let i = 0; i < fourier.length; i++) {
let prevx = x;
let prevy = y;
let freq = fourier[i].freq;
let radius = fourier[i].amp;
let phase = fourier[i].phase;
x += radius * cos(freq * time + phase + rotation);
y += radius * sin(freq * time + phase + rotation);
stroke(255, 100);
noFill();
ellipse(prevx, prevy, radius * 2);
stroke(255);
line(prevx, prevy, x, y);
}
return createVector(x, y);
}
function draw() {
background(0);
let vx = epiCycles(width / 2 + 100, 100, 0, fourierX);
let vy = epiCycles(100, height / 2 + 100, HALF_PI, fourierY);
let v = createVector(vx.x, vy.y);
path.unshift(v);
line(vx.x, vx.y, v.x, v.y);
line(vy.x, vy.y, v.x, v.y);
beginShape();
noFill();
for (let i = 0; i < path.length; i++) {
vertex(path[i].x, path[i].y);
}
endShape();
const dt = TWO_PI / fourierY.length;
time += dt;
// if (time > TWO_PI) {
// time = 0;
// // path = [];
// }
}