# Difference Of Rectangles

• 09-14-2005
SMurf
Difference Of Rectangles
Hello,

I'm currently on my own window manager/system... just to see if I could do it, really. :D
Most of the mechanics of moving the windows, resizing them, etc. has been laid down, I'm currently working on repainting the windows as necessary. However, I seem to be encountering some trouble. Debugging seems to indicate that there may be an issue with the following function:-
Code:

```t_region *difference(short sX, short sY, short sWidth, short sHeight, short sX1, short sY1, short sWidth1, short sHeight1) {         t_region *regions = NULL, *current;         if (sX < sX1)         {                 current = create_region(sX, sY, (sX + sWidth < sX1) ? sWidth : sX1, sHeight);                 regions = append_regions(regions, current);         }         if (sX + sWidth > sX1 + sWidth1)         {                 current = create_region((sX > sX1 + sWidth1) ? sX : sX1 + sWidth1, sY, sWidth - sWidth1, sHeight);                 regions = append_regions(regions, current);         }         if (sY < sY1)         {                 current = create_region(sX, sY, sWidth, (sY + sHeight < sY1) ? sHeight : sY1);                 regions = append_regions(regions, current);         }         if (sY + sHeight > sY1 + sHeight1)         {                 current = create_region(sX, (sY > sY1 + sHeight1) ? sY : sY1 + sHeight1, sWidth, sHeight - sHeight1);                 regions = append_regions(regions, current);         }         return regions; }```
The function is supposed to find the difference of two overlapping rectangles (the area left when one is "subtracted" from the other). As I don't fancy messing about with complex polygons it simply returns up to 4 rectangles describing the area left.

I've googled for a couple of days looking for a clear reference to match this against, but came up with literally hundreds of API pages(!).

So, could anyone just tell me whether the checks being performed are correct? Do I need to modify the source rectangle(s) after creating one of the differences? Does this work for all overlapping rectangles, or would I need to determine which is inside which, or clip them so one is completely inside the other? :confused:
• 09-14-2005
dwks
Maybe you want else ifs instead of ifs?
• 09-14-2005
SMurf
Nope, because in each case I'm checking for the difference on each side of the rectangle being subtracted.

i.e. if the second rectangle was smaller and in the centre of the first rectangle, there would be a difference on the left side, the right side, the top and bottom sides, generating 4 rectangles.

But if the second rectangle was almost the same as the first rectangle except slightly narrower, it should only generate a rectangle for the difference on the right side.

So, there may be as few as no rectangles generated (the rectangles cancel each other out), or as many as 4.