바깥 쪽 사각형과 안에 사각형이 있습니다. 교차하는 사각형을 잘라낸 후에 나머지 모든 사각형을 반환하려면 어떻게해야합니까?Rectangle.Intersection에서 나머지 바깥 사각형 찾기
-2
A
답변
0
이이 Region
class을 사용하는 것입니다 대표하는 자연적인 방법, 무엇인가 : 당신이 정말로 Rectangle
구조의 목록을 원하는 경우
var result = new Region(outer);
result.Exclude(inner);
, 당신은 GetRegionScans
를 사용 RectangleF
s의 변환을 볼 수 있습니다 단위 행렬을 사용하여 으로 변환하면 Ceiling
또는 Floor
을 사용합니다.
0
나는이 함수를 작성하여 교차하는 것을 제외한 목록을 반환합니다.
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;
}