compute centroids function