I need to generate an image of $n$ randomly sized but non-overlapping blobs in a fixed rectangular region; for example, a square of 300 pixels.
The blobs could be disks to keep things simple. The non-overlapping part is tricky; this is what I have so far:
Clear @ pair;
pair[n_] := Module[{pts=RandomReal[1,{n,2}]},
Image @ Rasterize[Graphics[{{PointSize@RandomReal[{0,.5}],Point[#]}&/@pts},
PlotRange->{{0,1},{0,1}},PlotRangePadding->Scaled[.1]],
ImageSize->300]->n
]
As you can see the ten disks are overlapping:
Answer
Just a quick modification of the code here,
distinctDisks[n_, range_:{0, 1}, radiusRange_:{0.03, 0.15}] := Module[
{d, f, p, r},
d = {Disk[RandomReal[range, 2], RandomReal[radiusRange]]};
Do[f = RegionDistance[RegionUnion @@ d];
While[
r = RandomReal[radiusRange];
p = RandomReal[range, 2];
f[p] < r];
d = Append[d, Disk[p, r]], {n - 1}];
d]
distinctDisks[25, {0, 5}, {0, 2}] // Graphics
Comments
Post a Comment