# Apollonian Packing of Circles within Ellipses
## 2 Materials and Methods
### Covering the Ellipse
#### 2.4.2 Implementation of the Packing Algorithm  
Iterative Version of the Algorithm  
The packing algorithm was implemented in Matlab(r). The core element of the implementation is a subfunction, EllipseSprout(n1,n2,n3,CA), which obtains, as input arguments, three values of row index \(n\) of the CA and produces the effect of adding a new row, \(n_{s}\), to the CA containing information about the new sprout circle generated by stem circles stored in rows \(n_{1}\), \(n_{2}\) and \(n_{3}\). The subfunction performs an initial test by controlling the presence of the conventional index value (i.e., value \(-1\)) in any of its first three parameters, \(n_{1}\), \(n_{2}\) and \(n_{3}\), indicating that one of the stems of the circle under analysis is actually boundary ellipse \(\mathcal{E}\), thus determining whether to apply the Descartes complex formula given in Equation (4) or the ellipse tangency algorithm described in (Section 2.3) to compute the center and radius of the new sprout circle to be stored in a new row of the CA with index \(n_{s}\). If radius \(r_{s}\) of the new sprout circle is smaller than specified amount \(r_{min}\), then flag \(f_{n_{s}}\) of row \(n_{s}\) is set to \(0\). At the beginning of the procedure, \(N-1\) instances of the EllipseSprout function are launched, i.e., EllipseSprout(-1,n,n+1) for \(n=0,\ldots,N-2\), resulting in the determination of new circles internally tangent to the elliptic boundary (corresponding to value \(-1\) as the first input parameter) and externally tangent to two consecutive circles of the scaffolding (corresponding to the two consecutive row indexes as the second and third input parameters). Information about these new \"first generation\" sprout circles is stored in \(N-1\) additional rows of the CA having index \(n=N,\ldots,2N-1\). The EllipseSprout function is subsequently launched to compute all \"second generation\" sprout circles generated by the \(N-1\) first-generation circles, starting from the array row of index \(2N\) and proceeding to compute the sprout circles defined by the n-th circle by  
\begin{table}
\begin{tabular}{c c c c c c c c} \hline \hline
**Row Index** & **Center** & **Center** & **Radius** & **1 st Stem** & **2 nd Stem** & **3 rd Stem** & **Flag** & **Tangency** \\ \(n\) & \(x_{n}\) & \(y_{n}\) & \(r_{n}\) & \(p^{\prime}_{n}\) & \(p^{\prime\prime}_{n}\) & \(p^{\prime\prime\prime}_{n}\) & \(f_{n}\) & \(x_{Tn}\) \\ \hline
0 & 0 & 0 & \(r_{0}\) & \(-1\) & 0 & 0 & 1 & 0 \\
1 & \(x_{1}\) & \(y_{1}\) & \(r_{means of its stem circle pointers, provided that its flag \(f_{n}\) has a non-zero value. Iteratively, the EllipseSprout function is subsequently launched to compute all \"next-generation\" sprout circles generated by \"previous-generation\" circles having non-zero \(f_{n}\) flag values, thus accomplishing complete Apollonian packing within the elliptic domain to desired accuracy \(r_{min}\). Recursive Version of the Algorithm  
Alternatively, the algorithm may be expressed in a recursive form. In this case, the core element of the implementation is a recursive subfunction, EllipseSproutRec(n1,n2,n3,CA), which obtains, as input arguments, three values of row index \(n\) of the CA and produces the effect of adding a new row, \(n_{s}\), to the CA containing information about the new sprout circles. The basic step of the EllipseSproutRec function is the determination of a new sprout circle generated by stems with indexes n1, n1 and n3 and the storage of its relevant information in the new row of the CA with index n4. Subsequently, the EllipseSproutRec function tests the flag \(f_{4}\) value, determining whether termination or recursion should occur:  
* If flag \(f_{4}\) has a zero value, execution of the function is terminated. * If flag \(f_{4}\) has a non-zero value, the function performs three recursive calls to generate the \"further-generation\" circles generated by circle \(n_{4}\), together with any two of its stems:  
- EllipseSproutRec(n4,n1,n2,CA). - EllipseSproutRec(n4,n1,n3,CA). - EllipseSproutRec(n4,n2,n3,CA).