= Solution
Here “centroid” means the \b[<vertex centroid>], the arithmetic mean of the listed <vertices>, rather than the uniform-area <centroid> of the polygon's interior:
``
centroid :: Polygon -> Point
centroid [] = error "centroid: empty vertex list"
centroid ps = (sx/n,sy/n)
where
(sx,sy) = foldr (\(x,y) (u,v) -> (x+u,y+v)) (0,0) ps
n = fromIntegral (length ps)
``
The nonempty input restriction prevents division by zero; computing both sums and the length takes <linear time>.
Back to article page