Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查多个矩形是否相交_C# - Fatal编程技术网

C# 检查多个矩形是否相交

C# 检查多个矩形是否相交,c#,C#,我遇到了一个问题,我的列表中有多个矩形,我需要检查这些矩形是否相互重叠,因为使用foreach循环会返回一个错误。 有没有其他方法来检查碰撞 来自注释的代码 for (var x = 0; x < Water.Count(); x++ ) { Rectangle rect = Water[x]; if(rect.Y < 699) { rect.Y++;; } Water[x] = rect; frameGraphics.FillRectan

我遇到了一个问题,我的列表中有多个矩形,我需要检查这些矩形是否相互重叠,因为使用foreach循环会返回一个错误。 有没有其他方法来检查碰撞

来自注释的代码

for (var x = 0; x < Water.Count(); x++ ) 
{ 
  Rectangle rect = Water[x]; 
  if(rect.Y < 699)
  { 
    rect.Y++;; 
  }
  Water[x] = rect; 
  frameGraphics.FillRectangle(new SolidBrush(Color.Red), Water[x]); 
} 
这是我的代码,使矩形下降,并检查y是否<699现在我需要检查这些列表中的一个矩形是否相互碰撞


谢谢

在检查交叉点之前,您应该使用或某种类型的同步对正在迭代的列表的访问,或者复制集合

您可以使用

下面是一个从msdn复制的示例

private Rect intersectExample2()
{
    // Initialize new rectangle.
    Rect myRectangle = new Rect();

    // The Location property specifies the coordinates of the upper left-hand  
    // corner of the rectangle. 
    myRectangle.Location = new Point(10, 5);

    // Set the Size property of the rectangle with a width of 200 
    // and a height of 50.
    myRectangle.Size = new Size(200, 50);

    // Create second rectangle to compare to the first.
    Rect myRectangle2 = new Rect();
    myRectangle2.Location = new Point(0, 0);
    myRectangle2.Size = new Size(200, 50);

    // Intersect method finds the intersection between the specified rectangles and  
    // returns the result as a Rect. If there is no intersection then the Empty Rect  
    // is returned. resultRectangle has a size of 190,45 and location of 10,5. 
    Rect resultRectangle = Rect.Intersect(myRectangle, myRectangle2);

    return resultRectangle;

}
下面是一些检查多个矩形是否相交的代码

bool CheckIfAllIntersect(IEnumerable<Rect> rectangles)
{
    return rectangles.Aggregate(rectangles.FirstOrDefault(), Rect.Intersect) != Rect.Empty;
}
如果希望任何矩形相交,可以使用以下命令

bool CheckIfAnyInteresect(IEnumerable<Rect> rectangles) 
{
    return rectangles.Any(rect => rectangles.Where(r => !r.Equals(rect)).Any(r => r.IntersectsWith(rect)));
}

到目前为止你尝试了什么?请将代码张贴到卡住的地方好吗?我尝试使用foreach循环遍历列表并检查与相交的冲突,但由于列表正在编辑,因此无效。列表更改是因为您正在删除相交矩形,还是因为外部原因而更改?我正在添加新的矩形当我单击时,您是否在多线程上运行?如果没有,您将无法执行click事件处理程序并同时查找相交的矩形,因此问题应该在其他地方。是的,我有,但我需要检查多个矩形,并且无法使用for循环,因为单击时列表将更改,这会引发异常。如果列表正在更改,在检查之前,您可能应该同步对列表的访问或复制列表。然后可以创建一个新方法,该方法接受IEnumerable,并在内部使用Rect.Intersect.List Water2=Water;foreach Rectangle check in Water2{if check.IntersectsWithWater[x]{intersect=true;}}}我尝试过这个方法,但它总是返回true?我认为这是因为它检查矩形是否与自身碰撞。Water2=水只是一个引用副本。你需要一个新的列表。var Water2=新的ListWater;不管谁否决了这个答案,请留下一个解释好吗?依我看,我的答案回答了这个问题。如果没有,我想改进它。