10

I would like to sample a uniformly random point in a polygon...

If sample a large number they'd be equally likely to fall into two regions if they have the same area.

This would be quite simple if it were a square since I would take two random numbers in [0,1] as my coordinates.

The shape I have is a regular polygon, but I'd like it to work for any polygon.

https://stackoverflow.com/questions/3058150/how-to-find-a-random-point-in-a-quadrangle

Glorfindel
  • 754
  • 1
  • 9
  • 20
john mangual
  • 1,951
  • 1
  • 21
  • 27

3 Answers3

10
  1. Triangulate the polygon
  2. Determine in which of the triangles the point should lie (weights triangle areas)
  3. Sample the point in the triangle as explained in this post
A.Schulz
  • 12,252
  • 1
  • 42
  • 64
4

One easy way is to find the bounding box for your polygon and use rejection sampling: sample from the bounding box and accept if it falls within the polygon, which will happen with probability $1/2$ at least (I think).

Another possibility is to triangulate your polygon. First sample a triangle in a proportionate way, then sample a random point in the triangle. The latter is simple: up to affine transformations, all triangles are of the form $\{(x,y) : x,y \geq 0, x+y \leq 1\}$. To sample uniformly a point from that distribution, first sample $x \in [0,1]$ according to the density $2(1-x)$ (i.e. sample a uniform $r \in [0,1]$ and compute $x = 1-\sqrt{1-r}$) and then sample $y \in [0,1-x]$ uniformly (i.e. sample a uniform $s \in [0,1]$ and compute $y = (1-x)s$). An even simpler method is to sample $x,y \in [0,1]$, and if $x+y > 1$ replace $(x,y)$ with $(1-x,1-y)$.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
4

This is a little crazy, but should work well even if your polygon is very weird.

Use the Reimann mapping theorem to find a conformal map from the unit disc to your polygon, viewing it as a subset of $\mathbb{C}$. See, for example the references in:

http://siam.org/pdf/news/1297.pdf

Then use the pushforward of a uniform density on the disc as the proposal density in Metropolis-Hastings MCMC sampling.

Nick Alger
  • 1,049
  • 7
  • 18