2012-12-09 29 views
1

我有一個類包含一個矩形,我用這些對象填充一個列表。這裏是什麼,我試圖做一個例子:列表上的矩形交互查詢<T>

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獨特交集的名單,沒有從之類的東西重複一個查詢。相交(A,B)和。交點(B,A)?

回答

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();