Distribute a fixed number of points "uniformly" inside a polygon

Just dropping points into the polygon according to a uniform distribution will not, unfortunately, generate very uniform solutions: with extremely high probability there will be large gaps and tight clusters.

Obtaining a highly uniform pattern within an arbitrary (convex) polygon is a difficult problem to solve; exact solutions are known only for special polygons and very small numbers of points. As the number of points $n$ grows, they tend to fall into a hexagonal pattern. However, like real crystals, these patterns can exhibit faults and fractures imposed by the polygon's boundary. As a quick and dirty solution you might estimate the average spacing by dividing the polygon's area by $n$ and solving for the dimensions of a hexagon of that area, creating an hexagonal mesh of those dimensions, and performing some trial overlays of that mesh (translating it a bit in each trial) to find something that works well. You are likely to run into trouble at places just inside the polygon's boundary, though.

One practical way to find solutions that really work is with spatial simulated annealing. Although that's a computationally intensive and purely approximate approach, in practice you can get reasonable solutions for the intended application in an extremely short time (thousands of iterations rather than hundreds of thousands).


The simplest idea is to generate a sequence of points $(X_i,Y_i)$, where $X_i,Y_i$ are independent uniform random variables, in a rectangle containing the polygon (the minimal rectangle bounding the polygon). Then, you simply drop the points which fall outside the polygon. You continue until you get $n$ unrejected points.

As we could expect, this problem has been considered before by many people. You might find this one very useful. The basic question is how fast/simple you want to generate the $n$ points. The method I described is most simple, but might be time consuming.

EDIT: To add some mathematics to this post, let us justify the rejection method and triangulation approaches (though they might seem intuitively clear).

Rejection method approach. Suppose that $(X,Y)$ is uniformly distributed in a rectangle $R$ bounding the polygon $M$, and that $S$ is an arbitrary square contained in $M$. Then, $$ {\rm P}((X,Y) \in S |(X,Y) \in M) = \frac{{{\rm P}((X,Y) \in S )}}{{{\rm P}((X,Y) \in M)}} = \frac{{{\rm area}(S) /{\rm area}(R)}}{{{\rm area}(M)/{\rm area}(R)}} = \frac{{\rm area}(S) }{{{\rm area}(M)}}. $$ Hence, given that $(X,Y) \in M$, $(X,Y)$ is uniformly distributed in $M$.

Triangulation approach. Suppose that the polygon $M$ is partitioned into triangles $T_1,\ldots,T_m$, and set $p_i = {\rm area} (T_i)/ {\rm area}(M)$. Suppose that ${\rm P}((X,Y) \in T_i)=p_i$ and that, given $(X,Y) \in T_i$, $(X,Y)$ is uniformly distributed in $T_i$. Now, for fixed $k$, let $S \subset M$ an arbitrary square contained in $T_k$ (think of arbitrarily small squares). Then, $$ {\rm P}((X,Y) \in S) = \sum\limits_{i = 1}^m {{\rm P}((X,Y) \in S|(X,Y) \in T_i )p_i } = {\rm P}((X,Y) \in S|(X,Y) \in T_k )p_k. $$ Since, given that $(X,Y) \in T_k$, $(X,Y)$ is uniformly distributed in $T_k$, we have $$ {\rm P}((X,Y) \in S|(X,Y) \in T_k ) = \frac{{{\rm area}(S)}}{{{\rm area}(T_k )}}. $$ Hence, $$ {\rm P}((X,Y) \in S) = \frac{{{\rm area}(S)}}{{{\rm area}(T_k )}}\frac{{{\rm area}(T_k )}}{{{\rm area}(M)}} = \frac{{{\rm area}(S)}}{{{\rm area}(M)}}. $$ We conclude that $(X,Y)$ is uniformly distributed in $M$.