组合几何零件的面积计算

时间:2012-01-13 20:42:10

标签: wpf geometry

我需要根据几何交集来计算一些区域。 在我的例子中,我有以下Geometries:

  • 左RectangleGeometry
  • 右RectangleGeometry
  • EllipseGeometry

椭圆位于矩形的中间,我想要两个得到以下数据:

  • 椭圆和左方矩形之间的交点区域
  • 椭圆和右方矩形之间的交点区域
  • 椭圆的总面积。

问题在于椭圆的总面积,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 个答案:

答案 0 :(得分:1)

我认为你可能永远不会从CombinedGeometry获得“精确”区域。正如所料,WPF不使用“理想”方法来计算此值。来自MSDN:“一些几何方法(如GetArea)生成或使用几何”的多边形近似

检查MSDN