# Mean line segment length

In geometry, the mean line segment length is the average length of a line segment connecting two points chosen uniformly at random in a given shape. In other words, it is the expected Euclidean distance between two random points, where each point in the shape is equally likely to be chosen.
Even for simple shapes such as a square or a triangle, solving for the exact value of their mean line segment lengths can be difficult because their closed-form expressions can get quite complicated. As an example, consider the following question:
    What is the average distance between two randomly chosen points inside a square with side length 1?
While the question may seem simple, it has a fairly complicated answer; the exact value for this is ${\frac {2+{\sqrt {2}}+5\ln(1+{\sqrt {2}})}{15}}$.

## Formal definition

The mean line segment length for an n-dimensional shape S may formally be defined as the expected Euclidean distance ||⋅|| between two random points x and y,
    $\mathbb {E} [\|x-y\|]={\frac {1}{\lambda (S)^{2}}}\int _{S}\int _{S}\|x-y\|\,d\lambda (x)\,d\lambda (y)$
where λ is the n-dimensional Lebesgue measure.
For the two-dimensional case, this is defined using the distance formula for two points (x$_1$, y$_1$) and (x$_2$, y$_2$)
    ${\frac {1}{\lambda (S)^{2}}}\iint _{S}\iint _{S}{\sqrt {(x_{1}-x_{2})^{2}+(y_{1}-y_{2})^{2}}}\,dx_{1}\,dy_{1}\,dx_{2}\,dy_{2}.$

## Approximation methods

Since computing the mean line segment length involves calculating multidimensional integrals, various methods for numerical integration can be used to approximate this value for any shape.
One such method is the Monte Carlo method. To approximate the mean line segment length of a given shape, two points are randomly chosen in its interior and the distance is measured. After several repetitions of these steps, the average of these distances will eventually converge to the true value.
These methods can only give an approximation; they cannot be used to determine its exact value.

## Formulas
### Line segment

For a line segment of length d, the average distance between two points is ⁠1/3⁠d.

### Triangle

For a triangle with side lengths a, b, and c, the average distance between two points in its interior is given by the formula
    ${\frac {4ss_{a}s_{b}s_{c}}{15}}\left[{\frac {1}{a^{3}}}\ln \left({\frac {s}{s_{a}}}\right)+{\frac {1}{b^{3}}}\ln \left({\frac {s}{s_{b}}}\right)+{\frac {1}{c^{3}}}\ln \left({\frac {s}{s_{c}}}\right)\right]+{\frac {a+b+c}{15}}+{\frac {(b+c)(b-c)^{2}}{30a^{2}}}+{\frac {(a+c)(a-c)^{2}}{30b^{2}}}+{\frac {(a+b)(a-b)^{2}}{30c^{2}}},$
where $s=(a+b+c)/2$ is the semiperimeter, and $s_{i}$ denotes $s-i$.
For an equilateral triangle with side length a, this is equal to
    $\left({\frac {4+3\ln 3}{20}}\right)a\approx 0.364791843\ldots a.$

### Square and rectangles

The average distance between two points inside a square with side length s is
    $\left({\frac {2+{\sqrt {2}}+5\ln(1+{\sqrt {2}})}{15}}\right)s\approx 0.521405433\ldots s.$
More generally, the mean line segment length of a rectangle with side lengths l and w is
    ${\frac {1}{15}}\left[{\frac {l^{3}}{w^{2}}}+{\frac {w^{3}}{l^{2}}}+d\left(3-{\frac {l^{2}}{w^{2}}}-{\frac {w^{2}}{l^{2}}}\right)+{\frac {5}{2}}\left({\frac {w^{2}}{l}}\ln \left({\frac {l+d}{w}}\right)+{\frac {l^{2}}{w}}\ln \left({\frac {w+d}{l}}\right)\right)\right]$
where $d={\sqrt {l^{2}+w^{2}}}$ is the length of the rectangle's diagonal.
If the two points are instead chosen to be on different sides of the square, the average distance is given by
    $\left({\frac {2+{\sqrt {2}}+5\ln(1+{\sqrt {2}})}{9}}\right)s\approx 0.869009\ldots s.$

### Cube and hypercubes

The average distance between points inside an n-dimensional unit hypercube is denoted as Δ(n), and is given as
    $\Delta (n)=\underbrace {\int _{0}^{1}\cdots \int _{0}^{1}} _{2n}{\sqrt {(x_{1}-y_{1})^{2}+(x_{2}-y_{2})^{2}+\cdots +(x_{n}-y_{n})^{2}}}\,dx_{1}\cdots \,dx_{n}\,dy_{1}\cdots \,dy_{n}$
The first two values, Δ(1) and Δ(2), refer to the unit line segment and unit square respectively.
For the three-dimensional case, the mean line segment length of a unit cube is also known as Robbins constant, named after David P. Robbins. This constant has a closed form,
    $\Delta (3)={\frac {4+17{\sqrt {2}}-6{\sqrt {3}}-7\pi }{105}}+{\frac {\ln(1+{\sqrt {2}})}{5}}+{\frac {2\ln(2+{\sqrt {3}})}{5}}.$
Its numerical value is approximately 0.661707182... (sequence A073012 in the OEIS)
Andersson et. al. (1976) showed that Δ(n) satisfies the bounds
    ${\tfrac {1}{3}}n^{1/2}\leq \Delta (n)\leq ({\tfrac {1}{6}}n)^{1/2}{\sqrt {{\frac {1}{3}}\left[1+2\left(1-{\frac {3}{5n}}\right)^{1/2}\right]}}.$
Choosing points from two different faces of the unit cube also gives a result with a closed form, given by,
    ${\frac {4+17{\sqrt {2}}-6{\sqrt {3}}-7\pi }{75}}+{\frac {7\ln {(1+{\sqrt {2}})}}{25}}+{\frac {14\ln {(2+{\sqrt {3}})}}{25}}.$

### Circle and sphere

The average chord length between points on the circumference of a circle of radius r is
    ${\frac {4}{\pi }}r\approx 1.273239544\ldots r$
And picking points on the surface of a sphere with radius r is
    ${\frac {4}{3}}r$

### Disks

The average distance between points inside a disk of radius r is
    ${\frac {128}{45\pi }}r\approx 0.905414787\ldots r.$
The values for a half disk and quarter disk are also known.
For a half disk of radius 1:
    ${\frac {64}{135}}{\frac {12\pi -23}{\pi ^{2}}}\approx 0.706053409\ldots$
For a quarter disk of radius 1:
    ${\frac {32}{135\pi ^{2}}}(6\ln {(2{\sqrt {2}}-2)}-94{\sqrt {2}}+48\pi +3)\approx 0.473877262\ldots$

### Balls

For a three-dimensional ball, this is
    ${\frac {36}{35}}r\approx 1.028571428\ldots r.$
More generally, the mean line segment length of an n-ball is
    ${\frac {2n}{2n+1}}\beta _{n}r$
where β$_n$ depends on the parity of n,
    $\beta _{n}={\begin{cases}{\dfrac {2^{3n+1}\,(n/2)!^{2}\,n!}{(n+1)\,(2n)!\,\pi }}&({\text{for even }}n)\\{\dfrac {2^{n+1}\,n!^{3}}{(n+1)\,((n-1)/2)!^{2}\,(2n)!}}&({\text{for odd }}n)\end{cases}}$

## General bounds

Burgstaller and Pillichshammer (2008) showed that for a compact subset of the n-dimensional Euclidean space with diameter 1, its mean line segment length L satisfies
    $L\leq {\sqrt {\frac {2n}{n+1}}}{\frac {2^{n-2}\Gamma (n/2)^{2}}{\Gamma (n-1/2){\sqrt {\pi }}}}$
where Γ denotes the gamma function. For n = 2, a stronger bound exists.
    $L\leq {\frac {229}{800}}+{\frac {44}{75}}{\sqrt {2-{\sqrt {3}}}}+{\frac {19}{480}}{\sqrt {5}}=0.678442\ldots$