#include "svg.h"

#define SQRT3_6 0.28867513459481288225457439025098

int color;
int dcolor = 1;

void mortonOrder(int n, double x0, double y0, double x2, double y2) {
    static int prevX=-1, prevY=-1;
    double x1 = (x0+x2)/2, y1 = (y0+y2)/2;
    if (n==0) {
        if (prevX!=-1 || prevY!=-1) {
            line(prevX, prevY, x1, y1);
            stroke(color = rotateH360(color, dcolor));
        }
        prevX = x1; prevY = y1;
        return;
    }  else {
        mortonOrder(n-1, x0, y0, x1, y1);
        mortonOrder(n-1, x1, y0, x2, y1);
        mortonOrder(n-1, x0, y1, x1, y2);
        mortonOrder(n-1, x1, y1, x2, y2);
    }
}

int main(void) {
    start();
    stroke(color = hsb360(0, 100, 100));
    strokeWeight(0.5);
    noFill();
    mortonOrder(6, 40, 30, 257, 180);
    finish();
    return 0;
}
