CS19002 Programming and Data Structures Laboratory |
Spring 2010, Section 5 |

A rectangle with sides parallel to the *X*- and *Y*-axes is
specified by four real numbers *a*, *b*, *c*, and *d*.
Assume that *a* <= *c* and *b* <= *d*.
The four corners of the rectangle are (*a*,*b*) [bottom left],
(*c*,*b*) [bottom right], (*a*,*d*) [top left], and
(*c*,*d*) [top right].

Read the values *a*_{1}, *b*_{1},
*c*_{1}, and *d*_{1} from the user
for a rectangle *R*_{1}. Subsequently, read the values
*a*_{2}, *b*_{2}, *c*_{2}, and
*d*_{2} for another rectangle *R*_{2}.
Your task is to determine whether the two rectangles *R*_{1}
and *R*_{2} intersect.

To solve this problem, it suffices to check whether both the following conditions are satisfied:

- The interval [
*a*_{1},*c*_{1}] has an overlap with the interval [*a*_{2},*c*_{2}]. - The interval [
*b*_{1},*d*_{1}] has an overlap with the interval [*b*_{2},*d*_{2}].

Two real intervals *I* = [*s*,*t*] and
*J* = [*u*,*v*] operlap if and only if
*I* starts before *J* ends and *J* starts before *I* ends,
that is, if and only if *s* <= *v* and
*u* <= *t*.

Report the output of your program for the following four runs.

a1, b1, c1, d1 a2, b2, c2, d2 1, 2, 3, 4 2, 3, 4, 5 1, 2, 3, 4 3, 4, 5, 6 1, 2, 3, 4 4, 5, 6, 7 1, 2, 3, 4 1.5, 5, 2.5, 6

Lab Home | Submission Site | My Home |