add new centroid calculation function to demo code