Rectangles are located in a doubly-linked list. In this context, a rectangle is a figure, formed using a data of the rectangle's structure (no matter how).
struct rect {
int l; /* left */
int t; /* top */
int r; /* right */
int b; /* bottom */
};
Its fields has the strict positions (I guarantee it for any rectangle).
[l; t] [r; t]
+------+
| |
| |
+------+
[l; b] [r; b]
Rectangles are stored in objects.
struct object {
struct rect *rect;
bool selected;
};
And objects are stored in the list's nodes.
struct node {
struct object *obj;
struct node *next;
struct node *prev;
};
Rectangles draws consistently, beginning from the list's head -- first draws a rectangle, closer to the beginning of the list. Thereby, an each next figure overlaps previous figures.
I select rectangles, with a selection rectangle, that forms by mouse cursor. The selection check is done, during iterating over the list from the end.
struct node *node = getend(list);
struct object *obj;
do {
obj = node->obj;
/* Check, if a selection rectangle somehow
* intersects with a rectangle of the current
* object. */
if (rcheck(sel_rect, obj->rect))
obj->selected = true;
}
while ((node = node->prev));
Take a look to the demonstration of my problem, a little below.
As you can see, the selection rectangle selects two rectangles: yellow and green. In this case, only the yellow figure should be selected; in general -- if behind a forward rectangle another figure is located (a backward rectangle), and a selection rectangle does not cover a "visible" part of this figure (on the animation it's the green polygon, formed by overlapping the yellow figure), but covers its "hidden" part, then a backward rectangle should not be selected.
A forward rectangle means, that it's located closer to the end of the list; a backward rectangle - that it's located closer to its beginning.
This alrogithm is used in game's two-dimensional map editor, for rectangular textures selection.

for (struct node *node = getend(list); node; node = node->prev) { ... }. As coded with ado/whileloop, you cannot handle an empty list.top, left, bottom, rightproduces simpler code thantop, left, width, height.