Suppose I have a set of (X,Y) coordinates of 1000 boxes.
( x1, y1) ( x2, y2) Area
(0.0000,0.0000) (0.3412,0.4175)
Let's say you're interested in box B for which the (x0, y0, x1, y1) coordinates are: (B.x0, B.y0, B.x1, B.y1)
Then with:
ax0 = min(x0,x1);
ax1 = max(x0,x1);
ay0 = min(y0,y1);
ay1 = max(y0,y1);
bx0 = min(B.x0,B.x1);
bx1 = max(B.x0,B.x1);
by0 = min(B.y0,B.y1);
by1 = max(B.y0,B.y1);
you go through the whole list of boxes (with a for loop) and you select the ones for which:
(((ay0 <= by1 && ay1 >= by0) && (ax1 == bx0 || ax0 == bx1) || // touch on x axis
((ax0 <= bx1 && ax1 >= bx0) && (ay1 == by0 || ay0 == by1)) // touch on y axis