Vectorized the centroid calculation function.