Starting from a set of points, I want to fill an area using disks. Each disk's center should be one of the points and the disks should not overlap. I've managed to write a function that, given a list of points, finds the respective radii of the disks:

findRadii[pts_] := Module[{
   vars = Unique /@ (("x" <> ToString@#) & /@ Range@Length@pts),
   norms = Norm[Subtract[##]] & @@@ Subsets[pts, {2}],
   dists, constraints},
  dists = Plus @@@ Subsets[vars, {2}];
  constraints = Thread[dists <= norms]~Join~Thread[vars > 0];
  NArgMax[{Total@vars^2, constraints}, vars]

The function just maximises the square of the sum of radii with the constraint that each radius should be positive and the sum of two radii should be smaller than the dsitance between the respective points (I know that this in fact does not maximize the filled area, which maximizing Total[vars^2] would, but I've found the result to look nicer).

Testing the function (and timing it) yields the following:

SeedRandom@1; foo = RandomReal[{0, 10}, {20, 2}];  

Timing[radii = findRadii[foo];]
(* {13.931, Null} *)

Graphics[{MapThread[Circle[#1, #2] &, {foo, radii}], Red, Point[foo]}]

enter image description here

Am I missing a simpler way to calculate the distances? How can the function's performance be increased? Ultimately, I would like to use it with >100 points in reasonable time. Note that I don't need a strict maximum of the covered area, but rather a visually appealing result.

  • 2
    $\begingroup$ sweetpepperspray.blogspot.com.ar/2010/06/osmos.html $\endgroup$ Apr 7, 2013 at 22:54
  • 1
    $\begingroup$ You can use Daniel's answer here and simply replace the overlap function he uses with an appropriate one for circles (e.g. r1 + r2 > EuclideanDistance[c1, c2]) $\endgroup$
    – rm -rf
    Apr 7, 2013 at 23:26
  • 1
    $\begingroup$ Another cool related question is How can I pack circles of different sizes into a spiral? $\endgroup$
    – Jens
    Apr 7, 2013 at 23:43
  • $\begingroup$ @rm -rf wouldn't that relocate the disks to form a more tightly packed area? I'd like the disks' positions to be fixed at the starting points, if possible. $\endgroup$
    – einbandi
    Apr 8, 2013 at 0:02
  • $\begingroup$ @einbandi It's been a while since I played with Daniel's code there, but I believe you can just set the penalty for moving the center to infinity $\endgroup$
    – rm -rf
    Apr 8, 2013 at 0:22

1 Answer 1


See if the following will do what you desire. It first estimates the distance as half the distance to the nearest neighbor, and then splits the differences with the closest circles.

pts = RandomReal[{0, 10}, {100, 2}];
nf = Nearest[pts -> Automatic];

dist = EuclideanDistance[#, pts[[Last@nf[#, 2]]]]/2 & /@ pts;
dist = FixedPoint[Function[{dist0}, 
    1/2 (dist0 + Table[Min[EuclideanDistance[pts[[i]], pts[[#]]] - dist0[[#]] & /@ 
                   Rest@nf[pts[[i]], Length[pts]]], {i, Length[pts]}])], 
    dist]; // Timing
(* {2.019525, Null} *)

Graphics[{MapThread[Circle[#1, #2] &, {pts, dist}], Red, Point[pts]}]


[Note: What might look like an isolated point on the left is actually two points close together.]

Quite a bit faster, but not very fast. Practically, though it is highly unlikely you have to test all points, only some of the nearest neighbors (here 9):

data = EuclideanDistance[#, pts[[Last@nf[#, 2]]]]/2 & /@ pts;
dist = FixedPoint[Function[{dist0}, 
    1/2 (dist0 + Table[Min[EuclideanDistance[pts[[i]], pts[[#]]] - dist0[[#]] & /@ 
                   Rest@nf[pts[[i]], 10]], {i, Length[pts]}])], 
    dist]; // Timing
(* {0.254985, Null} *)

The output is the same in this case. Of course there's no guarantee that the nine nearest neighbors will prevent overlap.

If you want the isolated pairs not to have the same radii, then you can start with

dist = RandomReal[{0.35, 0.7}] EuclideanDistance[#, pts[[Last@nf[#, 2]]]] & /@ pts;

And there will be a chance the radii will be significantly different. The lack of symmetry might be more visually appealing, depending on your intended purpose.

  • $\begingroup$ The answer from @Michael E2 based on Nearest is far better than mine. I found that with only 3 nearest neighbours the solutions are similar to the OP's. However, the number of constraints using Nearest is linear in the number of points rather than quadratic. So the corresponding timings can still be long, but much much faster than the original findRadii. $\endgroup$ Apr 8, 2013 at 3:24
  • 1
    $\begingroup$ Ok,it's answer for hard to supass.But I think the DelaunayMesh and NearestNeighborGraph maybe can help to speed up still. $\endgroup$
    – yode
    Feb 26, 2017 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.