Hilbert curve

An approach to wrap a line though all 'pixels' of an n-th order Hibert space: a Hilbert curve
en.wikipedia.org/wiki/Hilbert_curve
youtube.com/watch?v=3s7h2MHQtxc

Edit: turtletoy.net/user/ksons implementation: turtletoy.net/turtle/59ccf67626

Log in to post a comment.

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

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

let order = 6; // min=1, max=10, step=1
let size = 180; // min=50, max=200, step=10

turtle.penup();
//turtle.goto(-50, 50);
//turtle.pendown();

function HilbertSpace(x, y, width, height, type, first = true) {
    this.x = x;
    this.y = y;
    this.w = width;
    this.h = height;
    this.t = type;
    this.split = [];
    this.first = first;
    
}
HilbertSpace.A = 1;
HilbertSpace.B = 2;
HilbertSpace.C = 3;
HilbertSpace.D = 4;
HilbertSpace.prototype.draw = function(i) {
    if(this.split.length == 0) {
        switch(this.t) {
            case HilbertSpace.A:
                turtle.goto(this.x - (this.w / 4), this.y + (this.h / 4));
                turtle.pendown();
                turtle.goto(this.x - (this.w / 4), this.y - (this.h / 4));
                turtle.goto(this.x + (this.w / 4), this.y - (this.h / 4));
                turtle.goto(this.x + (this.w / 4), this.y + (this.h / 4));
            break;
            case HilbertSpace.B:
                turtle.goto(this.x + (this.w / 4), this.y - (this.h / 4));
                turtle.pendown();
                turtle.goto(this.x - (this.w / 4), this.y - (this.h / 4));
                turtle.goto(this.x - (this.w / 4), this.y + (this.h / 4));
                turtle.goto(this.x + (this.w / 4), this.y + (this.h / 4));
            break;
            case HilbertSpace.C:
                turtle.goto(this.x + (this.w / 4), this.y - (this.h / 4));
                turtle.pendown();
                turtle.goto(this.x + (this.w / 4), this.y + (this.h / 4));
                turtle.goto(this.x - (this.w / 4), this.y + (this.h / 4));
                turtle.goto(this.x - (this.w / 4), this.y - (this.h / 4));
            break;
            case HilbertSpace.D:
                turtle.goto(this.x - (this.w / 4), this.y + (this.h / 4));
                turtle.pendown();
                turtle.goto(this.x + (this.w / 4), this.y + (this.h / 4));
                turtle.goto(this.x + (this.w / 4), this.y - (this.h / 4));
                turtle.goto(this.x - (this.w / 4), this.y - (this.h / 4));
            break;
        }
        return;
    }
    this.split.forEach(p => p.draw());
}
HilbertSpace.prototype.nextOrder = function(level = 1) {
    if(level == 1) {
        return
    }
    if(this.split.length == 0) {
        switch(this.t) {
            case HilbertSpace.A:
                this.split.push( new HilbertSpace(this.x - (this.w / 4), this.y + (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.D, this.first) );
                this.split.push( new HilbertSpace(this.x - (this.w / 4), this.y - (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.A, false) );
                this.split.push( new HilbertSpace(this.x + (this.w / 4), this.y - (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.A, false) );
                this.split.push( new HilbertSpace(this.x + (this.w / 4), this.y + (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.B, false) );
            break;
            case HilbertSpace.B:
                this.split.push( new HilbertSpace(this.x + (this.w / 4), this.y - (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.C, this.first) );
                this.split.push( new HilbertSpace(this.x - (this.w / 4), this.y - (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.B, false) );
                this.split.push( new HilbertSpace(this.x - (this.w / 4), this.y + (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.B, false) );
                this.split.push( new HilbertSpace(this.x + (this.w / 4), this.y + (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.A, false) );
            break;
            case HilbertSpace.C:
                this.split.push( new HilbertSpace(this.x + (this.w / 4), this.y - (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.B, this.first) );
                this.split.push( new HilbertSpace(this.x + (this.w / 4), this.y + (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.C, false) );
                this.split.push( new HilbertSpace(this.x - (this.w / 4), this.y + (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.C, false) );
                this.split.push( new HilbertSpace(this.x - (this.w / 4), this.y - (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.D, false) );
            break;
            case HilbertSpace.D:
                this.split.push( new HilbertSpace(this.x - (this.w / 4), this.y + (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.A, this.first) );
                this.split.push( new HilbertSpace(this.x + (this.w / 4), this.y + (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.D, false) );
                this.split.push( new HilbertSpace(this.x + (this.w / 4), this.y - (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.D, false) );
                this.split.push( new HilbertSpace(this.x - (this.w / 4), this.y - (this.h / 4), this.w / 2, this.h / 2, HilbertSpace.C, false) );
            break;
        }
        this.first = false;
    }
    this.split.forEach(p => p.nextOrder(level - 1));
}

var hc = new HilbertSpace(0, 0, size, size, HilbertSpace.A);
hc.nextOrder(order);
//hc.draw();

// The walk function will be called until it returns false.
function walk(i) {
  hc.draw(i);
  return false;
}