Skip to main content

graphics - find the maximum number of not intersecting circles inside an ellipse


$Version


"10.3.0 for Linux x86 (64-bit) (October 9, 2015)"



(Sequel to 98724 and 99345)


(I use codes originally created by Andy Ross, ybeltukov and J.M.).



I am wondering if one can find the maximum number of randomly generated circles inside a given ellipse.


Using the function findPoints defined below


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]];


and taking


npts = 150;(*number of points*)
r = 0.03;(*radius of the circles*)
minD = 2.2 r;(*minimum distance in terms of the radius*)
low = 0; (*unit square*)
high = 1;(*unit square*)

ep = With[{a = 2/5, b = 1/2},
BoundaryDiscretizeRegion@

ParametricRegion[(low + high) {1, 1}/2 +
c ({a Cos[t], b Sin[t]} +
r Normalize[Cross[D[{a Cos[t], b Sin[t]}, t]]]), {{c, 0,
1}, {t, 0, 2 \[Pi]}}]];

SeedRandom[159];
pts = Select[findPoints[npts, low, high, minD], RegionMember[ep, #] &];
g2d = Graphics[{Disk[#, r] & /@ pts, Circle[{1/2, 1/2}, {2/5, 1/2}]},
PlotRange -> All, Frame -> True]


we get


enter image description here


and 72 circles (disks) are lying within the ellipse.


pts // Length
(*72*)

Now I increase progressively the number npts.


npts = 160;

SeedRandom[159];

pts = Select[findPoints[npts, low, high, minD],
RegionMember[ep, #] &] // Length
(*80*)

npts = 170;

SeedRandom[159];
Timing[(pts = Select[findPoints[npts, low, high, minD],
RegionMember[ep, #] &] )// Length]
(*{8.23561, 87}*)


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

SeedRandom[159];
Timing[(pts =
Select[findPoints[npts, low, high, minD],

RegionMember[ep, #] &]) // Length]

(*{12.7237, 87}*)

I guess that we have reach a plateau as it is clear below.


    g2d = Graphics[{Disk[#, r] & /@ pts, Circle[{1/2, 1/2}, {2/5, 1/2}]}, 
PlotRange -> All, Frame -> True]

enter image description here


My question now is how we can use Mathematica in order to extract the maximum number of not intersecting circles withing an ellipse?



Thank you very much.



Answer



The problem consists of two questions: how to determine if circle is inside the ellipse and how to maximize the number of circles?


1. Circle is inside the ellipse?


Let us show that the region of possible circle centers are bounded by a parallel curve of degree 8.


ClearAll[x, y, a, b, r];
eq1 = Simplify[RegionDistance[Disk[{0, 0}, {a, b}], {x, y}]^2 == r^2,
x^2/a^2 + y^2/b^2 > 1]

enter image description here



RegionDistance gives distance outside the ellipse, but it doesn't matter now. Now we can eliminate Root:


pointInEllipse[x_, y_, a_, b_] = 1 - x^2/a^2 - y^2/b^2;
circleNotIntersectEllipse[x_, y_, a_, b_, r_] =
FullSimplify@*Subtract @@
Eliminate[{eq1 /. _Root -> q,
FirstCase[eq1, Root[eq_, _] :> eq@q == 0, , ∞]}, q]

enter image description here


The last formula is positive when the circle doesn't intersect the ellipse. Now we can visualize the obtained region for different values of the circle radius


a = 2;

b = 1;
RegionPlot[Evaluate@
Table[circleNotIntersectEllipse[x, y, a, b, r] > 0 &&
pointInEllipse[x, y, a, b] > 0, {r, 0.2, 1, 0.2}], {x, -1.1 a,
1.1 a}, {y, -1.1 b, 1.1 b}, PlotPoints -> 60, Epilog -> Circle[{0, 0}, {a, b}],
AspectRatio -> Automatic]

enter image description here


There are some glitches, but they appear only for unreasonable big values of the radius.




I think that the circle packing is the good starting point


r = 0.1;
{x, y} = Transpose@Join[Tuples@{##}, Tuples@{# + r, #2 + Sqrt[3] r}] &[
Range[-#, #, 2 r], Range[-#, #, Sqrt[3] 2 r]] &@Max[a, b];
RegionPlot[circleNotIntersectEllipse[x, y, a, b, r] > 0 &&
pointInEllipse[x, y, a, b] > 0, {x, -1.1 a, 1.1 a}, {y, -1.1 b, 1.1 b},
PlotPoints -> 60, Epilog -> {Circle[{0, 0}, {a, b}], Point@Transpose@{x, y}},
AspectRatio -> Automatic]

enter image description here



Points inside the blue region show possible circle centers. Now we have to find the best translation and orientation of the circle packing


transform[x0_, 
y0_, φ_] := {{Cos[φ], Sin[φ]}, {-Sin[φ],
Cos[φ]}}.{x, y} + {x0, y0};
inside[x0_, y0_, φ_] :=
UnitStep[circleNotIntersectEllipse[##, a, b, r], pointInEllipse[##, a, b]] & @@
transform[x0, y0, φ]
func[x0_?NumericQ, y0_?NumericQ, φ_?NumericQ] :=
Total@inside[x0, y0, φ];


{x1, y1, φ1} =
NArgMax[func[x0, y0, φ0], {x0, y0, φ0}]
(* {-0.327895, 0.286679, -0.290644} *)

circles =
Pick[Transpose@transform[x1, y1, φ1], inside[x1, y1, φ1], 1];
Length@circles
(* 160 *)

The number of circles is close to the theoretical prediction from Thies Heidecke's answer



Ï€/2/Sqrt[3] a b/r^2
(* 181.38 *)

Finally, we can plot the result packing


Graphics[{Circle[{0, 0}, {a, b}], Lighter@Blue, Disk[#, r] & /@ circles}]

enter image description here


May be there is possibility to insert one or two circles more, but it requires much more advanced technique.


P.S. The order of evaluation matters. For simplicity I omit some scoping constrictions.


Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...