centroid calculation
weighted vertices
polygon geometry
computational geometry
mathematical algorithms

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:

python
1from typing import Iterable
2
3
4def weighted_vertex_centroid(points: Iterable[tuple[float, float, float]]) -> tuple[float, float]:
5    total_weight = 0.0
6    weighted_x = 0.0
7    weighted_y = 0.0
8
9    for x, y, w in points:
10        if w < 0:
11            raise ValueError("weights must be non-negative")
12        total_weight += w
13        weighted_x += x * w
14        weighted_y += y * w
15
16    if total_weight == 0:
17        raise ValueError("total weight must be positive")
18
19    return weighted_x / total_weight, weighted_y / total_weight
20
21
22pts = [(2.0, 3.0, 2.0), (5.0, 7.0, 4.0), (8.0, 5.0, 3.0)]
23print(weighted_vertex_centroid(pts))

That produces the weighted center from the supplied vertex masses alone.

Work Through an Example

Take three weighted vertices:

  • '(2, 3) with weight 2'
  • '(5, 7) with weight 4'
  • '(8, 5) with weight 3'

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:

python
1def polygon_area_centroid(vertices: list[tuple[float, float]]) -> tuple[float, float]:
2    area_twice = 0.0
3    cx = 0.0
4    cy = 0.0
5
6    for i in range(len(vertices)):
7        x1, y1 = vertices[i]
8        x2, y2 = vertices[(i + 1) % len(vertices)]
9        cross = x1 * y2 - x2 * y1
10        area_twice += cross
11        cx += (x1 + x2) * cross
12        cy += (y1 + y2) * cross
13
14    if area_twice == 0:
15        raise ValueError("degenerate polygon")
16
17    return cx / (3 * area_twice), cy / (3 * area_twice)

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:

python
1from dataclasses import dataclass
2
3
4@dataclass
5class WeightedPoint:
6    x: float
7    y: float
8    weight: float

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."

Course illustration
Course illustration

All Rights Reserved.