Skip to content

Instantly share code, notes, and snippets.

@karlosgliberal
Created November 13, 2016 18:10
Show Gist options
  • Save karlosgliberal/0e9e61d9f427c2c9c6c2cb06ded3052a to your computer and use it in GitHub Desktop.
Save karlosgliberal/0e9e61d9f427c2c9c6c2cb06ded3052a to your computer and use it in GitHub Desktop.
Generado de planos Voronoi
var poligonos_obj;
var json
var myImg;
var pixelColors = [];
var scanLine = 0;
var plano =[];
var plano_objeto = {};
var colores = [];
var listado;
var count = 840;
var c, width, height, polydraw, colorArr, vertices, voronoi, polygons, colorDominat, paleta;
function preload() {
myImg = loadImage("yourimage.png");
}
function setup() {
console.log(listado);
c = createCanvas(myImg.width, myImg.height, SVG);
scanLine = 0;
smooth(2);
width = myImg.width;
height = myImg.height;
vertices = d3.range(4000).map(function(d) {
return [Math.random() * width, Math.random() * height];
});
voronoi = d3.geom.voronoi().clipExtent([[0, 0], [width, height]]);
polygons = voronoi(vertices);
polydraw();
}
function polydraw() {
strokeWeight(.3);
stroke(255);
poligonos_obj = polygons.reduce(function(result, item, index) {
result[index] = item; //a, b, c
return result;
}, {})
for (var j = 0, jL = polygons.length; j < jL; j++) {
var singlegon = polygons[j];
stroke(255);
beginShape();
var colorCordenda = myImg.get(polygons[j]['point'][0], polygons[j]['point'][1])
fill(colorCordenda);
colores.push(colorCordenda);
for (var k = 0, kL = singlegon.length; k < kL; k++){
vertex(singlegon[k][0], singlegon[k][1]);
}
endShape(CLOSE);
}
for (var i = 0; i < polygons.length; i++) {
poligonos_obj[i].unshift(colores[i]);
}
save();
//fin();
}
function fin(){
count = count + 1;
saveJSON(poligonos_obj, 'plano'+count+'.json', true);
plano_objeto = [];
colores = [];
console.log(count);
//clear();
vertices = d3.range(4000).map(function(d) {
return [Math.random() * width, Math.random() * height];
});
voronoi = d3.geom.voronoi().clipExtent([[0, 0], [width, height]]);
polygons = voronoi(vertices);
console.log(listado[count]);
myImg = loadImage("images/"+listado[count], function(){
polydraw();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment