0

I think you know all the Rectangle.Intersects(Rectangle)-Method (returns boolean) in C# for example. Because I switched to plattform that doesn't has something like that, I try to make somwhink like that manually by creating my own Rectangle-Class. But I have really no idea of how to get if two rectangles intersect. Thanks for your help!

jalgames
  • 781
  • 4
  • 23

1 Answers1

1

Let the rectangles be defined by r1 = ((x11, y11), (x12, y12)) and r2 = ((x21, y21), (x22, y22)), then the problem can be solved in one dimension at a time:

The interval [x11, x12] must overlap [x21, x22]. The same goes for the y coordinates.

Overlapping intervals can be tested lime this:

x11 <= x21 < x12 or x21 < x12 <= x22

mzedeler
  • 4,177
  • 4
  • 28
  • 41
  • Just note that I haven't checked the above against really contrived examples. If you have a zero width (or height) rectangle, it'll fail. – mzedeler Apr 08 '13 at 11:01