私は外側の矩形と内側の矩形を持っています。交差する四角形を切り取った後、残りの四角形を返すにはどうすればよいですか?残りの外接矩形をRectangle.Intersectionから探します。
-2
A
答えて
0
これはRegion
classを使用することです表現する自然な方法、のようなもの:
var result = new Region(outer);
result.Exclude(inner);
あなたが本当にRectangle
構造体のリストが必要な場合、あなたはGetRegionScans
を使用してRectangleF
秒への変換を見ることができます恒等行列を使用してRectangle
に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;
}
関連する問題
- 1. C#塗りつぶし矩形は空白を残す
- 2. 外接矩形内のすべてのテキストを取得
- 3. 中央矩形から最も離れた矩形を取得
- 4. 矩形が別の矩形に近いかどうかをチェックします。
- 5. 矩形の値にアクセスしますか?
- 6. マップから矩形を削除する
- 7. 2つの矩形がある場合、重なり領域を表す別の矩形を返します。
- 8. 配列からの矩形の交差
- 9. 矩形の残りの部分を2つの部分に分割して、2つの新しい矩形がより「squarish」になるようにするには、サブ矩形を付けます。
- 10. 矩形のエッジをカーブします
- 11. 矩形ペイントメソッド(MIDP)の塗りつぶし
- 12. UIViewの境界矩形をフレーム矩形よりも大きくすることができますか?
- 13. SVGの矩形要素に小さな隙間が残る
- 14. 複数の色のD3.js矩形を塗りつぶしたり、複数の矩形に分割する
- 15. JPanelに矩形がありません
- 16. 矩形内の矩形のサイズを計算する
- 17. SDL DisplayFormatは矩形を表します
- 18. 矩形内の矩形を制限する
- 19. イメージROIをEMGU CV ...の矩形以外の形状に設定していますか?
- 20. カーソルをブロックまたは矩形から行に変更しますか?
- 21. 形状分析は、他の形状から矩形を区別する
- 22. バインドされた矩形からそれに含まれる回転矩形の座標への変換座標
- 23. Androidは矩形の片側からの入力のみを許可します
- 24. 画像から矩形領域を切り抜く画像を使用して
- 25. ワンダーツインパワー "矩形にズーム"が有効になりますか?
- 26. JavaScriptで矩形を切り取る
- 27. リーフレット - 2つの正中線点から矩形座標を計算します。
- 28. QT-コンボボックスから色を選択して矩形を描く
- 29. スウィフトドロー矩形
- 30. クロップ矩形