2012-01-13 21 views
0

ジオメトリの交点に基づいていくつかの領域を計算する必要があります。 私の例では、私は以下のジオメトリがあります複合ジオメトリの面積の計算

  • 左RectangleGeometryを
  • 右RectangleGeometryを
  • EllipseGeometry

楕円は、四角形の真ん中にあると私は2は、以下のデータを取得したいが:

  • 楕円と左からの交点ctangle
  • 楕円と右矩形の交点の領域
  • 楕円の総面積。

問題は、楕円の総面積は、EllipseGeometry.GetArea()、及び "LeftEllipseGeometry" .GetArea()+ "RightEllipseGeometry" .GetArea()が異なっているということです。 交点の面積の合計は、楕円領域と同じでなければなりません。

問題をテストして確認できるサンプルを作成しました。

MainWindow.xaml.cs 
void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     //LEFT 
     rectLeft = new RectangleGeometry(); 
     rectLeft.Rect = new Rect(new Point(75, 100), new Point(700, 600)); 
     Path pathRectLeft = new Path(); 
     pathRectLeft.Stroke = Brushes.Red; 
     pathRectLeft.Data = rectLeft; 
     grdMain.Children.Add(pathRectLeft); 

     //RIGHT 
     rectRight = new RectangleGeometry(); 
     rectRight.Rect = new Rect(new Point(700, 100), new Point(1300, 600)); 
     Path pathRectRight = new Path(); 
     pathRectRight.Stroke = Brushes.Green; 
     pathRectRight.Data = rectRight; 
     grdMain.Children.Add(pathRectRight); 

     //ELLIPSE 
     ellipseGeo = new EllipseGeometry(); 
     ellipseGeo.RadiusX = 200; 
     ellipseGeo.RadiusY = 200; 
     ellipseGeo.Center = new Point(700, 350); 
     Path ellipsePath = new Path(); 
     ellipsePath.Stroke = Brushes.Blue; 
     ellipsePath.Data = ellipseGeo; 
     grdMain.Children.Add(ellipsePath); 
     lblEllipseArea.Content = String.Concat("Area Ellipse = ", ellipseGeo.GetArea());         
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    {    
     CombinedGeometry cgLeft = new CombinedGeometry(); 
     cgLeft.Geometry1 = rectLeft; 
     cgLeft.Geometry2 = ellipseGeo;    
     cgLeft.GeometryCombineMode = GeometryCombineMode.Intersect; 

     Path cgLeftPath = new Path(); 
     cgLeftPath.Stroke = Brushes.Yellow; 
     cgLeftPath.Data = cgLeft; 
     grdMain.Children.Add(cgLeftPath); 
     lblEllipseAreaLeft.Content = String.Concat("Area Left Ellipse = ", cgLeft.GetArea()); 

     CombinedGeometry cgRight = new CombinedGeometry(); 
     cgRight.Geometry1 = rectRight; 
     cgRight.Geometry2 = ellipseGeo; 
     cgRight.GeometryCombineMode = GeometryCombineMode.Intersect; 

     Path cgRightPath = new Path(); 
     cgRightPath.Stroke = Brushes.White; 
     cgRightPath.Data = cgRight; 
     grdMain.Children.Add(cgRightPath); 
     lblEllipseAreaRight.Content = String.Concat("Area Right Ellipse = ", cgRight.GetArea());    

     lblEllipseTotal.Content = String.Concat("Area Ellipse Total = ", cgLeft.GetArea() + cgRight.GetArea()); 

    } 

MainWindow.xaml 

<Grid> 
    <StackPanel Orientation="Vertical" Background="Black"> 
    <Grid Background="Black" Height="700" Name="grdMain"> 

    </Grid> 
    <Grid Background="Black" Height="150"> 
      <StackPanel Orientation="Vertical"> 
      <Button Height="30" Width="70" Click="Button_Click">Click Me!!!</Button> 
      <StackPanel Orientation="Horizontal"> 
       <Label Foreground="White" Name="lblEllipseArea"></Label> 
       <Label Foreground="White" Name="lblEllipseArea2" Margin="20 0 0 0"></Label> 
       <Label Foreground="White" Name="lblEllipseAreaRight" Margin="20 0 0 0"></Label> 
       <Label Foreground="White" Name="lblEllipseAreaRight2" Margin="20 0 0 0"></Label> 
       <Label Foreground="White" Name="lblEllipseAreaLeft" Margin="20 0 0 0"></Label> 
       <Label Foreground="White" Name="lblEllipseAreaLeft2" Margin="20 0 0 0"></Label> 

      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <Label Foreground="White" Name="lblEllipseTotal" Margin="20 0 0 0"></Label> 
       <Label Foreground="White" Name="lblEllipseTotal2" Margin="20 0 0 0"></Label> 
      </StackPanel> 
      </StackPanel> 
     </Grid> 
    </StackPanel> 
</Grid> 

答えて

1

CombinedGeometryから「正確な」領域が決して得られないと思います。期待どおり、WPFはこの値を計算するのに「理想的な」方法を使用しません。 MSDNより: "一部の幾何学的方法(GetAreaなど)は、形状が"の近似の多角形を生成または使用します。

チェックMSDN

関連する問題