I would like to compute a weighted centroid of a polygon where each vertex $V_i$ has an associated weight $W_i$. I know the formula for an "homogeneous" polygon but I'm not sure how to insert the weights into the formula.
1 Answers
Let us compute the centroid of a triangle having a surface density linearly interpolated between the vertices. WLOG, the unit triangle by $(0,0),(0,1),(1,0)$ will do. The density function is $w_{xy}=w_{00}+x(w_{10}-w_{00})+y(w_{01}-w_{00})=ax+by+c$ where the coefficients are deduced from the weights at the corners.
For the mass: $$\int_{x=0}^1\int_{y=0}^{1-x}(ax+by+c)\,dy\,dx=\int_{x=0}^1(ax(1-x)+\frac{b(1-x)^2}2+c(1-x))\,dx=\frac a6+\frac b6+\frac c2.$$
For the $x$ moment:
$$\int_{x=0}^1\int_{y=0}^{1-x}x(ax+by+c)\,dy\,dx=\int_{x=0}^1(ax^2(1-x)+\frac{bx(1-x)^2}2+cx(1-x))\,dx\\ =\frac a{12}+\frac b{24}+\frac c6.$$ and similarly for $y$.
Then the centroid is
$$\left(\frac{2a+b+4c}{4a+4b+12c},\frac{a+2b+4c}{4a+4b+12c}\right).$$
For an arbitrary triangle, compute the affine transform that maps the canonical triangle to the arbitrary one, and the centroid will just follow.
To get the centroid of a triangulation, compute the centroid of the centroids, each weighted by the corresponding triangle mass.
$$C = \frac{\int_A {\bf x}\rho({\bf x})dA}{\int_A \rho({\bf x})dA}$$
My region $A$ is a polygon defined by a set of vertices but it would be too costly to iterate over all the points (I'm in a discrete problem). My idea was to interporlate the density function from the vertices in order to compute a centroid (that won't be the same as the exact solution of course).
Considering the polygon as a set of triangles, my idea was to use interpolation such as https://en.wikibooks.org/wiki/GLSL_Programming/Rasterization
– Nicolas Rougier Jan 13 '17 at 13:00