Question

I discovered that even numbers of overlapping shapes added to canvas like that:

GeometryGroup gg = new GeometryGroup();
EllipseGeometry e1 = new EllipseGeometry(new Point(10, 10),  20, 20);
EllipseGeometry e2 = new EllipseGeometry(new Point(10, 10),  20, 20);
//EllipseGeometry e3 = new EllipseGeometry(new Point(10, 10),  20, 20);
gg.Children.Add(e1);
gg.Children.Add(e2);
//gg.Children.Add(e3);

Path p = new Path();
p.Data = gg;
p.Fill = Brushes.Red;

MyCanvas.Children.Add(p);

"clears" all previous shapes. If you run these code you will see nothing, but if you uncomment some lines, a circle will appear. Is anybody able give explaination of this strange behavior?

Was it helpful?

Solution

You need to set the FillRule of the Geometry group accordingly. It must have a value of NonZero. Currently you draw each shape exactly on top of each other, causing the area to be white with an even number of shapes because EvenOdd is the default fill rule:

alt text http://i.msdn.microsoft.com/ms595394.ref_mil_fillrule_stars(en-us,VS.90).png

(Image from http://msdn.microsoft.com/en-us/library/system.windows.media.geometrygroup.fillrule.aspx)

GeometryGroup gg = new GeometryGroup();
gg.FillRule = FillRule.Nonzero;
EllipseGeometry e1 = new EllipseGeometry(new Point(10, 10), 20, 20);
EllipseGeometry e2 = new EllipseGeometry(new Point(10, 10), 20, 20);
gg.Children.Add(e1);
gg.Children.Add(e2);

Path p = new Path();
p.Data = gg;
p.Fill = Brushes.Red;

OTHER TIPS

This occurs because your ellipses are exactly overlapping. The default FillRule for a GeometryGroup is EvenOdd, meaning that a point is considered inside the shape if a line from the point to the exterior crosses an odd number of borders. This is a bit like an XOR rule for shapes. In your case, points inside your shape will always cross an even number of borders (either both e1 and e2, or nothing at all): therefore, no points are ever considered inside the geometry, and no points get filled. Informally, e2 creates a "hole" in e1 which consumes the whole of e1. See the illustration on the GeometryGroup page on MSDN.

If you change the FillRule to NonZero, you will see a more "union"-like effect which is perhaps what you're trying to achieve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top