从 Rectangle.Intersection 中找到剩余的外部矩形
Find remaining outer rectangles from Rectangle.Intersection
我有一个外矩形和一个内矩形。如何 return 切割相交矩形后剩余的所有矩形?
我创建了这个函数,return 一个不包括相交列表的列表。
private IEnumerable<Rectangle> GetExternalRectangles(Rectangle surface, Rectangle test)
{
var result = new List<Rectangle>();
if (!test.IntersectsWith(surface)) return new List<Rectangle> { surface };
#region Top and Bottom
if (test.Top>surface.Top && test.Bottom < surface.Bottom) // test inside surface vertically
{
result.Add(new Rectangle(surface.Location, new Size(surface.Width, test.Top - surface.Top)));
result.Add(new Rectangle(new Point(surface.Left,test.Bottom), new Size(surface.Width, surface.Bottom-test.Bottom)));
}
if (test.Top > surface.Top && test.Bottom > surface.Bottom) // test inside surface vertically, overflow bottom
{
result.Add(new Rectangle(surface.Location, new Size(surface.Width, test.Top - surface.Top)));
//result.Add(new Rectangle(new Point(surface.Left,test.Bottom), new Size(surface.Width, surface.Bottom-test.Bottom)));
}
if (test.Top < surface.Top && test.Bottom < surface.Bottom) // test inside surface vertically, overflow top
{
//result.Add(new Rectangle(surface.Location, new Size(surface.Width, test.Top - surface.Top)));
result.Add(new Rectangle(new Point(surface.Left, test.Bottom), new Size(surface.Width, surface.Bottom - test.Bottom)));
}
#endregion
#region Lateral
if (test.Left > surface.Left && test.Right < surface.Right) // test inside surface horizontally
{
result.Add(new Rectangle(new Point(surface.Left,Math.Max(surface.Top,test.Top)), new Size(test.Left-surface.Left, Math.Min(surface.Bottom, test.Bottom)- Math.Max(surface.Top, test.Top))));
result.Add(new Rectangle(new Point(test.Right, Math.Max(surface.Top, test.Top)), new Size(surface.Right - test.Right, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
}
if (test.Left > surface.Left && test.Right > surface.Right) // test inside surface horizontally, overflow right
{
result.Add(new Rectangle(new Point(surface.Left, Math.Max(surface.Top, test.Top)), new Size(test.Left - surface.Left, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
//result.Add(new Rectangle(new Point(test.Right, Math.Max(surface.Top, test.Top)), new Size(surface.Right - test.Right, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
}
if (test.Left < surface.Left && test.Right < surface.Right) // test inside surface horizontally, overflow left
{
//result.Add(new Rectangle(new Point(surface.Left, Math.Max(surface.Top, test.Top)), new Size(test.Left - surface.Left, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
result.Add(new Rectangle(new Point(test.Right, Math.Max(surface.Top, test.Top)), new Size(surface.Right - test.Right, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
}
#endregion
return result;
}
表示此内容的自然方式是使用 Region
class,例如:
var result = new Region(outer);
result.Exclude(inner);
如果你真的想要一个 Rectangle
结构的列表,你可以看看使用单位矩阵使用 GetRegionScans
转换为 RectangleF
s,然后将它们转换为 Rectangle
s 使用 Ceiling
或 Floor
.
我有一个外矩形和一个内矩形。如何 return 切割相交矩形后剩余的所有矩形?
我创建了这个函数,return 一个不包括相交列表的列表。
private IEnumerable<Rectangle> GetExternalRectangles(Rectangle surface, Rectangle test)
{
var result = new List<Rectangle>();
if (!test.IntersectsWith(surface)) return new List<Rectangle> { surface };
#region Top and Bottom
if (test.Top>surface.Top && test.Bottom < surface.Bottom) // test inside surface vertically
{
result.Add(new Rectangle(surface.Location, new Size(surface.Width, test.Top - surface.Top)));
result.Add(new Rectangle(new Point(surface.Left,test.Bottom), new Size(surface.Width, surface.Bottom-test.Bottom)));
}
if (test.Top > surface.Top && test.Bottom > surface.Bottom) // test inside surface vertically, overflow bottom
{
result.Add(new Rectangle(surface.Location, new Size(surface.Width, test.Top - surface.Top)));
//result.Add(new Rectangle(new Point(surface.Left,test.Bottom), new Size(surface.Width, surface.Bottom-test.Bottom)));
}
if (test.Top < surface.Top && test.Bottom < surface.Bottom) // test inside surface vertically, overflow top
{
//result.Add(new Rectangle(surface.Location, new Size(surface.Width, test.Top - surface.Top)));
result.Add(new Rectangle(new Point(surface.Left, test.Bottom), new Size(surface.Width, surface.Bottom - test.Bottom)));
}
#endregion
#region Lateral
if (test.Left > surface.Left && test.Right < surface.Right) // test inside surface horizontally
{
result.Add(new Rectangle(new Point(surface.Left,Math.Max(surface.Top,test.Top)), new Size(test.Left-surface.Left, Math.Min(surface.Bottom, test.Bottom)- Math.Max(surface.Top, test.Top))));
result.Add(new Rectangle(new Point(test.Right, Math.Max(surface.Top, test.Top)), new Size(surface.Right - test.Right, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
}
if (test.Left > surface.Left && test.Right > surface.Right) // test inside surface horizontally, overflow right
{
result.Add(new Rectangle(new Point(surface.Left, Math.Max(surface.Top, test.Top)), new Size(test.Left - surface.Left, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
//result.Add(new Rectangle(new Point(test.Right, Math.Max(surface.Top, test.Top)), new Size(surface.Right - test.Right, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
}
if (test.Left < surface.Left && test.Right < surface.Right) // test inside surface horizontally, overflow left
{
//result.Add(new Rectangle(new Point(surface.Left, Math.Max(surface.Top, test.Top)), new Size(test.Left - surface.Left, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
result.Add(new Rectangle(new Point(test.Right, Math.Max(surface.Top, test.Top)), new Size(surface.Right - test.Right, Math.Min(surface.Bottom, test.Bottom) - Math.Max(surface.Top, test.Top))));
}
#endregion
return result;
}
表示此内容的自然方式是使用 Region
class,例如:
var result = new Region(outer);
result.Exclude(inner);
如果你真的想要一个 Rectangle
结构的列表,你可以看看使用单位矩阵使用 GetRegionScans
转换为 RectangleF
s,然后将它们转换为 Rectangle
s 使用 Ceiling
或 Floor
.