I do not know Java, but the steps you could follow are as follows:
- Find the middle of each line of rectangles (should be easy, just avarage x1 + x2 and y1 + y2)
- , , , .
- , xa, ya ( , ), . , , .
- xb, yb ( ). , xa-xb ya-yb ( , ). ( ?), , , xa, ya xa, yb xa, ya xb, ya, , .
- , , . , . , , , .
, , , .