Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Region.IsEmpty() need a Graphics context?

Tags:

.net

gdi+

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?

like image 608
Zano Avatar asked Jun 24 '13 21:06

Zano


3 Answers

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.

like image 97
Hans Passant Avatar answered Oct 22 '22 12:10

Hans Passant


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).

like image 36
Reed Copsey Avatar answered Oct 22 '22 13:10

Reed Copsey


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".

like image 21
Paul Sasik Avatar answered Oct 22 '22 11:10

Paul Sasik