Basic calculation for a WPF polygon: area and center

The System.Windows.Shapes.Shape namespace provides access to a Polygon object that can be used in XAML or code.

Is there a Microsoft library that provides some very simple calculations in the field of Polygon or centriod?

My preference is not to re-execute these functions on your own or not to copy the math / geometry library.

+4
source share
2 answers

The RenderedGeometry property returns a Geometry object, which itself has GetArea .

There seems to be nothing that a centroid could calculate, but this should be fairly easy to do based on the Points Polygon property:

 Point centroid = polygon.Points.Aggregate( new { xSum = 0.0, ySum = 0.0, n = 0 }, (acc, p) => new { xSum = acc.xSum + pX, ySum = acc.ySum + pY, n = acc.n + 1 }, acc => new Point(acc.xSum / acc.n, acc.ySum / acc.n)); 
+8
source

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.

+2
source

All Articles