且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算x / y网格上两个矩形之间的重叠?

更新时间:2023-02-01 23:08:56

First, compute the x and y range for each rectangle (because you have a torus geometry do it mod gridsize).

So, given your Rectangle-1, compute:

x1 = x = 0, x2 = x + w = 20
y1 = y = 0, y2 = y + h = 20

Same for Rectangle-2:

x3 = 495, x4 = 505 mod 500 = 5
y3 = 0,   y4 = 10

Create the x and y "regions" for each rectangle:

Reactangle-1: x-regions: (0, 20)
              y-regions: (0, 20)

Rectangle-2:  x-regions: (495, 500), (0, 5)
              y-regions: (0, 10)

If any (both) x and y regions between the two rectangles have a non-null intersection, then your rectangles overlap. Here the (0, 20) x-region of Rectangle-1 and the (0, 5) x-region of Rectangle-2 have a non-null intersection and so do the (0, 20) and (0, 10) y-regions.