I am completely new to processing and need to solve a problem. I am trying to write a simple code to allow me to arrange imported shapes randomly scattered on the canvas. I have found the tutorial for circle packing (did not manage to run it successfully) and a separate one for importing shapes but I am struggling to combine them. Could anyone help me in trying to combine these two functions?
I would really appreciate it,
I have been using svg yes but would like to export it as vector or png after if possible?
This one imports the shape fine:
PShape s;
void setup() {
size(2000,1000);
// The file "bot.svg" must be in the data folder
// of the current sketch to load successfully
s = loadShape("plant.svg");
}
void draw() {
shape(s, width/2, height/2, 10, 10);
}
For the second part,
I am trying to understand this one now but it gives me a syntax error when I paste it in my sketchbook.
var circles;
var img;
function preload() {
img = loadImage("assets/kitten.jpg");
}
function setup() {
createCanvas(600, 600);
var density = displayDensity();
pixelDensity(1);
img.loadPixels();
circles = [];
console.log(img.width);
console.log(img.height);
console.log("pixels", img.pixels.length);
console.log(density)
}
function draw() {
background(0);
var total = 10;
var count = 0;
var attempts = 0;
while (count < total) {
var newC = newCircle();
if (newC !== null) {
circles.push(newC);
count++;
}
attempts++;
if (attempts > 1000) {
noLoop();
console.log("finished");
break;
}
}
for (var i = 0; i < circles.length; i++) {
var circle = circles[i];
if (circle.growing) {
if (circle.edges()) {
circle.growing = false;
} else {
for (var j = 0; j < circles.length; j++) {
var other = circles[j];
if (circle !== other) {
var d = dist(circle.x, circle.y, other.x, other.y);
var distance = circle.r + other.r;
if (d - 1 < distance) {
circle.growing = false;
break;
}
}
}
}
}
circle.show();
circle.grow();
}
}
function newCircle() {
var x = random(0, img.width);
var y = random(0, img.height);
var valid = true;
for (var i = 0; i < circles.length; i++) {
var circle = circles[i];
var d = dist(x, y, circle.x, circle.y);
if (d - 2 < circle.r) {
valid = false;
break;
}
}
if (valid) {
var index = (int(x) + int(y) * img.width) * 4;
var r = img.pixels[index];
var g = img.pixels[index+1];
var b = img.pixels[index+2];
var c = color(r,g,b);
return new Circle(x, y, color(c));
} else {
return null;
}
}
Thanks so much! This is working great!
Any chance you could show me how to substitute the circle for my file shape and choose the maximum number of shapes?
Thank you for your help