Thread: Rectangle List Craziness

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

    Red face Rectangle List Craziness

    Hello,

    I'm currently trying to write my own GUI, or more specifically, a window manager. Talk about leaping into the deep end...

    Anyway, it's coming along, even added child window support last week, but there's something I can never get my head around: the proper administration of window update regions. If anyone can point out a good reference for making these things, I'd be most grateful.

    To the problem at hand, then. I think that something in the rectangle list (region) functions below isn't right and I can't put my finger on it.
    Code:
    // first off, behold!  A region!
    typedef struct TAGregion {
        SDL_Rect rcRegion; // Using an SDL Rect for fast passing to SDL blit function
        short sRight; // These ease calculations a bit
        short sBottom;
        struct TAGregion *next;
    } t_region;
    
    // Find the difference (A - B) of two rectangles
    t_region *difference(short sX, short sY, short sRight, short sBottom, short sX1, short sY1, short sRight1, short sBottom1)
    {
        t_region *regions = NULL;
    
        if (sX < sX1)
        {
            create_rect_region(&regions, sX, sY, sX1, sBottom);
            sX += sX1 - sX;
        }
    
        if (sY < sY1)
        {
            create_rect_region(&regions, sX, sY, sRight, sY1);
            sY += sY1 - sY;
        }
    
        if (sRight > sRight1)
        {
            create_rect_region(&regions, (sX > sRight1) ? sX : sRight1, sY, sRight, sBottom);
            sRight -= sRight - sRight1;
        }
    
        if (sBottom > sBottom1)
            create_rect_region(&regions, sX, (sY > sBottom1) ? sY : sBottom1, sRight, sBottom);
    
        return regions;
    }
    
    // find the difference of two lists (regions)
    t_region *difference_regions(t_region *region1, t_region *region2)
    {
        t_region *rgn2, *rgnRet = NULL;
    
        while (region1)
        {
            rgn2 = region2;
            while (rgn2)
            {
                rgnRet = append_regions(rgnRet, difference(region1->rcRegion.x, region1->rcRegion.y, region1->sRight, region1->sBottom, rgn2->rcRegion.x, rgn2->rcRegion.y, rgn2->sRight, rgn2->sBottom));
                rgn2 = rgn2->next;
            }
    
            region1 = region1->next;
        }
    
        return rgnRet;
    }
    
    // find the intersection (A & B) of two rectangles
    t_region *intersection(short sX, short sY, short sRight, short sBottom, short sX1, short sY1, short sRight1, short sBottom1)
    {
        if ((sX > sX1) ? sX : sX1 >= (sRight < sRight1) ? sRight : sRight1 || (sY > sY1) ? sY : sY1 >= (sBottom < sBottom1) ? sBottom : sBottom1)
            return NULL;
        else
            return create_rect_region(NULL, (sX > sX1) ? sX : sX1, (sY > sY1) ? sY : sY1, (sRight < sRight1) ? sRight : sRight1, (sBottom < sBottom1) ? sBottom : sBottom1);
    
    }
    
    // find the intersection of two regions
    t_region *intersection_regions(t_region *region1, t_region *region2)
    {
        t_region *rgn2, *rgnRet = NULL;
    
        while (region1)
        {
            rgn2 = region2;
            while (rgn2)
            {
                rgnRet = append_regions(rgnRet, intersection(region1->rcRegion.x, region1->rcRegion.y, region1->sRight, region1->sBottom, rgn2->rcRegion.x, rgn2->rcRegion.y, rgn2->sRight, rgn2->sBottom));
                rgn2 = rgn2->next;
            }
    
            region1 = region1->next;
        }
    
        return rgnRet;
    }

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Never mind, I needed parentheses around all the conditionals in intersection(). Made sense in my head...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM