2012-01-13 24 views
0

我需要根據幾何相交來計算一些面積。 在我的例子,我有以下幾何:組合幾何部分的面積計算

  • 左RectangleGeometry
  • 右RectangleGeometry
  • EllipseGeometry

橢圓是矩形的中間,我要兩張得到以下數據:

  • Ellipse和left re的交點區域ctangle
  • 橢圓與右矩形相交的區域
  • 橢圓的總面積。

的問題是,該橢圓的總面積,EllipseGeometry.GetArea(),並且 「LeftEllipseGeometry」 .GetArea()+ 「RightEllipseGeometry」 .GetArea()是不同的。 交點區域的總和必須與ellipe區域相同。

我做了一個例子,你可以測試和看到問題。

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