# Sampling uniformly from all possible line segments of a given length that fit inside a container

Consider that task of randomly placing a line segment of some length $L$ near a plane s.t. a point $p$ at the center of the line segment is at most a distance $H$ from the plane and intersections between the line segment and the plane are forbidden. Alternatively, consider the task of randomly placing the line segment in a box or sphere that is large enough to contain the line segment without intersection between the segment and the boundaries of the container.

What is a proper procedure for "randomly" placing the line segment in scenarios like those outlined above s.t. a placement operation constitutes a uniform random sampling of all legal configurations of the line segment? For example, it's not at all clear to me that procedures such as: (a) selecting two points a distance $L$ apart and testing for intersection, or (b) selecting one point for the center of the line segment that randomly generating $(r,\theta,\phi)$ values for the line segment's 3D orientation, then testing for intersection, are free from bias.

Does it depend on the exact container geometry, or is there a general solution? What about for cylinders of radius $r$?

Maybe to talk a bit more about what I'm after - if we had something like a monoatomic gas molecule diffusing in a box via a Brownian motion, we can say (to rough approximation) that the system is ergodic and that a uniformly chosen coordinate, with room to admit a sphere approximately as large as the atomic particle, represents a uniform random sampling of a possible position of the particle. Of course, we also have to assume some kind of "reachability" condition here, i.e. that the particle can diffuse from one point to any other point. How can we perform the same random sampling if we stretched the sphere out into a cylinder?

## 2 Answers

You might consider first the distribution in two special cases: when the (open) segment is first placed in a (closed) ball of diameter equal to the length of the ball, and when the ball has two (diametrically opposed and congruent) caps cut off from it. I can imagine a distribution that is proportional to the spherical surface area for each case, but I can also imagine them being weighted the same. If there is a gravitational or magnetic field, I can also imagine a distribution that does not have the symmetry of either of these domains. Hopefully the physics will suggest to you how to weight these two cases.

When you have such a weighting, you can now "integrate" over the whole domain, using these two cases and similar. I do not have any examples for you doing this form of integration, butI am confident that someone else here can supply you with an example or a reference.

Here is one possible approach. Roughly: Construct a configuration space of all possible legal placements of the segment, and then generate random points within that configuration space using some of the tools developed for robotic path planning.

As an example, consider a unit-length segment $s$ that must lie in the upper halfplane of $\mathbb{R}^2$ (your opening example). Let $\theta$ be the angle of $s$ w.r.t. the vertical. Use the midpoint $m$ of $s$ to indicate its position. For $\theta=0$, $m$ must lie in $y \ge \frac{1}{2}$. For other values of $\theta$, $m$ lies in $y \ge \frac{1}{2} \cos \theta$. Imagine stacking up all these half-space regions as $\theta$ increases. Now you have a $3$-dimensional configuration space for all legal placements of $s$.

Similarly, you could define a configuration space for any of your scenarios, although they could be quite complicated, high-dimensional spaces. Nevertheless, there has been a large amount of work on random sampling from such spaces for the purposes of path planning for robot arms. For example, probabilistic roadmap algorithms.