WPF 多边形的基本计算:面积和质心

发布于 2024-10-07 09:42:48 字数 177 浏览 0 评论 0原文

System.Windows.Shapes.Shape 命名空间提供对可在 XAML 或代码中使用的 Polygon 对象的访问。

是否有 Microsoft 库提供一些非常基本的多边形(如面积或质心)计算?

我的偏好是不要自己重新实现这些函数或复制数学/几何库。

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

Is there a Microsoft library that provides some very basic calculations on a Polygon like area or centriod?

My preference is to not re-implement these functions myself or copy a math/geometry library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2024-10-14 09:42:48

RenderedGeometry 属性返回一个Geometry 对象,它本身有一个GetArea 方法。

似乎没有任何东西可以计算质心,但根据PolygonPoints属性,它应该很容易做到:

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));

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

There doesn't seem to be anything to compute the centroid, but it should be quite easy to do, based on the Points property of the Polygon:

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));
冷了相思 2024-10-14 09:42:48

我在这篇文章中发布了一些 linq 化的几何操作:

How to Zip one IEnumerable with本身

我发布的质心计算与@Thomas Levesque 发布的不同。我从 维基百科 - Centroid 获得它。他的看起来比我发布的简单得多。

这是我的算法(它使用上面链接中的 SignedArea 和 Pairwise):

  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); 
  } 

该链接上还有一些其他算法,您可能会发现有用。

I posted some linq-ified geometric operations in this post:

How to Zip one IEnumerable with itself

The centroid computation I posted is different from the one that @Thomas Levesque posted. I got it from Wikipedia - Centroid. His looks a lot simpler than the one that I posted.

Here is my algorithm (it makes use of 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) * c.x, 1.0 / (a * 6.0) * c.y); 
  } 

There are also some other algorithms at that link that you might find useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文