The basic strategy:
| |
0,0 | 1,0 | 2,0
-----A-----+-----
| |
0,1 | 1,1 | 2,1
-----+-----B-----
| |
0,2 | 1,2 | 2,2
| |
In the above diagram, the points A and B are the upper left and lower right, respectively, of the 'Source' rectangle (the first rect).
We find the placement of each of the the upper left and lower right of the 'Mask' rectangle in that grid.
If both these points are in the first or last column; or first or last row; then there is no overlap; and we can return the empty list.
Otherwise, there are 16 cases remaining; for example, the OP's first example is the case we can label (1,1),(2,2). Each case can be mapped to a set of resulting rectangles whose corners are always coordinates with horizontal values in either Source rectangles left,right, or the Mask rectangles left,right; and similarly for the vertical values, the Source's top,bottom or the masks.
For example, for the (1,1),(2,2) case, the rectangles would be ((l,t),(T,r)) and ((l,T),(R,b)), where l,t,r,b and L,T,R,B are the left, right, top and bottom of the Surce and Mask rectangles, respectively.
So we can create a lookup table that maps the coordinates to the set of all such possible combinations (which is what the product(product(*zip(*))) bit is about) to a set of rectangles that should be provided for each of the cases (which, after some golfy-decompression, is what the rest of the list stuff is about).