I posted some linq-ified geometric operations in this post:
How to pin one IEnumerable to yourself
The centroid calculation I posted is different from what was posted on @Thomas Levesque. I got it from Wikipedia - Centroid . Its appearance is much simpler than the one I posted.
Here is my algorithm (it uses SignedArea and Pairwise from the link above):
public static Position Centroid(IEnumerable<Position> pts) { double a = SignedArea(pts); var c = pts.Pairwise((p1, p2) => new { x = (p1.X + p2.X) * (p1.X * p2.Y - p2.X * p1.Y), y = (p1.Y + p2.Y) * (p1.X * p2.Y - p2.X * p1.Y) }) .Aggregate((t1, t2) => new { x = t1.x + t2.x, y = t1.y + t2.y }); return new Position(1.0 / (a * 6.0) * cx, 1.0 / (a * 6.0) * cy); }
There are other algorithms in this link that may be useful to you.
source share