Rectangle Intersection Testing Algorithm


Given two rectangles tex_ff8682d12b39a6dfc9481028678d3b46 Rectangle Intersection Testing Algorithm algorithms delphi geometry math (as shown in the following figure), tex_349c91ef52f3e1edb8cf717e280c6482 Rectangle Intersection Testing Algorithm algorithms delphi geometry math , tex_9fd1f8f2e3cd24ed8f0dd49e1b84ac82 Rectangle Intersection Testing Algorithm algorithms delphi geometry math are lower-left points while tex_fa1dfbcca9978963be1c4f0b4e1131e2 Rectangle Intersection Testing Algorithm algorithms delphi geometry math and tex_ff366739d742e64177da5ce165764fe5 Rectangle Intersection Testing Algorithm algorithms delphi geometry math are top-right points. We want to find out whether these two rectangles overlap each other.

box Rectangle Intersection Testing Algorithm algorithms delphi geometry math

The solution is simple: instead of checking the situations these two collide, we can check the opposite. There are four situations that these two rectangles do not overlap each other.

  • P2x > P1x
  • P3x < P0x
  • P2y > P1y
  • P3y < P0y

They check the following respectively.

  • The blue box is on the right of the red box.
  • The blue box is on the left of the red box.
  • The blue box is on the top of the red box.
  • The blue box is on the bottom of the red box.

The boolean NOT should give the correct test on whether these two collide. The following Delphi code tests for box intersection algorithm.

// type definition
type _2DP = array [1 .. 2] of Single;

function v2DPIntersect(
  const lowerleft0: _2DP; 
  const topright0: _2DP; 
  const lowerleft1: _2DP; 
  const topright1: _2DP
): boolean; inline;
begin
  Result := (not
           ((topright1[1] < lowerleft0[1]) or
           (lowerleft1[1] > topright0[1]) or
           (topright1[2] < lowerleft0[2]) or
           (lowerleft1[2] > topright0[2])));
end;

Based on this, we can futher compute the intesection area (rectangle). The following four coordinates are computed.

  1. tex_c382181a6d80c5878403fe3a4cd9a9ff Rectangle Intersection Testing Algorithm algorithms delphi geometry math
  2. tex_6a1111dfaded28ea93d9e401e319d749 Rectangle Intersection Testing Algorithm algorithms delphi geometry math
  3. tex_fd79bb7217ca198d27d6ef931bec47f8 Rectangle Intersection Testing Algorithm algorithms delphi geometry math
  4. tex_ccf460fa741b25ae92f29aba20297a95 Rectangle Intersection Testing Algorithm algorithms delphi geometry math

The intersection area is thus represented by (lower-left to upper-right rectangle) (x0, y0) ~ (x1, y1).

You may refer to this post: Two Rectangles Overlap Detection Algorithm in C++ if you want to perform the overlapping tests for two rectangles.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
688 words
Last Post: Remainder Computation (Modulo) on Floating Numbers, Delphi and Python
Next Post: Disable Buggy Compiler Warnings Undefined Function Return for Delphi 2007

The Permanent URL is: Rectangle Intersection Testing Algorithm

Leave a Reply