5
$\begingroup$

With the following code:

findPoints = 
  Compile[{{n, _Integer}, {low, _Real}, {high, _Real}, {minD, _Real}},
    Block[{data = RandomReal[{low, high}, {1, 2}], k = 1, rv, temp}, 
    While[k < n, rv = RandomReal[{low, high}, 2];
     temp = Transpose[Transpose[data] - rv];
     If[Min[Sqrt[(#.#)] & /@ temp] > minD, data = Join[data, {rv}];
      k++;];];
    data]];

npts = 150;
r = 0.03;
minD = 2.2 r;
low = 0;
high = 1;

SeedRandom[159]
pts = findPoints[npts, low, high, minD];
g2d = Graphics[{FaceForm@Lighter[Blue, 0.4], 
    EdgeForm@Directive[Thickness[0.004], Black], Disk[#, r] & /@ pts},
    PlotRange -> All, Background -> Lighter@Blue];

d1 = Disk[{0.5, 0.5}, 0.5];
d2 = Disk[{0.5, 0.5}, 0.3];
annulus = RegionDifference[d1, d2];

mask2 = BoundaryDiscretizeRegion[#, {{-1, 1}, {-1, 1}}, 
     MaxCellMeasure -> {1 -> .02}] &@BoundaryDiscretizeRegion[annulus];
r2d2 = DiscretizeGraphics[g2d, MaxCellMeasure -> {1 -> .01}, 
   PlotRange -> All];
inside2 = RegionIntersection[r2d2, mask2]

I can produce (pseudo)randomly distributed circles inside an annulus. enter image description here

I have two questions. The first is a ridiculous one: How can we modify the color (e.g. Red) of the DiscretizeGraphics output.

The second one is not a tricky one. I want the circles to have random radius. Any ideas of how can I achieve that?

For References about above codes see the question:

find the maximum number of not intersecting circles inside an ellipse

and references therein.

$\endgroup$
5
$\begingroup$

Far from efficient, but we can adapt the Neat Example from the RegionDisjoint ref page. Note that a non-uniform distribution of radii would probably speed things up.

outerReg = Annulus[];

randomBall[dim_, reg_] := (
  While[
    !RegionWithin[reg, ball = Ball[RandomPoint[reg], RandomReal[{1/15, 1/6}]]],
    (* spin *)
  ];
  ball
)

appendDisjointBall[dim_][reg : Ball[pts_, rs_]] :=
  Block[{ball = randomBall[dim, outerReg]}, 
    While[! RegionDisjoint[ball, reg],
      ball = randomBall[dim, outerReg]
    ];
    Ball[Append[pts, #1], Append[rs, #2]] & @@ ball
  ]

disjointBalls[n_, dim_] := 
 Nest[appendDisjointBall[dim], List /@ randomBall[dim, outerReg], n - 1]

n = 40;
scene2D = disjointBalls[n, 2];

Graphics[{
  {EdgeForm[Black], GrayLevel[.9], Annulus[]}, 
  {EdgeForm[Black], Thread[{RandomColor[Hue[_], n], Thread[scene2D]}]}
}]

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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