如何获取重叠矩形的坐标

7
假设我有以下重叠的矩形("a"和"b"):
aaaaaaaa
aaaaccccbbbbb
aaaaccccbbbbb
aaaaccccbbbbb
    bbbbbbbbb
    bbbbbbbbb

我看到了很多有关如何计算内矩形(“c”)面积的想法,但是我该如何获取其实际的顶部/左侧/底部/右侧坐标?

5个回答

13

真不敢相信它竟然这么简单。谢谢! - Chris

9
两个矩形重叠区域的 X 坐标可以根据以下逻辑找到。
要找到 Y 坐标,将第四个假设中的 X 替换为 Y,并在所有三种情况中也替换 X 为 Y。
假设:
A 和 B 是矩形(其边沿沿着 X 和 Y 轴对齐)。
每个矩形由两个点定义 (x_min / y_min) - (x_max / y_max),其中 x_min < x_max,y_min < y_max。
A.x_min < B.x_min。
Case 1 — 无重叠部分:
+--------+
|A       |    
|        |    +----+
|        |    |B   |
|        |    +----+
|        |
+--------+

A.xmin < B.xmin < A.xmax < B.xmax   ⇒   Overlap.

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

A.xmin < B.xmin < A.xmax < B.xmax   ⇒   重叠的X坐标: B.xminA.xmax


情况3 - 完全重叠:

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

A.xmin < B.xmin < B.xmax < A.xmax  ⇒   重叠的X坐标: B.xminB.xmax


附言: 实际上,您可以进一步简化此算法。 重叠的X坐标始终为:

max(A.xmin, B.xmin) – min(A.xmax, B.xmax)

除非第二个值小于第一个值; 这意味着没有重叠。


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

只要坐标系将右方和向下定义为正方向。 - Pontus Gagge
@ChrisW:检查它们是否相交:在所有不不相交的情况下,它们相交。如果StartA>EndB(完全在B之后)和EndA<StartB(完全在B之前)都不成立,则它们不相交。现在使用德摩根定理:Not(A或B)<=> Not A And Not B ==>(StartA <= EndB)and(EndA >= StartB),在任何其他情况下,返回(0,0,0,0)矩形或NULL。 - Stefan Steiger

1

假设:

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

使用C#计算重叠矩形RO的方法如下:

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

内部矩形RI:

对于重叠的矩形RO,在上述解决方案中交换最小值和最大值。


0
我在项目中使用了一个抽象验证器,并且为了检查一些布局控件是否重叠,我将布局图形转换成了矩形:
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)));
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接