xxxxxxxxxx
let grid = {};
let debug = false;
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 1);
background(100, 80, 20);
}
function draw() {
let maxX = 40;
if (debug) {
maxX = 8;
}
for (let y = 0; y < maxX; y++) {
for (let x = 0; x < maxX; x++) {
drawTile(x * 20 + 10, y * 20 + 10, 10, x, y);
}
}
noLoop();
}
// x, y are the center point and size is the radius
function drawTile(x, y, radius, row, col) {
let possibleStartingPoints = [0, 1, 2, 3, 4, 5, 6, 7];
let allEndingPoints = {
0: [4],
1: [3, 5, 7],
2: [6],
3: [1, 5, 7],
4: [0],
5: [1, 3, 7],
6: [2],
7: [1, 3, 5]
};
let possibleEndingPoints = [];
// reduce possibilities according to tiles above and left of in grid to connect lines
let top = getTile(row, col - 1);
let left = getTile(row - 1, col);
let topLeft = getTile(row - 1, col - 1);
let topRight = getTile(row + 1, col - 1);
let topMiddle = top.flat().includes(5);
let leftMiddle = left.flat().includes(3);
let topBottomRight = top.flat().includes(4);
let topRightBottomLeft = topRight.flat().includes(6);
let topLeftCorner = topLeft.flat().includes(4) || top.flat().includes(6) || left.flat().includes(2);
let topRightCorner = top.flat().includes(4) || topRight.flat().includes(6);
if (debug) {
stroke(0, 100, 50);
square(x - 10, y - 10, radius * 2);
console.log(row, col);
console.log("is top middle", topMiddle);
console.log("is left middle", leftMiddle);
}
stroke(40, 80, 70);
let possibleLines = [];
if (topMiddle && !leftMiddle && !topLeftCorner) {
possibleLines = [
[
[1, 3],
[3, 5]
],
[
[3, 5],
[1, 5]
],
[
[1, 5]
],
[
[1, 8],
[6, 8]
],
[
[1, 3],
[3, 5]
],
[
[1, 8],
[8, 4]
],
[
[1, 8],
[3, 8]
]
];
if (debug) {
console.log("only top middle");
}
} else if (!topMiddle && leftMiddle && !topLeftCorner) {
possibleLines = [
[
[7, 3]
],
[
[7, 5]
],
[
[7, 8],
[8, 4]
]
];
if (debug) {
console.log("only left middle");
}
} else if (topMiddle && leftMiddle && !topLeftCorner) {
possibleLines = [
[
[1, 7]
],
[
[1, 7],
[3, 5]
],
[
[1, 5],
[3, 7]
]
];
if (topRightBottomLeft || topBottomRight) {
let twoSix = possibleLines.map(x => {r=[];x.forEach(e => r.push(e)); r.push([2,6]); return r;})
twoSix.forEach(e => possibleLines.push(e));
}
possibleLines.push([
[4,8],
[6,8],
[1,7]
]);
possibleLines.push([
[4,8],
[7,3],
[1,3]
]);
if (debug) {
console.log("top and left middle");
}
} else if (topMiddle && leftMiddle && topLeftCorner) {
possibleLines = [
[
[5, 7],
[1, 3],
[0, 4]
]
];
if (debug) {
console.log("left, topLeft, top");
}
} else if (!topMiddle && leftMiddle && topLeftCorner) {
possibleLines = [
[
[0, 4],
[7, 5]
]
];
if (debug) {
console.log("left, topLeft");
}
} else if (topMiddle && !leftMiddle && topLeftCorner) {
possibleLines = [
[
[0, 4],
[1, 3]
],
[
[1, 5],
[0, 8]
]
];
if (debug) {
console.log("top, topLeft");
}
} else if (!topMiddle && !leftMiddle && topLeftCorner) {
possibleLines = [
[
[0, 4]
]
];
if (debug) {
console.log("topLeft");
}
} else if (topLeftCorner && topRightCorner) {
possibleLines = [
[
[0, 8],
[8, 2]
],
[
[0, 4],
[8, 2]
]
];
if (debug) {
console.log("topLeft");
}
} else {
possibleLines = [
[
[3, 5]
]
];
if (debug) {
console.log("neither top or left middle");
}
}
let lines = random(possibleLines);
lines.forEach(line => {
let startingPointIndex = line[0];
let endingPointIndex = line[1]
if (debug) {
console.log("begin:" + startingPointIndex);
console.log("end:" + endingPointIndex);
console.log("\n");
}
drawLineByIndices(startingPointIndex, endingPointIndex, x, y, radius, row, col);
});
}
function getTile(row, col) {
let key = "row:" + row + ";col:" + col;
let tile = grid[key];
if (!tile) {
tile = [];
grid[key] = tile;
}
return tile;
}
function drawLineByIndices(startingPointIndex, endingPointIndex, x, y, radius, row, col) {
let allPointCoords = {
0: {
x: x - radius,
y: y - radius
},
1: {
x: x,
y: y - radius
},
2: {
x: x + radius,
y: y - radius
},
3: {
x: x + radius,
y: y
},
4: {
x: x + radius,
y: y + radius
},
5: {
x: x,
y: y + radius
},
6: {
x: x - radius,
y: y + radius
},
7: {
x: x - radius,
y: y
},
8: {
x: x,
y: y
}
};
if (debug) {
console.log("allPointCoords:"+JSON.stringify(allPointCoords));
console.log("startingPointIndex:"+startingPointIndex);
}
let startingPoint = allPointCoords[""+startingPointIndex];
let endingPoint = allPointCoords[""+endingPointIndex];
let tile = getTile(row, col);
tile.push([startingPointIndex, endingPointIndex]);
line(startingPoint.x, startingPoint.y, endingPoint.x, endingPoint.y);
}