【问题标题】:Basic Calculation for WPF Polygon: Area and CentroidWPF 多边形的基本计算:面积和质心
【发布时间】:2011-05-24 07:53:51
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# wpf geometry polygon


    【解决方案1】:

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

    【讨论】:

    • 感谢您指出如何计算面积和质心;这让我开始了。
    【解决方案2】:

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

    How to Zip one IEnumerable with itself

    我发布的质心计算与@Thomas Levesque 发布的不同。我是从Wikipedia - 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); 
      } 
    

    该链接上还有一些您可能会觉得有用的其他算法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-22
      • 2013-11-13
      • 1970-01-01
      • 2016-05-21
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      相关资源
      最近更新 更多