xxxxxxxxxx
158
const cols = [
[169, 68, 56], // A
[210, 69, 69], // B
[228, 222, 190], // C
[230, 186, 163], // D
[210, 69, 69], // B
[220, 220, 220], // E
[230, 186, 163], // D
[210, 69, 69], // B
[228, 222, 190], // C
[230, 186, 163], // D
[0, 0, 0], // F
[228, 222, 190], // C
[230, 186, 163], // D
[210, 69, 69], // B
[228, 222, 190], // C
[169, 68, 56] // A
];
const thick = 18;
const thin = 12;
let salt;
function setup() {
createCanvas(800, 800);
salt = int(random(10000000));
}
function hash(a) {
// https://stackoverflow.com/questions/167735/fast-pseudo-random-number-generator-for-procedural-content
a = a ^ 61 ^ (a >> 16);
a = a + (a << 3);
a = a ^ (a >> 4);
a = a * 0x27d4eb2d;
a = a ^ (a >> 15);
return a;
}
function getInOut(x, y, sx, sy) {
const idx = (y - sx + 100) * 1000 + (x + sy) + salt;
let inout = hash(idx) % 2 === 0;
if (sx === -1) {
inout = !inout;
}
return inout;
}
function getCode(x, y) {
return (
8 * getInOut(x, y, 1, -1) +
4 * getInOut(x, y, 1, 1) +
2 * getInOut(x, y, -1, 1) +
getInOut(x, y, -1, -1)
);
}
function doVerts(cx, cy, x, y, sx, sy) {
const a = 50 / (1 + sqrt(2));
const b = a / sqrt(2);
const inout = getInOut(x, y, sx, sy);
vertex(cx + sx * b, cy + sy * b);
if (inout) {
vertex(cx + sx * b, cy + sy * (50 - b));
} else {
vertex(cx + sx * (50 - b), cy + sy * b);
}
vertex(cx + sx * (50 - b), cy + sy * (50 - b));
vertex(cx + sx * 50, cy + sy * 50);
}
function drawOne(x, y) {
const cx = 25 * x;
const cy = 25 * y;
beginShape(); /*
doVerts(cx - 50, cy, 1, -1, (y - 1 + 100) * 1000 + (x - 1) + salt);
doVerts(cx, cy - 50, 1, 1, (y - 1 + 100) * 1000 + (x + 1) + salt);
doVerts(cx + 50, cy, -1, 1, (y + 1 + 100) * 1000 + (x + 1) + salt);
doVerts(cx, cy + 50, -1, -1, (y + 1 + 100) * 1000 + (x - 1) + salt);
*/
doVerts(cx - 50, cy, x, y, 1, -1);
doVerts(cx, cy - 50, x, y, 1, 1);
doVerts(cx + 50, cy, x, y, -1, 1);
doVerts(cx, cy + 50, x, y, -1, -1);
endShape(CLOSE);
}
function draw() {
background(220);
push();
translate(width / 2, height / 2);
scale(1.25);
translate(-width / 2, -height / 2);
const r = 100 / (2 + sqrt(2));
fill(255, 0, 0, 100);
stroke(0);
strokeWeight(thick);
for (let y = 1; y < 32; y += 4) {
for (let x = 1; x < 32; x += 4) {
let code = getCode(x, y);
fill(cols[code]);
drawOne(x, y);
code = getCode(x+2, y+2);
fill(cols[code]);
drawOne(x + 2, y + 2);
}
}
noFill();
stroke(255);
strokeWeight(thin);
for (let y = 1; y < 32; y += 4) {
for (let x = 1; x < 32; x += 4) {
drawOne(x, y);
drawOne(x + 2, y + 2);
}
}
const d = ((thin + thick) / 4) * sqrt(2);
stroke(0);
strokeWeight((thick - thin) / 2);
for (let y = 1; y < 36; y += 4) {
for (let x = 1; x < 36; x += 4) {
line(x * 25 - 50 - d, y * 25, x * 25 - 50, y * 25 - d);
line(x * 25 - 50, y * 25 + d, x * 25 - 50 + d, y * 25);
line(x * 25 - d, y * 25 - 50, x * 25, y * 25 - 50 + d);
line(x * 25, y * 25 - 50 - d, x * 25 + d, y * 25 - 50);
}
}
pop();
noLoop();
}
function keyPressed()
{
if( key === ' ' ) {
++salt;
loop();
} else if( key === 's' ) {
save( 'out.png' );
}
}