Fouriertletoy

A start for drawing 'vector' images using Fourier series of functions of i
From 3Blue1Browns on Fourier series youtube.com/watch?v=r6sGWTCMz2k

Log in to post a comment.

function FourierSeries(series) {
    comp = series.pop();
    return new FourierComponent(comp[0], comp[1], comp[2], series.length > 0? FourierSeries(series): null);
}

function FourierComponent(radius, phase, rotation, child = null) {
    this.r = radius;
    this.p = phase / (2 * Math.PI);
    this.o = rotation;
    
    this.c = child;
}
FourierComponent.prototype.transpose = function(i) {
    return (
        this.o + (
            (i / 3600) * (2 * Math.PI)
        )
    ) / this.p;
}
FourierComponent.prototype.getX = function(i) {
    return (this.r * Math.cos( this.transpose(i) ) ) + (this.c === null? 0: this.c.getX(i));
}
FourierComponent.prototype.getY = function(i) {
    return (this.r * Math.sin( this.transpose(i) ) ) + (this.c === null? 0: this.c.getY(i));
}
FourierComponent.prototype.setChild = function(child) {
    this.c = child;
    return this;
}

function wait(ms) {
  var start = new Date().getTime();
  do { var end = new Date().getTime(); } while(end < start + ms);
}

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(.25);

// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();

var fc = FourierSeries([
      [70, Math.PI / 2, 0]
    , [20, Math.PI * 50, 0]
    , [5, Math.PI / 20, 0]
    , [1, Math.PI / 300, 0]
]);

/*var fc = new FourierComponent(70, 2 * Math.PI, 0,
            new FourierComponent(20, Math.PI / 25, 0,
                new FourierComponent(5, Math.PI*2, 0,
                    new FourierComponent(1, Math.PI*300, 0)
                )
            )
        );
*/

// The walk function will be called until it returns false.
function walk(i) {
    turtle.goto(fc.getX(i), fc.getY(i));

    if(i == 0) {
        turtle.pendown();
    }
    //wait(.1);
    return i < 360000 / 4;
}