# Average distance to a random point in a rectangle from an arbitrary point

I'm interested in the mean distance between an arbitrary 2D point, $(p, q)$, and a uniformly distributed point inside a rectangle defined by the lower left and upper right vertices $(x_0, y_0)$ and $(x_1, y_1)$, respectively. There is no constraint that $(p,q)$ need lie within the rectangle.

I figure the solution is given by

$$\int\limits_{y_0}^{y_1}\int\limits_{x_0}^{x_1} \left[(x-p)^2 + (y-q)^2 \right]^{1/2} \text{d}x\text{d}y,$$

which my basic knowledge of calculus prevents from tackling in any way other than numerically. I'm not expecting a closed analytic solution (though it would be great!), but are there any tricks that might help approximate it? And more generally, does this type of integral have a name that would help me look up methods associated with its solution?

Also happy to settle for the assumption that $(p,q)$ lies outside the rectangle.

Thanks all.

• shift the integration coordinates by $x'=x+p$ and $y'=y+q$ and then change to polar coordinates. – tired Jan 27 '15 at 17:56
• i think yes. But one has to be careful on how to split the integral then – tired Jan 27 '15 at 18:02
• I tried that too, but I agree with @tired - the limits of integration become terribly complicated when transforming to polars. – Gabriel Jan 27 '15 at 18:06
• hi, the integrals can be also done without polar coordinates. The inner one is quite easy (after shifting) done using IBP. The outer one can also be performed by parts, but this will get a litte bit messy. I think the final (indefinite) result should only combinations of , $x^n$,$\log(x)$ and $\sqrt{x^2+y^2}$ and also x and y interchanged. $n$ should be $\{0,1,2,3\}$. – tired Jan 27 '15 at 18:33

In principle the integral is not too difficult using polar coordinates. I will do everything for $x_0=-x_1 ,y_0 =- y_1$ and $p=q=0$ to simplify notation a little bit. Then we don't have to shift and the whole area is given by $4$ times the integral over the first quadrant. We split the remaining integral in two regions.

1.) The triangle with vertices $\{0,0\} ,\{x_1,0\},\{x_1,y_1\}$. Here the upper bound of $r$ goes from $x_1$ to $r_1=\sqrt{x_1^2+y_1^2}$ which means that $r\in [0,\frac{x_1}{\cos(\phi)}]$. The value of $\phi$ is between $0$ and $\phi_1=\arctan(y_1/x_1)$

2.) The triangle with vertices $\{0,0\} ,\{0,y_1\},\{x_1,y_1\}$. Here the upper bound of $r$ goes from $y_1$ to $r_1=\sqrt{x_1^2+y_1^2}$ which means that $r\in [0,\frac{y_1}{\sin(\phi)}]$. The value of $\phi$ is between $\phi_1=\arctan(y_1/x_1)$ and $\pi/2$

We get: $$\int_{-y_1}^{y_1}\int_{-x_1}^{x_1} dx dy\sqrt{x^2+y^2}=4\int_{0}^{y_1}\int_{0}^{x_1} dx dy\sqrt{x^2+y^2}=\\ 4\int_{0}^{\phi_1}d\phi\int_{0}^{x_1/\cos(\phi_1)}dr r^2+4\int_{\phi_1}^{\pi/2}d\phi\int_{0}^{y_1/\sin(\phi_1)}dr r^2=\\ \frac{4}{3}x_1^3\int_{0}^{\phi_1}d\phi \left(\frac{1}{\cos(\phi)}\right)^3+\frac{4}{3}y_1^3\int_{\phi_1}^{\pi/2}d\phi\left(\frac{1}{\sin(\phi)}\right)^3$$

The remaining integrals can be done by IBP and the substitutions $y=\arccos{\phi}$ , $y=\arcsin{\phi}$ respectivly.

The result in the end can be simplified a bit but looks still very messy, so i spare it here. But eventually you get a closed form :)

Can you take it from here?

• Thanks very much, I'll take it from there and see what I end up with! – Gabriel Jan 28 '15 at 9:48
• Just for clarification: In the notation you used the integrand comes after the differential? – flawr Jan 28 '15 at 9:48
• yes, maybe the other way round would be a little bit more readable – tired Jan 28 '15 at 9:50
• A (rephrased) question on the proposed solution: how can one make $x_0 = -x_1$ and $y_0 = -y_1$ without affecting the result ? I mean I understand how one can shift the problem to make $p = 0, q = 0$ since the average distance won't be affected. But changing $x_0$ seems to affect it. For instance $(p, q) = (1,1), (x_0, y_0) = (2,2), (x_1, y_1) = (3,3)$ can be shifted to $(p, q) = (0,0), (x_0, y_0) = (1,1), (x_1, y_1) = (2,2)$ without affecting the average, but changing it to $(p, q) = (0,0), (x_0, y_0) = (-2,-2), (x_1, y_1) = (2,2)$ would change the result, no ? – Jan May 9 '15 at 15:50
• A bit more thinking and I think I can answer my own question: the rectangle between $(x_0, y_0)$ and $(x_1, y_1)$ can be expressed as a composition of the symmetrical rectangles $(-x_1,-y_1), (x_1, y_1)$, $(-x_0,-y_0), (x_0, y_0)$, $(-x_0,-y_1), (x_0, y_1)$ and $(-x_1,-y_0), (x_1, y_0)$, so a solution to a symmetrical rectangle will solve the arbitrary rectangle problem. – Jan May 9 '15 at 16:00