问题
I get a full ellipse not the intersection between the two. What is wrong?
<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);
}
}
If I change the ellipse to a rectangle, and do a union is giving the two shapes overlapped. It behaves as if the two geometries have no positioning, is this because I am using the rendered geometry and it has no position associated with it? Then what is another way then to get the geometry of the shape?
回答1:
Yes the problem is with using the RenderedGeometry, as it does not contain the position relative to the Canvas.
Try making use of your own EllipseGeometry built from the positions of the Ellipses.
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);
回答2:
You are seeing the right intersection, it should be just the shape of the ellipse. There's one thing you haven't considered, the geometries are all at the origin (the position relative to the canvas is irrelevant). Since both geometries are the same shape and location, you'll see the same shape when you get the intersection. Try changing the shape of either ellipse in any way and you'll see the difference.
What you need to do is translate one of the geometries by the appropriate offsets the get the right shape. Maybe even leave out setting the attached Top
and Left
properties all together.
To demonstrate what you could do instead:
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);
来源:https://stackoverflow.com/questions/7341133/what-is-wrong-with-the-following-code