xxxxxxxxxx
93
// Coding Challenge 130.3: Drawing with Fourier Transform and Epicycles
// Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/130.1-fourier-transform-drawing.html
// https://thecodingtrain.com/CodingChallenges/130.2-fourier-transform-drawing.html
// https://thecodingtrain.com/CodingChallenges/130.3-fourier-transform-drawing.html
// https://youtu.be/7_vKzcgpfvU
let data;
function preload() {
data = loadStrings('fourier.txt');
}
let x = [];
let fourierX;
let time = 0;
let path = [];
function setup() {
createCanvas(800, 600);
const skip = 1;
// for (let i = 0; i < data.drawing.length; i += skip) {
// const c = new Complex(data.drawing[i].x, data.drawing[i].y);
// x.push(c);
// }
for (let i = 0; i < data.length; i += skip) {
const s = data[i].split(',');
const px = parseFloat(s[0] / 2);
const py = parseFloat(s[1] / 2);
const c = new Complex(px, py);
x.push(c);
}
const centroid = new Complex(0, 0);
for (let i = 0; i < x.length; i++) {
centroid.add(x[i]);
}
centroid.re /= x.length;
centroid.im /= x.length;
for (let i = 0; i < x.length; i++) {
x[i].re -= centroid.re;
x[i].im -= centroid.im;
}
fourierX = dft(x);
fourierX.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 v = epicycles(width / 2, height / 2, 0, fourierX);
path.unshift(v);
beginShape();
noFill();
for (let i = 0; i < path.length; i++) {
vertex(path[i].x, path[i].y);
}
endShape();
const dt = TWO_PI / fourierX.length;
time += dt;
if (time > TWO_PI) {
time = 0;
path = [];
}
}