简体   繁体   English

如何获得重叠的矩形坐标

[英]How to get overlapping rectangle coordinates

Assume I have the following overlapping rectangles ("a" and "b"): 假设我有以下重叠的矩形(“ a”和“ 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? 我已经看到了许多有关如何计算内部矩形(“ c”)的面积的想法,但是我将如何获取实际的顶部/左侧/底部/右侧坐标?

The X coordinates of the overlap area of two rectangles can be found according to the following logic. 可以根据以下逻辑找到两个矩形的重叠区域的X坐标。

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. 要找到Y坐标,请在四个假设的最后一个以及所有三种情况下用Y替换X。


Assumptions: 假设:

  • A and B are rectangles (with their sides aligned along the X and Y axes), AB是矩形(其边沿X和Y轴对齐),

  • each of the rectangles is defined by two points ( x min / y min ) – ( x max / y max ) 每个矩形都由两个点( x min / y min )–( x max / y max )定义

  • where x min < x max and y min < y max . 其中x min < x maxy min < y max

  • Ax min < Bx min Ax 分钟 < Bx 分钟


Case 1 — No overlap: 情况1-无重叠:

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

Ax min < Ax max < Bx min < Bx max ⇒ No overlap. Ax 最小值 < Ax 最大值 < Bx 最小值 < Bx 最大值 ⇒没有重叠。


Case 2 — Some overlap: 情况2-一些重叠:

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

Ax min < Bx min < Ax max < Bx max ⇒ Overlap X coordinates: Bx minAx max 分钟 <Bx的 < MAX <Bx的最大值 ⇒重叠X坐标:Bx的最小 - 最大


Case 3 — Complete overlap: 情况3-完全重叠:

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

Ax min < Bx min < Bx max < Ax max ⇒ Overlap X coordinates: Bx minBx max Ax 最小值 < Bx 最小值 < Bx 最大值 < Ax 最大值 ⇒重叠X坐标: Bx 最小值Bx 最大值


PS: You can actually further simplify this algorithm. PS:您实际上可以进一步简化此算法。 The overlap X coordinates are always: 重叠的X坐标始终为:

max( Ax min , Bx min ) – min( Ax max , Bx max ) max( Ax minBx min )– min( Ax maxBx 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使用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: 内矩形RI:

Swap Min and Max in above solution for overlapping rectangle RO. 在上面的解决方案中将Min和Max交换为重叠的矩形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)));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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