Public
Edited
Mar 7, 2024
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
weightedVoronoiRelaxation = function(imagedata, voronoi, precentage = 100) {
let see = voronoi.xmax - voronoi.xmin;
let seed = voronoi.delaunay.points;
let weights = new Array(seed.length/2).fill(0), centroids=new Array(seed.length).fill(0), counts = new Array(seed.length/2).fill(0);
let seedIndex = 0;
for(let x = voronoi.xmin; x < voronoi.xmax; x ++){
for(let y = voronoi.ymin; y < voronoi.ymax; y ++){
let ind = (x + y * width) * 4;
let r = imagedata[ind + 0];
let g = imagedata[ind + 1];
let b = imagedata[ind + 2];
let bright = (r + g + b) / 3;
let weight = 1 - bright / 255;
//With d3.Delaunay, we can use voronoi.delaunay.find() to find the closest Voronoi cell seed to an location
seedIndex = voronoi.delaunay.find(x, y, seedIndex);
centroids[seedIndex*2] += x * weight;
centroids[seedIndex*2 + 1] += y * weight;
weights[seedIndex] += weight;
counts[seedIndex] ++;
}
}
for(let i = 0; i < centroids.length; i += 2) {
if (weights[i/2] > 0) {
centroids[i] /= weights[i/2];
centroids[i+1] /= weights[i/2];
} else {
centroids[i] = seed[i];
centroids[i+1] = seed[i+1];
}
}
for (let i = 0; i < seed.length; i ++) {
seed[i] += (centroids[i] - seed[i]) * (precentage/100);
}
return new d3.Delaunay(seed).voronoi([voronoi.xmin, voronoi.ymin, voronoi.xmax, voronoi.ymax]);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more