In the picture, imagine this grid of circles extends infinitely. I would like to calculate the average distance of any point outside a circle to the nearest circle. I simplified this the average distance of any point in the red plane to the blue circle. My attempt goes like this:
distance to circle $= r-R$, then
$$ \text{avg} = \frac{\iint(r*(r-R))dr \, d\phi}{\iint(r)d\phi \, dr} \quad \text{with} \quad 0<\phi < \frac{\pi}4 \quad \text{and} \quad R<r<\frac{X}{\cos(\phi)}$$
with $R$ the radius of the blue circle, and $X$ the right vertical boundary of the red plane. As $x = r\cos(\phi)$, hence $r=\frac{x}{\cos(\phi)}$. Integrating to $r$ this gives:
$$ \text{avg} = \frac{\int(\frac13 r^3 - R\frac12 r^2) d\phi}{\int\frac12 r^2d\phi} \quad \text{with} \quad R<r<\frac{X}{\cos(\phi)}$$
which becomes
$$ \text{avg} = \frac{\int \left[ \frac13 \left( \frac{X}{\cos(\phi)} \right)^3 - R \frac12 \left( \frac{X}{\cos(\phi)} \right)^2 - \left( \frac13 R^3 - \frac12R^3\right ) \right]\, d\phi}{\int(\frac12\left[ \frac{X}{\cos(\phi)})^2-\frac{1}{2}R^2 \right] \, d\phi} \quad \text{with} \quad 0<\phi<\frac{\pi}{4}$$
with a little help from wolfram alpha I get:
$$\int_0^{\pi/4} \left( \frac{X}{\cos(\phi)} \right)^3 \, d\phi = \frac{1}{2}X^3 \left( \sqrt2+2~\tanh^{-1}( \tan(\frac{\pi}{8}) \right) ~,$$
and
$$\int_{0}^{\pi/4} \left( \frac{X}{\cos(\phi)} \right)^2 \,d\phi = X^2$$
hence
$$ \text{avg} = \frac{\frac13 \frac12 (X^3 (\sqrt 2 + 2 \tanh^{-1}( \tan(\frac{\pi}{8})) -\frac12 R X^2+\frac{1}{3}R^3}{\frac12 (X^2-\frac14 \pi R^2)}$$
I calculates some values this for some values of $R=1$ and $X$ in excel, results plotted in the $2^{nd}$ graph. The value goes up as $X/R$ approaches $~1~$ (?!). This cannot be correct.
I would appreciate if someone could tell me where I am going wrong. Thanks

WLOG we have a unit circle and a square of side $s$ (presumably your $\frac XR$).
The area is given by the integral $$\int_{\phi=0}^{\pi/4}\int_{r=1}^{r\cos\phi=s}r\,dr\,d\phi=\frac12\int_{\phi=0}^{\pi/4}\left(\frac{s^2}{\cos^2\phi}-1\right)\,d\phi=\frac12\left.\left(s^2\tan\phi-\phi\right)\right|_0^{\pi/4}=\frac{s^2}2-\frac\pi8.$$
This result is obviously correct.
Now for the distance,
$$\int_{\phi=0}^{\pi/4}\int_{r=1}^{r\cos\phi=s}(r-1)r\,dr\,d\phi=\int_{\phi=0}^{\pi/4}\left(\frac{s^3}{3\cos^3\phi}-\frac{s^2}{2\cos^2\phi}+\frac16\right)\,d\phi\\ =\left.\left(\frac{s^3}3F(\phi)-\frac{s^2}2\tan\phi+\frac\phi6\right)\right|_0^{\pi/4}=\frac{as^3}3-\frac{s^2}2+\frac\pi{24}$$
where (by Alpha) $a=\dfrac1{\sqrt2}+\text{artanh}\left(\tan\dfrac\pi8\right)=1.14779357469631\cdots$.
The ratio is a smooth, quasi-linear function: