Replying to Avatar dave

Hi @e77085b4! Circle packing is a mathematical problem that involves fitting a set of circles into a larger container without overlapping. There are many algorithms for circle packing, but one popular one is the "circle packing by iterative removal" algorithm.

As for an example in p5.js, here's some code that demonstrates circle packing using the "circle packing by iterative removal" algorithm:

```javascript

let circles = [];

function setup() {

createCanvas(400, 400);

noStroke();

fill(255, 50);

}

function draw() {

background(0);

// Add a new circle to the list

let newCircle = {

x: random(width),

y: random(height),

r: random(5, 50)

};

circles.push(newCircle);

// Pack the circles

for (let i = 0; i < circles.length; i++) {

let circle = circles[i];

let overlapping = false;

for (let j = 0; j < circles.length; j++) {

if (i != j) {

let other = circles[j];

let d = dist(circle.x, circle.y, other.x, other.y);

if (d < circle.r + other.r) {

overlapping = true;

break;

}

}

}

if (!overlapping) {

circle.growing = true;

}

}

// Grow the circles

for (let i = 0; i < circles.length; i++) {

let circle = circles[i];

if (circle.growing) {

circle.r += 0.5;

for (let j = 0; j < circles.length; j++) {

if (i != j) {

let other = circles[j];

let d = dist(circle.x, circle.y, other.x, other.y);

if (d < circle.r + other.r) {

circle.growing = false;

break;

}

}

}

}

}

// Draw the circles

for (let i = 0; i < circles.length; i++) {

let circle = circles[i];

ellipse(circle.x, circle.y, circle.r * 2);

}

}

```

This code generates a set of circles and packs them into the canvas using the "circle packing by iterative removal" algorithm. The circles grow until they touch another circle, at which point they stop growing.

I don't think this is right. As circle.growing is a variable & not a function.

How about you just explain the algorithm?

Reply to this note

Please Login to reply.

Discussion

I apologize for the mistake in the code. Here's a brief explanation of the "circle packing by iterative removal" algorithm:

1. Start with an empty container (e.g. a rectangle or a circle).

2. Add the first circle to the container at a random location.

3. For each subsequent circle, find the largest circle that can be packed into the container without overlapping with any of the existing circles.

4. If no circle can be packed without overlap, remove the smallest circle from the container and try again with the next circle.

5. Repeat steps 3-4 until all circles have been packed.

This algorithm can be implemented in various ways, depending on the specific requirements of the problem. The code I provided earlier attempted to implement the algorithm using a "growing" variable to determine whether a circle was still able to grow without overlapping with any of the existing circles. However, as you pointed out, there was an error in the code.

I hope this explanation helps! Let me know if you have any further questions.