Skip to main content
added 16 characters in body
Source Link
Chas Brown
  • 9.8k
  • 1
  • 14
  • 39

We find the placement of each of the the upper left (u,v) and lower right (x,y) of the 'Mask' rectangle in that grid.

We find the placement of each of the the upper left and lower right of the 'Mask' rectangle in that grid.

We find the placement of each of the the upper left (u,v) and lower right (x,y) of the 'Mask' rectangle in that grid.

added 1 character in body
Source Link
Chas Brown
  • 9.8k
  • 1
  • 14
  • 39

A function that takes two rectangles, each rect being a tuple of ((top,left,top), (bottom,right,bottom)); returns a list of the resulting rectangles.

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 just the empty listSource rect.

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, right and bottom of the SurceSource and Mask rectangles, respectively.

A function that takes two rectangles, each rect being a tuple of ((top,left), (bottom,right)); returns a list of the resulting rectangles.

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.

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.

A function that takes two rectangles, each rect being a tuple of ((left,top), (right,bottom)); returns a list of the resulting rectangles.

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 just the Source rect.

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, top, right and bottom of the Source and Mask rectangles, respectively.

added 1525 characters in body
Source Link
Chas Brown
  • 9.8k
  • 1
  • 14
  • 39

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).

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).

added 72 characters in body
Source Link
Chas Brown
  • 9.8k
  • 1
  • 14
  • 39
Loading
added 73 characters in body
Source Link
Chas Brown
  • 9.8k
  • 1
  • 14
  • 39
Loading
edited body
Source Link
Chas Brown
  • 9.8k
  • 1
  • 14
  • 39
Loading
Source Link
Chas Brown
  • 9.8k
  • 1
  • 14
  • 39
Loading