### Golden Squares

A T-Square fractal: en.wikipedia.org/wiki/t-square_(fractal)

```let depth = 10; //min=1, max=13, step=1
let goldenRatio = 1.618033988749895 //min=1.5, max=2.5, step=.01
let margin = 5; //min=0, max=30, step=5
let blackwhite = 0; //min=0, max=1, step=1

Canvas.setpenopacity((blackwhite * 2) - 1);

const turtle = new Turtle();

var width = 200 - (2 * margin);
var gr = goldenRatio;

var squares = [];

var widths = [100];
var total = 100;
for(var i = 1; i < depth; i++) {
widths.push( widths[widths.length - 1] / gr );
total += 2 * widths[widths.length - 1];
}
var ratio = width / total;
for(var i = 0; i < widths.length; i++) {
widths[i] *= ratio;
}

var corners = [ [ [-widths[0] / 2, -widths[0] / 2], widths[0], widths[0]] ];
function walk(i) {
var newCorners = [];
if(i == 0) {
newCorners.push([ [corners[i][0][0], corners[i][0][1]], -corners[i][1] / gr, -corners[i][2] / gr ]);
}
for(var j = 0; j < corners.length; j++) {
drawSquare(corners[j][0][0], corners[j][0][1], corners[j][1], corners[j][2]);

if(!hasCornerInExisting(corners[j][0][0] + corners[j][1], corners[j][0][1], corners[j][1] / gr, -corners[j][2] / gr)) {
newCorners.push([ [corners[j][0][0] + corners[j][1], corners[j][0][1]], corners[j][1] / gr, -corners[j][2] / gr])
}
if(!hasCornerInExisting(corners[j][0][0], corners[j][0][1] + corners[j][2], -corners[j][1] / gr, corners[j][2] / gr)) {
newCorners.push([ [corners[j][0][0], corners[j][0][1] + corners[j][2]], -corners[j][1] / gr, corners[j][2] / gr])
}
if(!hasCornerInExisting(corners[j][0][0] + corners[j][1], corners[j][0][1] + corners[j][2], corners[j][1] / gr, corners[j][2] / gr)) {
newCorners.push([ [corners[j][0][0] + corners[j][1], corners[j][0][1] + corners[j][2]], corners[j][1] / gr, corners[j][2] / gr])
}
}
corners = newCorners;
return i < depth - 1;
}

function drawSquare(x, y, w, h) {
var ah = Math.abs(h);
for(var i = 0; i < ah; i+=.15) {
turtle.jump(x, y + (h < 0? -i: i));
turtle.goto(x + w, y + (h < 0? -i: i));
}
addSquare(x, y, w, h);
}

function addSquare(x, y, w, h) {
var x1 = x;
var x2 = x+w;
var y1 = y;
var y2 = y+h;
squares.push([Math.min(x1, x2), Math.min(y1, y2), Math.max(x1, x2), Math.max(y1, y2)]);
}
function hasCornerInExisting(x, y, w, h) {
var points = [[x, y], [x+w, y], [x, y+h], [x+w, y+h]];
for(var i = 0; i < squares.length; i++) {
for(var j = 0; j < points.length; j++) {
if(squares[i][0] < points[j][0] && points[j][0] < squares[i][2] && squares[i][1] < points[j][1] && points[j][1] < squares[i][3]) {
return true;
}
}
}
return false;
}
```