以下代码有什么问题?

时间:2011-09-07 22:13:57

标签: wpf geometry intersection

我得到一个完整的椭圆而不是两者之间的交点。有什么问题?

<Canvas x:Name="mainCnavs">
<Ellipse x:Name="ellipse" Width="100" Height="50" Canvas.Top="100" Canvas.Left="300" Fill="Transparent" Stroke="Red" StrokeThickness="3"/>
<Ellipse x:Name="ellipse1" Width="100" Height="50" Canvas.Top="100" Canvas.Left="250" Fill="Transparent" Stroke="Red" StrokeThickness="3"/>
</Canvas>

 public partial class MainWindow : Window
    {
        CombinedGeometry g;
        Path p;
        public MainWindow()
        {
            InitializeComponent();
            mainWindow.Loaded += new RoutedEventHandler(mainWindow_Loaded);

        }

        void mainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            g = new CombinedGeometry(ellipse.RenderedGeometry, ellipse1.RenderedGeometry);
            g.GeometryCombineMode = GeometryCombineMode.Intersect;
            statusBar.Text = "top left " + g.Bounds.TopLeft.ToString() +"   bounds.size   " + g.Bounds.Size + "    bounds " + g.Bounds.ToString();
            p = new Path();
            p.Data = g;
            p.Fill = Brushes.Green;
            mainCnavs.Children.Add(p);
        }


}

如果我将椭圆更改为矩形,并且联合使两个形状重叠。它表现得好像两个几何没有定位,这是因为我使用的是渲染几何,它没有与之相关的位置?那么获得形状几何形状的另一种方法是什么呢?

2 个答案:

答案 0 :(得分:1)

您正在看到正确的交叉点,它应该只是椭圆的形状。你有一件事没有考虑过,几何都在原点(相对于画布的位置是无关紧要的)。由于两个几何形状和位置相同,因此当您获得交叉点时,您将看到相同的形状。尝试以任何方式改变椭圆的形状,你会看到差异。

您需要做的是将其中一个几何体转换为适当的偏移量,以获得正确的形状。甚至可能不会将附加的TopLeft属性设置在一起。

要证明你可以做的事情:

var ellipseGeometry1 = new EllipseGeometry(new Rect(50, 0, 100, 50));
var ellipseGeometry2 = new EllipseGeometry(new Rect(0, 0, 100, 50));
var ellipse1 = new Path { Data = ellipseGeometry1, Stroke = Brushes.Red, Fill = Brushes.Transparent, StrokeThickness = 3 };
var ellipse2 = new Path { Data = ellipseGeometry2, Stroke = Brushes.Red, Fill = Brushes.Transparent, StrokeThickness = 3 };
Canvas.SetTop(ellipse1, 100);
Canvas.SetLeft(ellipse1, 250);
Canvas.SetTop(ellipse2, 100);
Canvas.SetLeft(ellipse2, 250);
mainCanvas.Children.Add(ellipse1);
mainCanvas.Children.Add(ellipse2);
var g = new CombinedGeometry
{
    Geometry1 = ellipseGeometry1,
    Geometry2 = ellipseGeometry2,
    GeometryCombineMode = GeometryCombineMode.Intersect,
};
var p = new Path
{
    Data = g,
    Stroke = Brushes.Transparent,
    Fill = Brushes.Green,
};
mainCanvas.Children.Add(p);

答案 1 :(得分:1)

是的,问题在于使用RenderedGeometry,因为它不包含相对于Canvas的位置。

尝试使用自己的Ellipses位置构建的EllipseGeometry。

        Rect r1 = new Rect(Canvas.GetLeft(ellipse), Canvas.GetTop(ellipse), ellipse.Width, ellipse.Height);
        Rect r2 = new Rect(Canvas.GetLeft(ellipse1), Canvas.GetTop(ellipse1), ellipse1.Width, ellipse1.Height);
        EllipseGeometry eg1 = new EllipseGeometry(r1);
        EllipseGeometry eg2 = new EllipseGeometry(r2);

        CombinedGeometry g = new CombinedGeometry(eg1, eg2);
        g.GeometryCombineMode = GeometryCombineMode.Intersect;
        //statusBar.Text = "top left " + g.Bounds.TopLeft.ToString() + "   bounds.size   " + g.Bounds.Size + "    bounds " + g.Bounds.ToString();
        Path p = new Path();
        p.Data = g;
        p.Fill = Brushes.Green;
        mainCnavs.Children.Add(p);