1

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
    What you are asking isn't well defined. Either you are asking the centroid of the vertices, which is simply the weighted average of the coordinates, or you are asking the centroid of the surface; but then you must specify how the weights influence their neighborhood. –  Jan 13 '17 at 11:07
  • I think the centroid is not the average of the coordinates: http://math.stackexchange.com/questions/3177/why-doesnt-a-simple-mean-give-the-position-of-a-centroid-in-a-polygon – Nicolas Rougier Jan 13 '17 at 11:12
  • I said the centroid of the vertices. Once again, your question is ill-defined. –  Jan 13 '17 at 11:24
  • Sorry, I read your answer too quickly. I'm looking for the centroid of the surface but I'm not sure yet how the weight would actually influence the centroid. The intuitive idea is that the stronger the weight for a vertex, the stronger it will "attract" the centroid. – Nicolas Rougier Jan 13 '17 at 11:32
  • Unless all the mass is concentrated at the vertices, which is not the case here, the weights must influence a finite area. There is no single way to define this influence so your question has no answer. –  Jan 13 '17 at 11:34
  • More precisely, I would like to compute the centroid $C$ such as:

    $$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
  • 1
    You can indeed work out the formula for the centroid of a triangle with weighted vertices and linear interpolation, then get the global centroid by a suitable weighted average of these centroids. Anyway there isn't a single way to triangulate a polygon, so your centroid still isn't uniquely defined. You should probably tell more of what you are trying to do. –  Jan 13 '17 at 13:17
  • I'm trying to implement "Weighted Voronoi Stippling" (http://www.mrl.nyu.edu/~ajsecord/stipples.html) using Python. The centroid (efficient) computation is given in the article but it might be still too slow. Since I don't need an exact result, I'm looking for a "good enough" approximation. – Nicolas Rougier Jan 13 '17 at 13:26
  • Then the triangulation method is indeed good enough. –  Jan 13 '17 at 13:30
  • Thanks, I'll look into that. – Nicolas Rougier Jan 13 '17 at 13:33

1 Answers1

1

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.