Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ron Savage (5224)

Ron Savage
  (email not shown publicly)

Journal of Ron Savage (5224)

Sunday June 08, 2008
10:25 PM

Non-graphical overlapping rectangles test

[ #36626 ]

I'm looking at the problem of drawing labels on a graph, e.g street names, which might overlap.

What I want, so I don't have to write it myself, is a module which takes 2 rectangles defined by their corners, and gives me a Boolean result as to whether or not the 2 rectangles overlap.

I don't want to be tied to any particular image processing software, e.g. Image::Magick, and I don't need the result to be the polygon defined by the overlap.

I can see an algorithm: Take the 2 end lines [*] of one rectangle, determine their equations, and see if they intersect any of the sides of the other rectangle. Then reverse the roles of the 2 rectangles to check the opposite case.

[*] For 2 lines meeting at the corner, it doesn't matter which of the 2 lines is used. So 2 bounding lines of a rectangle which don't meet ought to be sufficient.

Has this been done?

Are there any modules which do part of it?

I searched CPAN for Math::*, but did not see anything obviously relevant.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • If I understand your problem description correctly:

    This is a pretty common problem typical of interactive 2d applications (a.k.a video games).

    A good algorithm is really straightforward.

    Search for AABB (Axis-aligned bounding box) and OBB (Oriented bounding box) collision tests.

    Depending on the number of tests you need to do, speed may or may not be an issue. You can speed-up the solve time by testing in the following order:

    1. Do two BSs (bounding sphere) overlap?
    2. Now, do the AABBs overlap?
    3. Finally, do th
    • $many x $thanx;

      Your comments gave me much to search for and read about.

      Looks like the AABB is painless and the OBB is painful.
  • The rectangles _don't_ overlap when one is either completely to the left of the other, or completely below the other.

    A rectangle can be defined by the coordinates of its top-left and bottom-right points:

    So let's say A = (x1,y1,x2,y2) and B = (u1,v1,u2,v2).

    (x1,y1)                 (u1,v1)
      +-------------+           +-----------+
      |             |           |