2017-03-29 2 views

答えて

0

これはRegion classを使用することです表現する自然な方法、のようなもの:

var result = new Region(outer); 
result.Exclude(inner); 

あなたが本当にRectangle構造体のリストが必要な場合、あなたはGetRegionScansを使用してRectangleF秒への変換を見ることができます恒等行列を使用してRectangleCeilingまたは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; 
    } 
関連する問題