简体   繁体   中英

How to get overlapping rectangle coordinates

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

aaaaaaaa
aaaaccccbbbbb
aaaaccccbbbbb
aaaaccccbbbbb
    bbbbbbbbb
    bbbbbbbbb

I've seen lots of ideas on how to calculate the area of the inner rectangle ("c"), but how would I go about getting the actual top/left/bottom/right coordinates for it?

The X coordinates of the overlap area of two rectangles can be found according to the following logic.

To find the Y coordinates, substitute Y for X in the last of the four assumptions, as well as in all of the three cases.


Assumptions:

  • A and B are rectangles (with their sides aligned along the X and Y axes),

  • each of the rectangles is defined by two points ( x min / y min ) – ( x max / y max )

  • where x min < x max and y min < y max .

  • Ax min < Bx min


Case 1 — No overlap:

+--------+
|A       |    
|        |    +----+
|        |    |B   |
|        |    +----+
|        |
+--------+

Ax min < Ax max < Bx min < Bx max ⇒ No overlap.


Case 2 — Some overlap:

+--------+
|A       |
|     +--+-+
|     |B | |
|     +--+-+
|        |
+--------+

Ax min < Bx min < Ax max < Bx max ⇒ Overlap X coordinates: Bx minAx max


Case 3 — Complete overlap:

+--------+
|A       |
| +----+ |
| |B   | |
| +----+ |
|        |
+--------+

Ax min < Bx min < Bx max < Ax max ⇒ Overlap X coordinates: Bx minBx max


PS: You can actually further simplify this algorithm. The overlap X coordinates are always:

max( Ax min , Bx min ) – min( Ax max , Bx max )

except when the second value is less than the first; that means that there is no overlap.

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

Assume:

Points of   rectangle R1: R1.A(x,y), R1.B(x,y), R1.C(x,y), R1.D(x,y)   
Points of   rectangle R2: R2.A(x,y), R2.B(x,y), R2.C(x,y), R2.D(x,y)   
Overlapping rectangle RO: RO.A(x,y), RO.B(x,y), RO.C(x,y), RO.D(x,y)    
Standard cartesian coordinates (positive is right and upwards).

Overlapping rectangle RO computes as follows with C#:

RO.A.x = Math.Min(R1.A.x, R2.A.x);
RO.A.y = Math.Max(R1.A.y, R2.A.y);
RO.C.x = Math.Max(R1.C.x, R2.C.x);
RO.C.y = Math.Min(R1.C.y, R2.C.y);
RO.B(x,y) and RO.D(x,y) = ....

Inner rectangle RI:

Swap Min and Max in above solution for overlapping rectangle RO.

I used an abstract validator for my project and to check if some layout controls where overlapping I created rectangles out of the layout figures:

RuleFor(p => DoControlsIntersect(p.PageControls.Select(x => new Rectangle(x.Row, x.Column, x.Width, x.Height)).ToList())).Equal(false).WithMessage(OverlappingFields);

private bool DoControlsIntersect(List<Rectangle> rectangles)
        {
            return rectangles.Any(rect => rectangles.Where(r => !r.Equals(rect)).Any(r => r.IntersectsWith(rect)));
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM