6

I'm using Region.IsEmpty() to see if two regions intersect, but I really don't understand why I have to provide a Graphics context.

The official description says

The current transformation of the graphics context g is used to compute the region interior on the drawing surface.

What transformation in two dimensions could possibly separate two overlapping regions, or make a non-empty region empty?

Is it a question of granularity? Of aliasing vs anti-aliasing?

Zano
  • 2,595
  • 27
  • 33

3 Answers3

4

The Region.IsEmpty(Graphics g) method checks to see if the current graphics context specified as g has any items which occupy the specific region.

It's not necessarily checking if two regions intersect, but rather whether a region intersections any other items on a drawing surface. The Graphics instance allows the Region to perform a check on the drawing surface, as this is defined as the Graphics. In a sense, this method is really like [not working code] g.ContainsElementsWhichIntersect(theRegion).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • So `Region` is in a sense a utility class for `Graphics` then? I followed the recommendation at http://stackoverflow.com/questions/3615326/how-can-i-tell-if-two-polygons-intersect/3615409#3615409 (and elsewhere), but it's seems that this is not the way to go then. – Zano Jun 24 '13 at 22:05
  • 1
    @Zano You basically have to draw one region into a graphics, then check with the 2nd. That would give you a "do these intersect" operation. – Reed Copsey Jun 24 '13 at 22:06
  • With the possible exception of Metafile graphics contexts, there's no concept of a Graphics context "having items" or "containing elements". Things you draw are rasterized onto a Bitmap image or HDC. There's no record you can check to see that a particular Region was drawn to produce the current bits, or that the drawing was done by System.Drawing rather than GDI or memory manipulation. With that in mind, your answer doesn't make any sense. – Esme Povirk Jun 25 '13 at 05:24
4

Regions are a GDI feature and strongly correlated to GDI device contexts. You can specify a region with floating point numbers, like the constructor that takes a GraphicsPath or a RectangleF. But the ultimate computations are done with integer precision. Just good enough for pixel accuracy, no more is needed.

The mapping from logical coordinates to device coordinates (i.e. pixels) is guided by the setup of the device context. Which may have a mapping mode other than 1:1. So a region that's a rectangle of, say, 2.0 x 2.0 may end up empty once it is mapped to pixels. Check out SetMapMode() for example.

So do watch for when you intend to use Regions as a general tool, particularly the lack of precision in the result (no better than integer precision) may come as a surprise.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

From your link:

Tests whether this Region has an empty interior on the specified drawing surface.

The operative term in that statement is drawing surface. To have a drawing surface you need a graphics context and therefore an instance of a Graphics object.

That the documentation mentions a transformation is probably just a bit of confusing jargon. It's just a fancy way of saying that the return value will hold true in the Graphic context's current state. If anything changes, such as a ScaleTransform or even a vanilla DrawLine call then there was a "transformation" and then your IsEmpty result may not be valid any more.

Also< I wouldn't be surprised if internally some sort of matrix transform was actually applied with the Region coordinates provided to detect "emptyness".

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189