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!
Asked
Active
Viewed 142 times
0
-
Are they axis-aligned rectangles? It's easy for axis-aligned rectangles. – John Dvorak Apr 07 '13 at 20:25
-
Yes. Only x, y, width and height – jalgames Apr 07 '13 at 20:28
-
Then it's easy. Have you tried searching? – John Dvorak Apr 07 '13 at 20:29
-
Yes. But I only found the information like what parameters the prebuild-methods use and so what. – jalgames Apr 07 '13 at 20:30
-
This should help: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/awt/Rectangle.java#Rectangle.intersection%28java.awt.Rectangle%29 referenced from here: http://stackoverflow.com/questions/13670167/how-to-find-intersection-rectanglepoints-of-instersecting-rectangles?rq=1 – John Dvorak Apr 07 '13 at 20:32
-
Thanks! I think i can change that to what I want. – jalgames Apr 07 '13 at 20:34
1 Answers
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