2010-12-12 95 views
4

System.Windows.Shapes.Shape命名空間提供對可用於XAML或代碼的多邊形對象的訪問。 WPF多邊形的基本計算:面積和質心

是否有微軟庫,提供像區或質心的多邊形一些非常基本的計算?

我的選擇是不重新實現這些功能自己或複製的數學/幾何庫。

回答

8

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

感謝您指出如何計算面積和質心;這讓我開始了。 – Zamboni 2010-12-13 03:01:27

2

我發佈了一些LINQ的指明分數幾何操作在這篇文章:

How to Zip one IEnumerable with itself

重心計算我貼是從@Thomas Levesque的發佈的不同。我從Wikipedia - Centroid得到它。他的容貌比我發佈的容易得多。

這裏是我的算法(它使用SignedArea和成對的從上面的鏈接):

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

還有那個鏈接,您可能會發現一些有用的其他算法。

+0

感謝這個答案。 – Zamboni 2010-12-13 03:01:58