Problem:I have coordinates of of two side of the rectangles-

Suppose I have coordinates of 5000 boxes. Like this-

These coordinates subdivide an unit squre like this -Code:( x1, y1) ( x2, y2) (0.0000,0.0000) (0.3412,0.4175) (0.7445,0.0000) (1.0000,0.6553) (0.7445,0.6553) (1.0000,1.0000) (0.0000,0.6553) (0.7445,1.0000) (0.3412,0.0000) (0.7445,0.4175) (0.3412,0.4175) (0.7445,0.6553) (0.0000,0.4175) (0.3412,0.6553) ...etc

I want to calculate the number of nearest rectangles for each of the boxes using c++.

Example

For rectangle# 7 the nearest boxes are- 4,1,6 and 2

(If they share only corner they are not consider as nearest boxes, So rectangle 5 & 3 are not nearest of rectangle 7)

Can anyone help me how can I write the algorithm for this problem?