Overlapping of two rectangles.

Observation:

The two rectangles do not overlap when K A’s UL’s x value is greater than B’s LR’s x value or K A’s UL’s y value is less than B’s LR’s y value or K A’s LR’s x value is less than B’s UL’s x value or K A’s LR’s y value is greater than B’s UL’s y value.
This solution is much simpler, requiring only four comparisons and one negation. You can implement the function as follows:

Implementation:

boolean overlap( Rect a, Rect b)
return !( a.ul.x > b.lr.x ||
a.ul.y < b.lr.y ||
a.lr.x < b.ul.x ||
a.lr.y > b.ul.y
} ;

Another Easier way to represent same expression:


boolean rectangleOverlap(P1,P2,P3,P4)
{
return ! ( P2.y < P3.y || P1.y < P4.y || P2.x P4.x );
}

Source: http://www.ihas1337code.com/2011/05/determine-if-two-rectangles-overlap.html

Advertisements

About Jeet

I am a Master’s student with Computer Science as major, in Stony Brook University, NY. I am interested in Systems and Security. In my free time, I watch movies, read novels, write blogs, journal and photography. Mysteries attract me and perhaps this is the reason, I am fascinated with System Security most. I am also interested in Astrology and all forms of prophecy.
This entry was posted in Algorithm Brews... Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s