如何在 C# 中完成矩形碰撞算法

How could be done algorithm for rectangle collisions in C#

如何在 C# 中针对 canvas 上的 Rectangle 冲突完成算法?

我们应该应用哪些 X,Y 值来避免碰撞?

基本上我在 List 中有两个 Rectangle 所以我只需要检测它们之间是否有碰撞。

我不确定这是否是有用的代码线-矩形碰撞的 Liang-Barsky 算法 https://gist.github.com/ChickenProp/3194723

提前致谢!

您可以使用 System.Windows.Rect.IntersectsWith。像这样尝试:

Rect rect1 = new Rect(left1, top1, widht1, height1);
Rect rect2 = new Rect(left2, top2, widht2, height2);

bool intersects = rect1.IntersectsWith(rect2);