Jun 23, 2009

Coding: Write code to compute the intersection of 2 rectangles (x direction is to the right, y direction is to the down)

Coding: Write code to compute the intersection of 2 rectangles (x direction is to the right, y direction is to the down)
Assume the rectangles are represented by
struct Rectangle{
int x1;
int y1;
int x2;
int y2;
};
Rectangle Intersection(Rectangle A, Rectangle B){
Rectangle C;
C.x1 = max(A.x1, B.x1);
C.y1 = max(A.y1, B.y1);
C.x2 = min(A.x2, B.x2);
C.y2 = min(A.y2, B.y2);
if(C.x2 > C.x1 && C.y2 > C.y1){
return C;
}
else{
return NULL;
}
}

No comments:

Post a Comment