News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

C++ IsRectEmpty for two rectangles. Union/Intersection or both?

Started by fearless, September 08, 2023, 08:31:44 PM

Previous topic - Next topic

fearless

Hi, I'm looking for some help with converting a line of c++ code to asm

if ( !((rcInvalid & rcText).IsRectEmpty()) )
I can call the win32 IsRectEmpty function. Both rcInvalid and rcText are RECT. However I'm unsure as to the exact meaning of the line.

  • Is it a union or intersection of rcInvalid and rcText rectangle structures and the result of that RECT is checked to see if its empty with IsRectEmpty?
  • Is it that both rcInvalid and rcText rectangle structures are checked to see if both are empty, by simplifying it and just wrapping both together instead of two separate calls like: rcInvalid.IsRectEmpty(), rcText.IsRectEmpty() ?
  • Is it that the values of both rcInvalid and rcText rectangle structures are ANDed together: rcInvalid.left & rcText.left, rcInvalid.top & rcText.top, rcInvalid.right & rcText.right, rcInvalid.bottom & rcText.bottom. The resulting RECT is checked to see if it is empty with IsRectEmpty?

Any help would be great, thanks in advance.

Biterider

Hi fearless
I don't think that the & operatior is performing a bitwise AND. I suspect that it is overloaded to perform some sort of operation on the RECTs, like "combine". I would check in the code to see if you can find a line containig "operator &".
On the result of this operation, "IsRectEmpty" is performed and then logically inverted.

Biterider

jj2007

Quote from: fearless on September 08, 2023, 08:31:44 PMif ( !((rcInvalid & rcText).IsRectEmpty()) )

This is really ugly. I guess if you debug it at assembly level, you'll see something like this:
invoke IsRectEmpty, addr rcInvalid
push eax
invoke IsRectEmpty, addr rcText
pop edx
and eax, edx

fearless

I found something that helps explain it, as its using MFC and CRect classes.

https://netghost.narod.ru/vcpp6/apf/apf.htm


OperatorDescription
=Copies all the coordinates from the right rectangle operand to the left rectangle, like an ordinary numeric assignment.
+Either displaces a rectangle position if a CPoint or CSize object is added to a rectangle or inflates the coordinates with their corresponding counterparts if a CRect object is added.
-Same as +, except that the coordinates are displaced in a negative direction or deflated if a CRect is used.
+=Same overall effect as + but affects only the current rectangle.
-=Same overall effect as - but affects only the current rectangle.
&Creates an intersection rectangle from the two rectangle operands.
|Creates a union rectangle from the two rectangle operands.
&=Same overall effect as & but affects only the current rectangle.
|=Same overall effect as | but affects only the current rectangle.
==Returns TRUE if the rectangles are identical, otherwise FALSE.
!=Returns FALSE if the rectangles are identical; otherwise returns TRUE.

So I can intersect both rectangles with the win32 api IntersectRect: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-intersectrect
and then test if that resulting rectangle is empty with IsRectEmpty: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-isrectempty

Cheers

jj2007

if ( !((rcInvalid & rcText).IsRectEmpty()) )

Quote from: fearless on September 08, 2023, 09:17:10 PM
&Creates an intersection rectangle from the two rectangle operands.

Yep, that's it - I was on the wrong track. But this syntax sucks a lot. Besides, it makes no sense to invoke IsRectEmpty, because IntersectRect tells you already if it's empty or not.

Testbed attached, pure Masm32 SDK code:
Rect1    RECT <10, 20, 30, 40>
Rect2    RECT <50, 60, 70, 80>
Rect3    RECT <20, 30, 99, 99>
R1 and R2 DON'T have a common section, OutRect is empty
0 left, 0 top, 0 right, 0 bottom
R1 and R3 have a common section, OutRect is NOT empty
20 left, 30 top, 30 right, 40 bottom

R1 and R2 DON'T have a common section, OutRect is empty
0 left, 0 top, 0 right, 0 bottom
R1 and R3 have a common section, OutRect is NOT empty
20 left, 30 top, 30 right, 40 bottom

What the documentation doesn't say is that IntersectRect "fills" the destination rectangle even if there is no overlap; in that case, all elements are zero, see above.

P.S.: Just for fun, I added a GUI version - 100 lines of purest Masm32 SDK code ;-)