xxxxxxxxxx
59
class Point {
constructor(x, y) {
this.pos = createVector(random(width), random(height));
this.color = color(random(255), random(255), random(255));
}
show() {
noStroke()
fill(255);
circle(this.pos.x, this.pos.y, 2)
}
}
let points = [];
function setup() {
createCanvas(200, 200);
pixelDensity(1);
for (let i = 0; i < 10; i++) {
points.push(new Point());
}
}
function draw() {
background(51);
loadPixels();
let d = 1;
let halfImage = (width) * (height) * 4;
for (let i = 0; i < halfImage; i += 4) {
let record = Infinity;
let recordP =null;
for (let p of points) {
let d = dist(p.pos.x, p.pos.y, i % width, floor(i / width));
if (d < record) {
record = d;
recordP = p;
}
}
pixels[i] = red(recordP.color);
pixels[i+1] = green(recordP.color);
pixels[i+2] = blue(recordP.color);
pixels[i+3] = alpha(recordP.color);
}
updatePixels();
let recordDist = Infinity;
for (let p of points) {
p.show();
}
}