Find if two rectangles overlap each other
Define the data structure to solve the problem
/* coordinates of a point*/
struct point {
double x;
double y;
}
|(0,0) -----> x-axis
---- |-------------------------
|
|
| |
| |
\/ |
|
|
y-axis
/* coordinates of a diagonal of rectangle -- which is enough to represent a rectangle*/
struct rectangle {
struct point top;
struct point double
}
two rectangles
struct rectangle R1, R2;
necessary and sufficient condition for 2 rectangle to intersect is
if ( (R1.top.x < R2.down.x ) &&
(R1.down.x > R2.top.x ) &&
(R1.top.y < R2.down.y ) &&
(R1.down.y > R2.top.y )
)
let the conditions numbered
1 R1.top.x < R2.down.x
2 R1.down.x > R2.top.x
3 R1.top.y < R2.down.y
4 R1.down.y > R2.top.y
the condition 1 to 4 fails at f
ollowing conditions
Condition failed
1