factor out distFromCentroid function for better performance and simplicity