Interesting question. Suppose we've four rectangle coordinates as shown below:
require(data.table) # v1.9.7+
dt = data.table(x0 = c(7,17,5,1), x1 = c(14,27,11,10),
y0 = c(1,55,6,14), y1 = c(10,70,20,34))
dt[, id := .I] # add row numbers
# x0 x1 y0 y1 id
# 1: 7 14 1 10 1
# 2: 17 27 55 70 2
# 3: 5 11 6 20 3
# 4: 1 10 14 34 4
To see if rectangle i intersects with j, the condition, IIUC is:
x0_i <= x1_j, x1_i >= x0_j, y0_i <= y1_j, y1_i >= y0_j, where
x0_i < x1_i, x0_j < x1_j, y0_i < y1_i, y0_j < y1_j
This can be achieved with a conditional join, which was recently implemented in the current development version of data.table, v1.9.7. See installation instructions here.
ans = dt[dt,
.(id1 = pmin(i.id, x.id), id2 = pmax(i.id, x.id),
x0 = pmax(x.x0, i.x0), x1 = pmin(x.x1, i.x1),
y0 = pmax(x.y0, i.y0), y1 = pmin(x.y1, i.y1)),
on = .(x0 <= x1, x1 >= x0, y0 <= y1, y1 >= y0)
][id1 != id2]
ans = unique(ans)
# id1 id2 x0 x1 y0 y1
# 1: 1 3 7 11 6 10
# 2: 3 4 5 10 14 20
Basically this performs a self-join. For each row in dt, it finds all matching indices with itself based on the condition provided to the on= argument, and based on the matching rows it extracts indices of the two rectangles and their intersecting portions. The id1 != id2 filters out self-overlaps..
Then we make sure that are no duplicates. The result shows which rectangle id1 overlaps with which ones id2, and their intersecting region x0, x1, y0, y1.
Is this more or less what you were looking for?