PIXELBANKv9.1.0
Menu

You are given a set of 2D points and need to compute their centroid (center of mass).

The centroid is the average position of all points: xΛ‰=1nβˆ‘i=1nxi,yΛ‰=1nβˆ‘i=1nyi\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i, \quad \bar{y} = \frac{1}{n}\sum_{i=1}^{n} y_i

Centroids are fundamental in alignment algorithms because:

  1. They're used to normalize point sets before computing transformations
  2. They help separate translation from rotation/scaling
  3. They minimize the sum of squared distances to all points

In Procrustes analysis and ICP (Iterative Closest Point), centering point sets at the origin simplifies the computation of optimal rotation.

Example:

Input:
points = [(0, 0), (2, 0), (2, 2), (0, 2)]
Output:
(1.0, 1.0)
Reasoning:
  1. Sum all x coordinates: 0 + 2 + 2 + 0 = 4
  2. Sum all y coordinates: 0 + 0 + 2 + 2 = 4
  3. Divide by count (n=4):
    • xΜ„ = 4/4 = 1.0
    • Θ³ = 4/4 = 1.0
  4. Centroid = (1.0, 1.0)

This is the center of the square formed by the four corner points.

Constraints:

  • points is a list of (x, y) tuples
  • Return centroid as (x, y) tuple
  • Round each coordinate to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.
Compute Centroid - Easy | PixelBank