xxxxxxxxxx
61
// Wave Function Collapse (tiled model)
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/challenges/171-wave-function-collapse
// https://youtu.be/0zac-cDzJwA
// Code from Challenge: https://editor.p5js.org/codingtrain/sketches/pLW3_PNDM
// Corrected and Expanded: https://github.com/CodingTrain/Wave-Function-Collapse
// Class for a cell
class Cell {
constructor(value, position) {
// Is it collapsed?
this.collapsed = false;
this.position = position;
this.i = position % DIM;
this.j = parseInt(position / DIM);
this.w = width / DIM;
this.h = height / DIM;
// Initial options via constructor
// if (value instanceof Array) {
// this.options = value;
// } else {
// // or all options to start
// this.options = [];
// for (let i = 0; i < value; i++) {
// this.options[i] = i;
// }
// }
}
draw() {
fill(0);
stroke(100);
// line(this.i * this.w, this.j * this.h, this.w * (this.i + 1), this.h * (this.j + 1))
// rect(this.i * this.w, this.j * this.h, this.w - 3, this.h - 3);
this.hexagon(this.i * this.w, this.j * this.h, this.w / 520);
this.hexagon(this.i * this.w, this.j * this.h + 75/4, this.w / 520);
this.hexagon(this.i * this.w + 75/4, this.j * this.h + 75 / 8, this.w / 520);
this.hexagon(this.i * this.w + 75/4, this.j * this.h + 75/4 + 75/8, this.w / 520);
}
hexagon(transX, transY, s) {
stroke(255);
strokeWeight(5);
fill("rgba(255, 255, 100, .25)");
push();
translate(transX, transY);
scale(s);
beginShape();
vertex(-75, -130);
vertex(75, -130);
vertex(150, 0);
vertex(75, 130);
vertex(-75, 130);
vertex(-150, 0);
endShape(CLOSE);
pop();
}
}