c# - UWP 非矩形对象对撞机检测器
问题描述
我需要知道两个通用对象是否相交。我尝试创建两个相同大小的对象的矩形,并使用它工作的相交方法,但是,这种方法在椭圆等对象上并不准确。
有没有一种方法可以告诉我两个非矩形物体是否相交?
GeneralTransform t1 = this.object1.TransformToVisual(this);
GeneralTransform t2 = this.object2.TransformToVisual(this);
Rect r1 = t1.TransformBounds(new Rect() { X = 0, Y = 0, Width = this.object1.ActualWidth, Height = this.object1.ActualHeight });
Rect r2 = t2.TransformBounds(new Rect() { X = 0, Y = 0, Width = this.object2.ActualWidth, Height = this.object2.ActualHeight });
bool result = r1.IntersectsWith(r2);
解决方案
您可以使用HitTestCore
. 请参阅该方法的文档,您需要接受的重载GeometryHitTestParameters
。
编辑:你得到第二个对象的几何形状,并用它调用'HitTestCore'。
推荐阅读
- pyspark - 如何将前导零添加到 pyspark 数据框列
- react-native - 将数据发送到 bottomTabNavigator 中的每个屏幕
- scala - 在 Scala 中读取带有多行字符串的 CSV 文件
- firebase - 如何在 Firestore 中测试零文档
- android - Parcelable 对象中的字符串资源字段的值并不总是可以通过 getString() 访问
- pdf - 使用 pandoc 创建带有 css 和 html 的 PDF
- java - Gradle 设置以处理具有相同代码库但依赖项不同的两种部署类型
- javascript - 如何创建返回有效负载的 Redux Saga
- sql - 查找关键字“WHERE”附近语法不正确的原因
- python - Handling conditional logic + sentinel value with mypy