System.Windows.Shapes.Shape 命名空间提供了可以在 XAML 或代码中使用的 Polygon 对象。
是否有微软库可提供多边形的基本计算,如面积或重心?
我的偏好是不重新实现这些函数或复制数学/几何库。
RenderedGeometry
属性返回一个Geometry
对象,该对象本身具有GetArea
方法。
似乎没有任何计算重心的东西,但是根据Polygon
的Points
属性应该很容易实现:
Point centroid =
polygon.Points.Aggregate(
new { xSum = 0.0, ySum = 0.0, n = 0 },
(acc, p) => new
{
xSum = acc.xSum + p.X,
ySum = acc.ySum + p.Y,
n = acc.n + 1
},
acc => new Point(acc.xSum / acc.n, acc.ySum / acc.n));
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) * c.x, 1.0 / (a * 6.0) * c.y);
}