1

我有一个包含 Rectangle 的类,我用这些对象填充了一个列表。这是我正在尝试做的一个例子:

class Foo 
{ 
   Rectangle rect; 
   public Foo(Rectangle r) { rect = r; }
}

List<Foo> listFoo = new List<Foo>();
// Call the next three Rectangles 'A' 'B' and 'C'.
listFoo.Add(new Foo(new Rectangle(0, 0, 5, 5)));    // Rect 'A' intersects with B
listFoo.Add(new Foo(new Rectangle(3, 3, 5, 5)));    // Rect 'B' intersects with A & C
listFoo.Add(new Foo(new Rectangle(6, 6, 5, 5)));    // Rect 'C' intersects with B

var query = ???;

foreach (Rectangle r in query) 
{ 
    // Should give two results
    // Rectangle(3, 3, 2, 2);   A & B
    // Rectangle(6, 6, 2, 2);   B & C
}

我可以编写一个使用 Rectangle.Intersect() 来返回 listFoo 中唯一交叉点列表的单个查询,而不会与 .Intersect(A,B) 和 .Intersect(B,A) 之类的内容重复吗?

4

1 回答 1

4
 var q = (from f1 in listFoo
          from f2 in listFoo
          let r = Rectangle.Intersect(f1.rect,f2.rect)
          where f1 != f2 && r != Rectangle.Empty
          select r).Distinct();
于 2012-12-09T22:43:33.407 回答