我有一个包含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)这样的重复项吗? / p>
答案 0 :(得分: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();