Thread: Difference Of Rectangles

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Difference Of Rectangles

    Hello,

    I'm currently on my own window manager/system... just to see if I could do it, really.
    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?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Maybe you want else ifs instead of ifs?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 06:55 AM