xxxxxxxxxx
148
let points = [];
let remaining = [];
let segments = [];
const s = 90;
let w, h;
let max = 15;
function setup() {
createCanvas(9000, 9000);
w = width/s;
h = height/s;
colorMode(HSB);
noFill();
stroke(0, 255, 255);
strokeWeight(20);
generate();
noLoop();
}
function draw() {
// background(0);
clear();
for(let i = 0; i < segments.length; i ++) {
segments[i].draw();
}
}
function mouseReleased() {
generate();
}
function keyReleased() {
save("squiggle.png");
}
function generate() {
generatePoints();
generateSegments();
}
function generateSegments() {
segments = [];
while(remaining.length > 0) {
let seg = new Segment();
let idx = randomPoint();
let n = [idx];
while(n.length > 0 && seg.points.length < max) {
idx = randomFromList(n);
let i = remaining.indexOf(idx);
remaining.splice(i, 1);
let p = points[idx]
seg.addPoint(p);
n = neighbours(idx);
}
segments.push(seg);
}
}
function neighbours(idx) {
let n = [];
let x = idx % w;
let y = floor(idx/w);
if(y > 0) n.push(x + (y - 1) * w);
if(y < h - 1) n.push(x + (y + 1) * w);
if(x > 0) n.push((x - 1) + y * w);
if(x < w - 1) n.push((x + 1) + y * w);
n = n.filter(i => remaining.indexOf(i) != -1);
return n;
}
function randomFromList(list) {
return list[floor(random(list.length))];
}
function randomPoint() {
let i = floor(random(remaining.length));
let idx = remaining[i];
return idx;
}
function generatePoints() {
points = [];
remaining = [];
for(let i = 0; i < w; i ++) {
for(let j = 0; j < h; j ++) {
let idx = i + j * w;
points.push(createVector(
(i * s) + random(s/2) + s/4,
(j * s) + random(s/2) + s/4
))
remaining.push(idx);
}
}
}
class Segment {
constructor() {
this.c = random(511)/2;
this.points = [];
}
addPoint(p) {
this.points.push(p);
}
draw() {
stroke(this.c, 255, 255);
if(this.points.length <= 1) {
return;
} else {
beginShape();
for(let i = 0; i < this.points.length; i++) {
let p = this.points[i];
if(!p) {
continue;
}
if(i == 0) {
vertex(p.x, p.y);
}
curveVertex(p.x, p.y);
if(i == this.points.length - 1) {
vertex(p.x, p.y);
}
}
endShape();
}
}
}