# Average part of a line inside a square

Let $$P$$ be a point inside a square with side length $$1$$. Let $$Q$$ be a point such that $$\|PQ\|=1$$. Find the average length of $$PQ$$ contained in the square.

I have recently been interested in problems like this: Average Distance Between Two Points in a Circular Disk and Average Distance Between Two Randomly Chosen Points in Unit Square. This led me to think up a somewhat similar problem.

However, I am not really sure how to go about solving it. I have been looking at Average distance Between Random Points on a Line Segment but my only idea is that there may be a double summation or integral to cover all the angles the line could be at in relation to the origin. I don't know if that made any sense.

• I suggest: pick a particular $P$ (like the center of the square, or one of the corners) and work it out carefully. The dependence on $P$ isn't clear (at least to me).
– lulu
Commented Nov 6, 2023 at 22:06
• @lulu With $P$ in center the average length is obviously greater than $0.5$, and with $P$ in a corner the average length is clearly $0.25$ Commented Nov 7, 2023 at 0:57
• @DanielMathias the calculation at the center isn't difficult either, but doesn't clarify the general dependence. Of course there's no reason for that to be very clean
– lulu
Commented Nov 7, 2023 at 1:19
• For what it worth, simulation gives a result of $\approx 0.4732$ Commented Nov 7, 2023 at 2:38

This problem deals with the boundary of the intersection of the disk $$D(P,1)$$ with center $$P$$ and radius 1 and the (filled) square $$C$$.

Let us give a complete treatment of the particular case where $$P$$ is at the center of the square ; in this case this frontier is the square itself with the following polar equation (see this answer) :

$$r = \frac{1/2}{|\cos(\theta-\pi/4)|}$$

(the square being with vertices $$(\pm s;0), \ (0; \pm s)$$ (where $$s:=\tfrac{\sqrt{2}}{2}$$) as can be seen on the graphics below :

The mean length is therefore obtained as :

$$\overline{L}= \frac{1}{2 \pi}\int_0^{2 \pi} \frac{1/2 \ d \theta}{|\cos(\theta-\pi/4)|}$$

Taking into account different symmetries, it suffices to work in the first part of the first quadrant and multiply the result by $$8$$ ; this allows in particular to drop the absolute value signs :

$$\overline{L}= \frac{1}{2 \pi} 8 \int_0^{\pi/4} \frac{1/2 \ d \theta}{\cos(\theta-\pi/4)}=\frac{2}{ \pi} \int_0^{\pi/4} \frac{d \theta}{\cos(\theta)}\tag{1}$$

Knowing that a primitive function of $$\frac{1}{\cos(\theta)}$$ is $$\ln \left(\frac{1+\tan(\theta/2)}{1-\tan(\theta/2)}\right)$$ and that $$\tan \tfrac{\pi}{8}= \sqrt{2}-1$$, a closed-form value can be given to (1).