Assume I have the following overlapping rectangles (\"a\" and \"b\"):
aaaaaaaa
aaaaccccbbbbb
aaaaccccbbbbb
aaaaccccbbbbb
bbbbbbbbb
bbbbbbbbb
static internal Rectangle intersect(Rectangle lhs, Rectangle rhs)
{
Dimension lhsLeft = lhs.Location.X;
Dimension rhsLeft = rhs.Location.X;
Dimension lhsTop = lhs.Location.Y;
Dimension rhsTop = rhs.Location.Y;
Dimension lhsRight = lhs.Right;
Dimension rhsRight = rhs.Right;
Dimension lhsBottom = lhs.Bottom;
Dimension rhsBottom = rhs.Bottom;
Dimension left = Dimension.max(lhsLeft, rhsLeft);
Dimension top = Dimension.max(lhsTop, rhsTop);
Dimension right = Dimension.min(lhsRight, rhsRight);
Dimension bottom = Dimension.min(lhsBottom, rhsBottom);
Point location = new Point(left, top);
Dimension width = (right > left) ? (right - left) : new Dimension(0);
Dimension height = (bottom > top) ? (bottom - top) : new Dimension(0);
return new Rectangle(location, new Size(width, height));
}