[英]In iText 7 - a paragraph and a rectangle with same coordinates are not right overlapping
[英]How to get overlapping rectangle coordinates
假设我有以下重叠的矩形(“ a”和“ b”):
aaaaaaaa
aaaaccccbbbbb
aaaaccccbbbbb
aaaaccccbbbbb
bbbbbbbbb
bbbbbbbbb
我已经看到了许多有关如何计算内部矩形(“ c”)的面积的想法,但是我将如何获取实际的顶部/左侧/底部/右侧坐标?
可以根据以下逻辑找到两个矩形的重叠区域的X坐标。
要找到Y坐标,请在四个假设的最后一个以及所有三种情况下用Y替换X。
假设:
A和B是矩形(其边沿X和Y轴对齐),
每个矩形都由两个点( x min / y min )–( x max / y max )定义
其中x min < x max和y min < y max 。
Ax 分钟 < Bx 分钟
情况1-无重叠:
+--------+
|A |
| | +----+
| | |B |
| | +----+
| |
+--------+
Ax 最小值 < Ax 最大值 < Bx 最小值 < Bx 最大值 ⇒没有重叠。
情况2-一些重叠:
+--------+
|A |
| +--+-+
| |B | |
| +--+-+
| |
+--------+
斧分钟 <Bx的分 < 斧MAX <Bx的最大值 ⇒重叠X坐标:Bx的最小 - 最大斧
情况3-完全重叠:
+--------+
|A |
| +----+ |
| |B | |
| +----+ |
| |
+--------+
Ax 最小值 < Bx 最小值 < Bx 最大值 < Ax 最大值 ⇒重叠X坐标: Bx 最小值 – Bx 最大值
PS:您实际上可以进一步简化此算法。 重叠的X坐标始终为:
max( Ax min , Bx min )– min( Ax max , Bx max )
除非第二个值小于第一个; 这意味着没有重叠。
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));
}
假设:
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).
重叠的矩形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) = ....
内矩形RI:
在上面的解决方案中将Min和Max交换为重叠的矩形RO。
我为我的项目使用了一个抽象验证器,并检查是否有一些布局控件在重叠的地方从布局图中创建了矩形:
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.