Skip to content

Instantly share code, notes, and snippets.

@adekau
Last active March 29, 2020 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adekau/59d2935d3b52bffa61be37539fa2202f to your computer and use it in GitHub Desktop.
Save adekau/59d2935d3b52bffa61be37539fa2202f to your computer and use it in GitHub Desktop.
Llama using Javascript (Typescript)
import * as M from 'mathjs';
let points = [];
let pts = [];
let complexPts = [];
let coeffs = [];
let time = 0;
export class Llama {
public load() {
pts = await this.sampleSvgPoints(text, this.samples);
complexPts = pts.map(pt => ptToComplex(pt));
coeffs = dft(complexPts);
}
private parameterize(cn: M.Complex[]) {
return async (t: number): Promise<M.Complex> => await this.sigma(k =>
M.multiply(M.complex(cn[k - 1].re, cn[k - 1].im), M.exp(M.multiply(M.i, (k - 1) * t))), [1, this.samples]) as M.Complex;
}
private async sigma(exp: (k: number) => M.MathType, interval: [number, number]): Promise<M.MathType> {
let sum: M.MathType = 0;
for (let i = interval[0]; i <= interval[1]; i++)
sum = M.add(sum, exp(i));
return sum;
}
public setup() {
createCanvas(800,600);
}
public draw(p) {
const pFn = this.parameterize(coeffs);
p.background(25);
p.translate(300, 200);
const pt = await pFn(time);
points.unshift(p.createVector(pt.re, pt.im));
p.stroke(230);
p.noFill();
p.beginShape();
for (let i =0 ; i < points.length; i++)
p.vertex(points[i].x, points[i].y);
p.endShape();
if (time > M.pi * 2)
points.pop();
time += ((2 * M.pi) / pts.length);
}
}
@adekau
Copy link
Author

adekau commented Mar 29, 2020

Full code for this is available as https://github.com/adekau/fourier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment