The function pack is designed to iteratively pack a specified number of non-overlapping disks within a given region while maximizing the radius of each disk. Here’s a detailed breakdown of its components and behavior:
1. Function Definition and Parameters:​​
•
pack[reg_, n_]
: The function takes two parameters: reg, which represents the region within which the disks are to be packed, and n, which is the number of disks to be packed.
2. Local Variables:​​
• disk: Represents a disk with center
{x, y}
and radius r.
​​
•
diski[i_]
: Represents the i-th disk with center
{x[i], y[i]}
and radius
r[i]
.
​​
•
circi[i_]
: Represents the boundary of the i-th disk.
​​
• cons: A list of constraints ensuring that a disk is within the specified region.
​​
•
consDJ[i_]
: A function that returns a constraint ensuring that the i-th disk is disjoint from the main disk.
3. Optimization Loop:​
​
• The function uses a loop (
Do
loop) to place each disk iteratively.
​
​
• For each disk i, it maximizes the radius r subject to the constraints that the disk must be within the region and disjoint from all previously placed disks.
​
​
•
NArgMax
is used to find the maximum radius and corresponding center coordinates for each disk.
​
​
• After finding the optimal disk parameters, cons is updated to include a new constraint ensuring that the newly placed disk is disjoint from the main disk.
4. Performance Output:​
​
• The function prints the total optimization time taken to place all disks.
5. Packing Fraction:​
​
• The packing fraction is calculated as the sum of the areas of all packed disks divided by the area of the region.
​
​
• This value is printed to indicate how efficiently the disks have been packed within the region.
6. Visualization:​
​
• The function uses
Show
to create a visualization of the region and the packed disks.
​
​
• The region is displayed with a light brown color and thick black edges.
​
​
• Each disk is displayed with a distinct color, and their boundaries are outlined in black.
​
Overall, the function aims to demonstrate an optimization process for packing disks within a region, calculating the efficiency of the packing, and providing a visual representation of the result.

Packing Code

pack[reg_,n_]:=​​Block[{disk,diski,circi,cons,consDJ,x,y,r,time,ds,cs,pf},​​​​disk=Disk[{x,y},r];(*Definetheinitialdisk*)​​diski[i_]=Disk[{x[i],y[i]},r[i]];(*Definethediskifunctiontocreatediskswithvaryingradii*)​​circi[i_]=RegionBoundary[diski[i]];(*Definethecircifunctiontogettheboundaryofthedisks*)​​cons=List@@(RegionWithin[reg,disk]);​​consDJ[i_]:=RegionDisjoint[diski[i],disk];​​​​(*Performtheoptimizationandmeasurethetimetaken*)​​time=AbsoluteTiming@Do[​​{Thread[{x[i],y[i],r[i]}=NArgMax[{r,cons},{x,y,r}]],​​cons=Join[cons,{consDJ[i]}]},​​{i,n}];​​​​(*Calculateandprintthepackingfraction*)​​Print["optimization time = ",time[[1]]];​​pf=Sum[Area@diski[i],{i,n}]/Area[reg];​​Print["packing fraction =",pf];​​​​(*Displaythepackeddiskswithintheregion*)​​Show[​​Region[Style[reg,LightBrown,EdgeForm[{Thick,Black}]]],​​Graphics@Table[{Hue[0.8*(i-1)/n],diski[i]},{i,n}],​​Graphics@{Thick,Black,Table[circi[i],{i,n}]},​​Frame->True​​]​​]

Test

In[]:=
pack[Polygon[{{0,0},{4,0},{0,3}}],25]
optimization time = 27.0232
packing fraction =0.914357
Out[]=
In[]:=
pack[Polygon[{{0,0},{1,0},{2,1},{1,1}}],25]
optimization time = 24.6102
packing fraction =0.911455
Out[]=
In[]:=
pack[Polygon[CirclePoints[6]/.{x_,y_}->{3x,y}],25]
optimization time = 31.9959
packing fraction =0.904205
Out[]=
In[]:=
packPolygon{0,0},{1,0},
1
3
,
2
3
,25
optimization time = 26.7004
packing fraction =0.921131
Out[]=
In[]:=
packPolygon{0,0},{1,0},
2
3
,
1
3
,
1
3
,
1
3
,25
optimization time = 27.1018
packing fraction =0.911455
Out[]=
In[]:=
packPolygon{0,0},{1,0},
2
3
,
4
9
,
1
3
,
1
3
,25
optimization time = 35.5467
packing fraction =0.916988
Out[]=
In[]:=
p10d=Polygon[CirclePoints[10]/.{x_,y_}->{x,y/2}];
In[]:=
pack[p10d,25]
optimization time = 48.787
packing fraction =0.91819
Out[]=

CITE THIS NOTEBOOK

Packing maximized disks in polygons​
by Frank Kampas​
Wolfram Community, STAFF PICKS, January 17, 2025
​https://community.wolfram.com/groups/-/m/t/3358742