Find the centroid of a polygon with weighted vertices
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If each polygon vertex carries its own weight, the "centroid" you want is usually the weighted average of the vertex coordinates, not the ordinary area centroid of the polygon interior. That distinction matters because weighted vertices describe mass or importance concentrated at corner points, while the standard polygon centroid assumes mass is spread uniformly across the shape.
Use the Weighted-Average Formula
For vertices v_i = (x_i, y_i) with weights w_i, the weighted centroid is:
- '
C_x = sum(w_i * x_i) / sum(w_i)' - '
C_y = sum(w_i * y_i) / sum(w_i)'
This is exactly the center of mass for point masses placed at the polygon vertices.
A direct Python implementation is simple:
That produces the weighted center from the supplied vertex masses alone.
Work Through an Example
Take three weighted vertices:
- '
(2, 3)with weight2' - '
(5, 7)with weight4' - '
(8, 5)with weight3'
Compute the weighted sums:
- '
sum(w_i * x_i) = 2*2 + 4*5 + 3*8 = 48' - '
sum(w_i * y_i) = 2*3 + 4*7 + 3*5 = 49' - '
sum(w_i) = 2 + 4 + 3 = 9'
So the centroid is:
- '
C_x = 48 / 9' - '
C_y = 49 / 9'
or about (5.33, 5.44).
This value can lie inside or outside the polygon depending on the weights and geometry. That is not an error. A weighted point-mass centroid reflects the mass distribution, not the visual center of the polygon outline.
Do Not Confuse It With the Area Centroid
For an ordinary filled polygon of uniform density, the centroid is computed from edge cross-products and polygon area, not just by averaging vertex coordinates. That formula is different.
Example area-centroid code for comparison:
If your problem statement says "weighted vertices," use the weighted-vertex formula unless it explicitly says the polygon's interior density varies according to those weights.
Make the Data Model Explicit
In production geometry code, define clearly what a weight means:
- mass concentrated at each vertex
- confidence or importance of each sampled point
- a proxy value used only for averaging
That definition determines whether weighted averaging is correct. If weights represent per-vertex attributes on a boundary sample of a real shape, you may need a more advanced model than a simple point-mass centroid.
Here is a small dataclass version that is easy to test:
Using an explicit type often prevents coordinate-order and weight-order mistakes.
Common Pitfalls
- Mixing up weighted vertex centroid with uniform-area polygon centroid gives the wrong formula.
- Allowing total weight to be zero makes the calculation undefined.
- Ignoring negative weights can produce a result that no longer matches a physical center-of-mass interpretation.
- Assuming the centroid must lie inside the polygon is incorrect for weighted point masses.
- Failing to document what the weights represent makes later geometric reasoning unreliable.
Summary
- For weighted vertices, compute the centroid as the weighted average of the vertex coordinates.
- This is a point-mass model, not the same as the ordinary area centroid of a filled polygon.
- Validate that total weight is positive and that your weight meaning is well defined.
- Use a simple loop or helper function to keep the implementation easy to test.
- Choose the formula based on the physical model, not just on the word "polygon."

