How to randomly generate non-intersecting cylinders within a circle?

I posted this question a while ago but unfortunately I’ve been unable to solve it with the answers provided. Also, I’ve had a look on Stack Overflow for similar questions but they’re way over my head. So apologies for creating a new thread, but I’m hoping for some more suggestions.

Basically, I want to randomly generate a bunch of non-intersecting cylinders of varying size within a circle, much like an Ishihara colour test:

alt text

Colour is unimportant, I just need to be able to define the minimum distance between each circle. Any help is much appreciated.

Here’s my code so far:

public Transform circle;
public int numberOfCircles;
public int circleSize;

void Start()
{
	for (int i = 0; i < numberOfCircles; i++)
	{	
		circleSize = Random.Range(1, 4);
			
		circle.localScale = new Vector3(1 * circleSize, 1, 1 * circleSize);

		Vector2 pos = Random.insideUnitCircle * 10;
			
		Vector3 newPos = new Vector3(pos.x, 0, pos.y);
			
		Instantiate(circle, newPos, Quaternion.identity);

	}
}

And here’s an example result, with 25 circles.:

[21534-screen+shot+2014-02-02+at+5.20.13+pm.png|21534]

As you can see, there’s a lot of intersecting, and the circles don’t really form the shape of a larger one.

I suppose I could just instantiate a hundred or so circles then destroy them until there is none intersecting, but I’m sure there is a more elegant solution.

This a standard algorithm/math question. StackOverflow is probably the best place for it.

It’s a difficult problem. In your image, note how the largest size is scattered evenly. Do you need that? And the next smaller size is 1/2 of the largest. Do you want that, or prefer random sizes? Do you need the tiny dots? What is the least amount of space where you need to add a tiny dot? In general, what ratio/distribution of large to small dots do you want?

I think the answers are confusing because the problem is hard and has lots of ways to do it.

Once you have a list of Vector3’s and sizes, the Unity part to spawn them is easy.

For fun, a “Unity” way might be to make a half-circle collider (mesh, or build from many boxes.) Then spawn a pile of random cylinders and let them fall in Once they settle down, record positions.

Just instantiate them all randomly, but then for each circle, get a list of transforms of the other circles that are within a certain distance. Then loop through each and apply a repelling force away from the circle inversely proportional to the distance if the distance is too close, and an attracting force toward it directionally proportional to the distance if the distance is too far. Keep doing it every frame and they will settle into a stable pattern within a few seconds.