How to get overlapping rectangular coordinates

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

aaaaaaaa aaaaccccbbbbb aaaaccccbbbbb aaaaccccbbbbb bbbbbbbbb bbbbbbbbb 

I saw a lot of ideas on how to calculate the area of ​​the inner rectangle ("c"), but how could I get the actual coordinates of the top / left / bottom / right for it?

+7
c #
source share
5 answers
+13
source share

The X-coordinates of the overlapping area of ​​the two rectangles can be found in accordance with the following logic.

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


Assumptions:

  • A and B are rectangles (with 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 <sub> minsub>


Case 1 - No match:

 +--------+ |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 β†’ Override X coordinates: Bx min - Ax <sub> tahsub>


Case 3 - Full match:

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

Ax min <Bx min <Bx max <Ax max β†’ Override X coordinates: Bx min - Bx <sub> tahsub>


PS:. You can simplify this algorithm even further. X-coordinate always:

max (Ax min , Bx min ) - min (Ax max , Bx max )

unless the second value is less than the first; this means that there is no overlap.

+9
source share
 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)); } 
+4
source share

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). 

The overlapping rectangle RO computes as follows using C #:

 RO.Ax = Math.Min(R1.Ax, R2.Ax); RO.Ay = Math.Max(R1.Ay, R2.Ay); RO.Cx = Math.Max(R1.Cx, R2.Cx); RO.Cy = Math.Min(R1.Cy, R2.Cy); RO.B(x,y) and RO.D(x,y) = .... 

Inner RI Rectangle:

Change Min and Max in the above solution to overlap the RO rectangle.

+1
source share

I used an abstract validator for my project and to check if there were any layout controls where they overlap, I created rectangles from the drawings:

 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))); } 
0
source share

All Articles