Algorithm to determine closest Rectangles Maybe this question belongs to StackOverflow but maybe someone here has a good idea to solve or optimize my approach to following problem:
Let's say we've a set $S$ of rectangles in Euclidean Coordinates. Those rectangles are form some sort of clusters hence, some are quite close to one another while others are not. The task is to group the rectangles which are close to one another. 
The problem is that I'm not able to determine something like a general distance to identify if a rectangle belongs to a cluster or not.
My approach so far is as follows: 
I introduce a new set $C$ and a initial distance $d$ and iterate over each element in $S$. If a the an element $s_i$ of $S$ is in the distance of $c_j$ element $C$, using the centers of $s_i$ and $c_j$  to calculate the Euclidean distance between this vectors, I adjust $c_j$ to a combination of both rectangles. If not, $s_i$ becomes a new element of $C$. As this progresses, i adjust the distance $d$ by multiplication with the term $(1-(1/avg(c_j))$ where $avg(c_j)$ is the average distance between all merged rectangles of $c_j$.
This works quite well in some cases but it's heavily depending on the initial distance. Any help from a mathematical perspective would be appreciated.
Edit:
The rectangles are always parallel to the x- and y-axis and of different sizes. They don't necessarily overlap to belong to a cluster. It's sometimes the case but they are often separated by some space.
 A: First, you might want to define your rectangle objects as having the following five characteristics:

*

*index

*$x$ coordinate of lower left vertex

*$\Delta x$ as width of rectangle

*$y$ as $y$ coordinate of lower left vertex

*$\Delta y$ as height of rectangle

*Cluster number (initially = index)

Decide in advance that two rectangles within a distance $\epsilon$ of each other belong to the same cluster. Then augment each rectangle as in the diagram, adding a border region of width $\epsilon/2$ to all four sides. This is accomplished by taking each $x,\Delta x,y,\Delta y$ for each of the original rectangles and replacing them as follows:
$$x\to x-\frac{\epsilon}{2},\,\Delta x\to\Delta x+\epsilon,\,y\to y-\frac{\epsilon}{2},\,\Delta y\to\Delta y+\epsilon$$
Use these as the augmented vertices for each rectangle.

Assuming $N$ rectangles,
Initially, assign each rectangle $R_i$ to it's own individual cluster $C_i$. Then
for $1\le i<j\le N$
if $x_j\le x_i+\Delta x_i\le x_j+\Delta x_j$
and
if $y_j\le x_i+\Delta y_i\le x_j+\Delta y_j$
then assign $x_j$ to $C_i$ and mark cluster $C_j$ as eliminated.
Remember, these are the augmented rectangles. Making the value of $\epsilon$ larger will result in the merger of more clusters. Making $\epsilon$ smaller will break up some clusters into smaller clusters.
