Determine If Two Rectangles Overlap

Given two axis-aligned rectangles A and B. Write a function to determine if the two rectangles overlap.

Idea:

1. Think of the opposite statement: When they will not overlap.

2. When they do not overlap. The x range will not overlap or y range will not overlap.

C++:
01 bool rect_intersect(int A[][2], int B[][2])
02 {
03     if(A[0][0]>B[0][1])
04         return false;
05     if(A[0][1]<B[0][0])
06         return false;
07     if(A[1][0]>B[1][1])
08         return false;
09     if(A[1][1]<B[1][0])
10         return false;
11     return true;
12 }
13 
14 
15 void creat_rec(int A[][2])
16 {
17     cout<<“Plase enter the x_left”<<endl;
18     cin>>A[0][0];
19     cout<<“Plase enter the x_right”<<endl;
20     cin>>A[0][1];
21     cout<<“Plase enter the y_low”<<endl;
22     cin>>A[1][0];
23     cout<<“Plase enter the y_high”<<endl;
24     cin>>A[1][1];
25 }
Advertisements
This entry was posted in Geometry. 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