# Probability distribution of the distance of a point in a square to a fixed point

The question is, given a fixed point with coordinates X,Y, in a square of size N. What is the probability distribution of the distance to a random point. More specifically, what is the probability distribution of the square root of the square sum of 2 randomly independent uniform variables (0,N).

• Your "specifically" suggests the distance from a random point to the point $(0,0)$. Are you asking about the distance from a random point to an arbitrary $(X,Y)$ or to $(0,0)$? – David Luke Thiessen Jun 15 at 4:39
• Well, the calculation should be extremely similar, thats whay I thought about the simple case, and then I could extend it to a general one. – Jorge Kageyama Jun 15 at 17:48

To find the distribution of the distance between the origin, $$(0,0)$$ and a random point on the unit square $$(0,1)^2$$ we can integrate over the area that is within $$d$$ of the origin. That is, find the cdf $$P(\sqrt{X^2 + Y^2} \leq d)$$, then take the derivative to find the pdf. Extensions to the square $$(0,N)$$ are immediate.

Case 1: $$0 \leq d \leq 1$$

$$F_{D}(d) = P(\sqrt{X^2 + Y^2} \leq d) = P(X^2 + Y^2 \leq d^2) \\ = \int_{0}^{d} \int_{0}^{\sqrt{d^2-x^2}}1dydx \\ = \int_{0}^{d} \sqrt{d^2 - x^2}dx\\ = \frac{d^2\pi}{4}$$

Case 2: $$1 \leq d \leq \sqrt{2}$$

$$F_{D}(d) = \int_{0}^{\sqrt{d^2 - 1}} 1 dx + \int_{\sqrt{d^2 - 1}}^{1}\int_{0}^{\sqrt{d^2-x^2}}1dydx \\ =\sqrt{d^2-1} + \int_{\sqrt{d^2-1}}^{1}\sqrt{d^2-x^2}dx\\ = \sqrt{d^2 -1} + \frac{1}{2}\left\{t\sqrt{d^2-t^2}+d^2\tan^{-1}\left(\frac{t}{\sqrt{d^2-t^2}}\right) \right\}|_{\sqrt{d^2-1}}^{1} \\ = \sqrt{d^2-1} + \frac{1}{2}\left\{\sqrt{d^2-1}+d^2\tan^{-1}\left(\frac{1}{\sqrt{d^2-1}}\right) - \sqrt{d^2-1}\sqrt{1}-d^2\tan^{-1}\left(\frac{\sqrt{d^2-1}}{1}\right)\right\} \\ =\sqrt{d^2-1} + \frac{d^2}{2}\left\{ \tan^{-1}\left(\frac{1}{\sqrt{d^2-1}}\right)-\tan^{-1}\left(\sqrt{d^2-1}\right)\right\}$$

Taking the derivative gives the density

$$f_{D}(d) = \frac{d\pi}{2}, 0 \leq d \leq 1\\ f_{D}(d) = d\left\{\tan^{-1}\left(\frac{1}{\sqrt{d^2-1}}\right)-\tan^{-1}(\sqrt{d^2-1})\right\}, 1 \leq d \leq \sqrt{2} \\$$

Comparing the result with @BruceET's simulation answer on Expected average distance from a random point in a square to a corner, we find it matches exactly.

den <- function(d) {
if(d < 0) {
return(0)
}
if(d < 1) {
return(d*pi/2)
}
if(d < sqrt(2)) {
return(d*(atan(1/sqrt(d^2-1)) - atan(sqrt(d^2-1))))
}
if(d > sqrt(2)) {
return(0)
}
stop()
}
ys <- xs <- seq(from=0,t=1.42,by=0.01)
for(i in seq_along(xs)){
ys[i] <- den(xs[i])
}

set.seed(2021)
x = runif(10^6);  y = runif(10^6)
d = sqrt(x^2 + y^2)
hist(d, prob=T, br=seq(0,1.42,0.02),xlim=c(0,1.5),ylim=c(0,1.5))
lines(xs,ys,col="red",lwd=2)


Created on 2021-06-15 by the reprex package (v2.0.0)

By symmetry this is equal to the distance between random points and $$(0,1)$$, $$(1,0)$$, and $$(1,1)$$. Finding the distance to a point on the boundary or the interior of the square will require considering more cases in the cumulative probability function.

• Thank you very much, this is really cool btw! This is tied now to my real objective. I have a stimuli at random fixed point in the grid, and measure the appearance of X,Y points in the experiment, and want to test if the distance to the stimuli is randomly distributed. Thank you a lot! In handsight, it probably would've just been a lot easier to test X and Y separately and perform multiple testing correction. – Jorge Kageyama Jun 16 at 8:26