kimos wrote:

how to calculate the intersection of 2 rectangle

The easiest way to think about this problem conceptually is to first

come up with an algorithm for the 1D version, intersecting two line

segments, then generalize it.

Suppose we have a line from a to b and a line from c to d. There are

four cases for intersecting them:

a <= b < c <= d : a----b c----d

In this case the intersection is empty.

a <= b = c <= d : a----b/c----d

In this case the intersection is the single point b=c (the line segment

from b to b.)

a <= c <= b <= d : a---c===b---d

In this case the intersection is the line from c to b.

a <= c <= d <= b : a---c===d---b

In this case the intersection is the line from c to d.

If you simply apply these same four cases to the x and y coordinate

ranges of your rectangles separately, the result will be the x and y

coordinate ranges of the intersection of the rectangles.

--

Derrick Coetzee

I grant this newsgroup posting into the public domain. I disclaim all

express or implied warranty and all liability. I am not a professional.