How to get overlapping rectangle coordinates

后端 未结 5 1343

Assume I have the following overlapping rectangles (\"a\" and \"b\"):

aaaaaaaa
aaaaccccbbbbb
aaaaccccbbbbb
aaaaccccbbbbb
    bbbbbbbbb
    bbbbbbbbb
5条回答
  •  日久生厌
    2021-02-09 04:57

    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));
    }
    

提交回复
热议问题