Fix off-by-one bug in centroid calculation