If we have

then it is a surprisingly inexpensive task to determine, if the two main rectangles can be joined to a polygon with a common area (the overlaid subrectangles), such eliminating the space for one of the two subrectangles.

This article

The discussion involves knowledge of *Karnaugh maps* (a special kind of decision tables).
If you want to understand the technique and not just get the result,
and if you are unfamiliar with Karnaugh maps, then it is suggested that you first
follow the links given in the section *Further Reading* to the left.

Assume you have two identical subrectangles, each within a distinct main rectangle. Identical means, that the two subrectangles have identical content and identical dimensions. The horizontal and vertical offsets may be anywhere within the corresponding main rectangles and do not need to be identical as well.

Let's visualize the problem with an example. We have a yellow rectangle with red outlines along its edges, and we have a cyan rectangle with blue outlines. They both share an identical subrectangle, shown in magenta.

In fig. 1 it is obvious, that the identical subrecatangles of the two main rectangles can be overlapped to form a polygon as shown in fig.2.

It also should be obvious, that the two main rectangles in fig. 3 can *not* be merged
into a polygon with a shared area (fig. 4).

The problem is, that the yellow and cyan areas are not identical (the areas marked with the double-ended arrow), and therefore must be kept apart. (If these areas were identical, they should have been included in their magenta subrectangles.)

You might say: "So, what's the deal? Simply fuse all those rectangles having two opposite corners, and done."

Unfortunately things are more complicated. It turns out, that there are quite many combinations,
of which some are not as obvious as the ones just shown. Quite the opposite, actually.
This will be

Let's dive into the details, after definining some terminology.

When we refer to the edges of a rectangle, we will call them
*Right*, *Bottom*, *Top*, and *Left*,
and abbreviate them with R, B, T, and L, resp. (fig. 5)

An edge *order* is important. Not a particular order, as the one shown in fig. 6,
but any determined order. In your particular project you might want to choose
a clockwise or counterclockwise direction.
In this article, however, whenever we are going to compare two rectangles,
we always will use the shown order RBTL for both rectangles,
from left to right in exactly this way.

Subrectangles can appear in virtually every position within a main rectangle, comprising none or some or all edges of the parental main rectangle:

No edges

1 edge

2 edges (corner)

2 edges (strip)

3 edges

4 edges

Because there are 4 edges in a rectangle,
of which each can either be included or not included in the subrectangle,
we have a total of _{4}=16

Fig. 7 shows all base types of these combinations; the others are obtained
by rotations. However, because we compare two rectangle/subrectangle-combinations with each other,
we even will have to deal with ^{4})^{2}=256

0°

90°

180°

270°

Let's have a closer look at these 256 cases.
It is obvious, that many of the rectangle/subrectangle-combinations
do include rotations (fig. 8).
This observation might tempt us to save some time
by reducing the set of 256 combinations so, that we find general cases
which include all possible rotations.

Of course, a rotation on a real rectangle/subrectangle-combination containing real data would manipulate the subrectangle's appearance such, that it would no longer be identical with the other rectangle's subrectangle. However, the idea would account for this by assuming that the subrectangle itself would not be rotated in any way.

The same is also true for reflections, as they can easily be reduced to rotations.However, to derive the final formula by means of a Karnaugh map, we will need
to exhaustively consider all possible cases, i.e., looking at all polygons
generatable by the 2×16 inputs.

*Inputs* are those 2 out of the 16 edge combinations which are involved.
*Polygons* are closed planar paths composed of a finite number
of sequential line segments. In our case the polygons have internal angles
being a multiple of 90°, so we more precisely should call them
"orthogonal polygons".

To represent all cases we use a table. The inputs are given as row and column headers. In the table, the rows represent the input for the first main rectangle. The first main rectangle is colored in cyan, and outlined in blue. Its subrectangle is colored blue. - Likewise do the columns represent the input for the second main rectangle, colored in yellow and outlined in red. Its subrectangle is colored red.

Note, that the rows and columns are labeled in the order defined above,
see fig. 6. If an edge is not involved, i.e. if a subrectangle
does not include that particular main rectangle's edge,
then this is marked with a "-" at the appropriate position. -
Also note, that the sequence in which the input rows and input columns appear
is not arbitrary: the reason for this order has to do with the *Gray Code*
and is explained below.

For each combination of the two input rectangles a resulting polygon is given, leaving the input colors unchanged with the exception of the subrectangular area which is shared by both input rectangles. This shared area is colored magenta.

In the table's cells, the size or shape of the input boxes may be modified to keep things somewhat space-efficient. However, keep in mind, that a magenta rectangle represents both the row header's blue rectangle as well as the column header's red rectangle. Likewise do the cyan and yellow areas within a cell correspond exactly to the cyan and yellow areas of that cell's row and column headers.

Let's have a look at the table now.

`----`

`---L`

`--TL`

`--T-`

`-BT-`

`-BTL`

`-B-L`

`-B--`

`RB--`

`RB-L`

`RBTL`

`RBT-`

`R-T-`

`R-TL`

`R--L`

`R---`

`----`

`---L`

`--TL`

`--T-`

`-BT-`

`-BTL`

`-B-L`

`-B--`

`RB--`

`RB-L`

`RBTL`

`RBT-`

`R-T-`

`R-TL`

`R--L`

`R---`

All cells displaying no entry have two inputs which can not be merged to a single polygon with a shared area.

Now table 1 is converted into an equivalent Karnaugh Map. The Karnaugh map essentially is a table exactly like the one in fig. 1, only that it consists completely of binary information.

In a Karnaugh map, we work with results of functions,
not with graphics, and these results are expressed in binary from as follows:
each cell will be marked with `1`

where a polygon resulted in table 1,
and `0`

otherwise.
However, for better legibility,
we omit the zeroes within the result cells and leave them blank:
this way, the human eye is better able to catch patterns.

Those readers famliar with Karnaugh maps should notice, that a blank cell does not imply "Don't care".

Similarly, the *input* as well must be coded such,
that we can represent the different input values as a binary variable,
each containing a *0* or `1`

.
We already coded the row and column headers binary, though:
either a particular edge was present, and then was marked with its initial letter
(for example with `T`

for the top edge),
or it was not present and then marked with a `-`

symbol.
Therefore our task is reduced to replace any letter by ones
and the `-`

with zeroes (and this time we better do not omit the zeroes). -
Each of these binary digits defines a variable,
namely a particular edge within the rectangle.
Hence we in total have 2×4 variables (4 edges for each of the two input rectangles):
4 variables head each row, and the other 4 each column.

`0000`

`0001`

`0011`

`0010`

`0110`

`0111`

`0101`

`0100`

`1100`

`1101`

`1111`

`1110`

`1010`

`1011`

`1001`

`1000`

`0000`

`1`

`0001`

`1`

`1`

`0011`

`1`

`1`

`1`

`1`

`0010`

`1`

`1`

`0110`

`1`

`1`

`1`

`1`

`0111`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`0101`

`1`

`1`

`1`

`1`

`0100`

`1`

`1`

`1100`

`1`

`1`

`1`

`1`

`1101`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1111`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1110`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1010`

`1`

`1`

`1`

`1`

`1011`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1001`

`1`

`1`

`1`

`1`

`1000`

`1`

`1`

It became apparent now that the rows and columns indeed are
by no means ordered as a binary sequence.
Howere, there *is* an order:
it follows the so-called
*one single position*.

Contrast this with "traditional" binary counting:
the transition from e.g. decimal 7 to 8
(binary `0111`

to `1000`

)
causes 4 bits to change at once.
Not so in a Gray sequence: you can easily verify in table 2,
that each element's successor changes in just 1 bit.
This property is required in Karnaugh maps.

Recall, that each position of the digits in the row and column headings
represent a variable
(`R`

, `B`

, `T`

, `L`

as per fig. 6)

We will need to refer to our variables in the following sections.
Because it would be quite cumbersome to always refer to
"the `T`

(or the 3rd digit) of the 2nd rectangle" all the time,
we are going to introduce a shorter notation now.

We redefine the digits in an unambigeous way and set
`A`

`R`

edge)
of the `B`

`B`

edge,
`C`

`T`

, and
`D`

`L`

.
Similarly, we rename the edges of the `E`

`F`

`G`

`H`

Now, if we reserve 4 rows and 4 columns to represent
each of the 2×4 variables
`A`

`D`

`E`

`H`

The header rows and header columns in which no entry appears is to be read
"variable is not set", i.e. `0`

.
When such an entry appears, e.g. `H`

`1`

.

In Karnaugh maps, it is common practice to display
half of the

`F`

`F`

`F`

`F`

`F`

`F`

`F`

`F`

`H`

`H`

`H`

`H`

`H`

`H`

`H`

`H`

`1`

`D`

`1`

`1`

`D`

`1`

`1`

`1`

`1`

`C`

`1`

`1`

`C`

`B`

`1`

`1`

`1`

`1`

`C`

`B`

`D`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`C`

`B`

`D`

`1`

`1`

`1`

`1`

`B`

`1`

`1`

`B`

`1`

`1`

`1`

`1`

`A`

`B`

`D`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`A`

`B`

`D`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`C`

`A`

`B`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`C`

`A`

`1`

`1`

`1`

`1`

`C`

`A`

`D`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`1`

`C`

`A`

`D`

`1`

`1`

`1`

`1`

`A`

`1`

`1`

`A`

`G`

`G`

`G`

`G`

`G`

`G`

`G`

`G`

`E`

`E`

`E`

`E`

`E`

`E`

`E`

`E`

Our first task is to identify the minterms
to use in the final expression.
(Find out more about

Note, that we use the following notation for Boolean operations:

`AND`

`OR`

(The symbol × is omitted when between terms to be `AND`

ed,
much like the arithmetic multiplication sign between variables;
e.g. `ABCD`

means `A×B×C×D`

.)

16 prime implicants can be found; they are given in table 5.

`1-1-1-1-`

`ABCD`

3`1-1-1--1`

`ABCH`

3`1-1--11-`

`ABGD`

3`1-1--1-1`

`ABGH`

3`1--11-1-`

`AFCD`

3`1--11--1`

`AFCH`

3`1--1-11-`

`AFGD`

3`1--1-1-1`

`AFGH`

3`-11-1-1-`

`EBCD`

3`-11-1--1`

`EBCH`

3`-11--11-`

`EBGD`

3`-11--1-1`

`EBGH`

3`-1-11-1-`

`EFCD`

3`-1-11--1`

`EFCH`

3`-1-1-11-`

`EFGD`

3`-1-1-1-1`

`EFGH`

3*Cost* is the number of involved boolean operations.
All found prime implicants require 3 `AND`

operations.
E.g. for the first prime implicant: `A AND B AND C AND D`

.

All terms are essentials, hence our first equation reads:

`X=ABCD+ABCH+ABGD+ABGH+AFCD+AFCH+AFGD+AFGH+EBCD+EBCH+EBGD+EBGH+EFCD+EFCH+EFGD+EFGH`

The total cost for `X`

therefore is:
16×3 `AND`

s + 15 `OR`

s = 63 boolean operations.

Some algebra will simplify this term. We don't even need to apply any "specials" of Boolean algebra.

This is the found equation which we want to simplify:

`X=ABCD+ABCH+ABGD+ABGH+AFCD+AFCH+AFGD+AFGH+EBCD+EBCH+EBGD+EBGH+EFCD+EFCH+EFGD+EFGH`

Factoring out the first two products of each term in above equation:

`X=AB(CD+CH+GD+GH)+AF(CD+CH+GD+GH)+EB(CD+CH+GD+GH)+EF(CD+CH+GD+GH)`

The terms in the 4 parantheses are identical, hence we have a binomial:

`X=(AB+AF+EB+EF)(CD+CH+GD+GH)`

Within the 2 parantheses we factor out their first factors:

`X=( A(B+F)+E(B+F) )( C(D+H)+G(D+H) )`

The inner two parantheses within each outer parathesis are identical, forming binomials once again:

`X=( (A+E)(B+F) )( (C+G)(D+H) )`

The outer parantheses can be removed, and the final equation reads:

`X=(A+E)(B+F)(C+G)(D+H)`

Total cost: 4 `OR`

s and 3 `AND`

s

Let's see if our equation holds.
A worksheet table is quickly set up to generate a truth table for the 256 possible values.
For easy access to the variables from the table body's cells,
`A`

to `D`

are written into separate columns,
and `E`

to `H`

into separate rows.

E.g. for *MS Excel*
(
`A1`

,
duplicate the first 6 rows and the first 5 columns as seen below.
Then the table's body starts in cell `F7`

.
Enter this formula into cell `F7`

:

`=IF(AND(OR($B7;F$2);OR($C7;F$3);OR($D7;F$4);OR($E7;F$5));"1";"")`

Copy it to all 256 cells of the cell body (i.e. a 16 × 16 grid from there).

These are the values you get.

If there exist two identical subrectangles `C1`

and `C2`

at an arbitrary position
within their parental rectangles A and B, resp.,
then the rectangles `A`

and `B`

can be joined
to form an orthogonal polygon with
a common subrectangle `C1=C2`

then and only then,
when the edges which the subrectangles share with their parental rectangles
include all 4 possible edges at the top, right, bottom and left at least once,
regardless of which rectangle provides which edge(s).

As per the date writing this article (Dec 20, 2006) I was not able to find an according theorem on the Internet. If there exists a such, I would be thankful for a comment or an e-mail in order to mention it. The same is true when you happen to come across another proof. In the meantime I suggest the text to the right.

It has been shown for all 256 possible cases, that the formula holds.
Now it's easy to see, why

On the other hand, the two rectangles in

The algorithm may formally be expressed like this:

Set `R:=1`

(`R`

will hold the final result)

For all 4 edges:

Set `X:=0`

if the subrectangle of the first main rectangle
does not include that particular edge of its main rectangle,
`X:=1`

otherwise

Set `Y:=0`

if the subrectangle of the second main rectangle
does not include that particular edge of its main rectangle,
`Y:=1`

otherwise

Perform the boolean operation `Z:=X OR Y`

Perform the boolean operation `R:=R AND Z`

If `R`

is `0`

, then the algorithm terminates and
no overlapping polygon is possible

If `R`

still is `1`

, repeat for the next edge.
If there is no more next edge, the algorithm terminates
and creation of a polygon with a shared area is possible

Note, however, that on a computer the loop of the formal algorithm is not needed, because we can make use of bit parallelism.

Assume, that we have the values of 4 edges
of the first rectangle `ABCD`

in a variable `X`

, coded in 4 bits;
and similarly those of the second rectangle `EFGH`

in a variable `Y`

.
The order of the edges does not matter, as long as both variables use the same order
and the 4 bits are in the same position.

`X`

`ABCD`

`Y`

`EFGH`

Then `X OR Y`

will perform all 4 Boolean `OR`

operations in one go.
Let's store the result in variable `Z`

and
name the 4 resulting bits `IJKL`

.

`Z := X OR Y`

`IJKL`

Now it is only left to find out, if any of the bits in `IJKL`

is `0`

.
If so, no overlapping area exists for the two inputs.
If all 4 bits are set to `1`

, though, then such an overlapping area exists.

And because the 4 bits form an unambigeous value when all set,
this can be found out with just one comparison.
E.g.: assume that the 4 bits all are in the least significant bits of a word
(bit values from LSB to MSB: `1+2+4+8`

). Then:

IF Z = 15 THEN Overlapping ELSE Not Overlapping FI

Of course, this algorithm does not tell you yet, *how* to merge
the original rectangles into a polygon combining the common area.
But it tells you, if it is possible `at all`

,
and this it does at a surprisingly low cost.