交集算法
我的 CSharp 项目中有两个对象,它们呈现矩形。现在我想计算一个对象是否与另一个对象相交。物体不能旋转。
我有以下方法:
getX();
getY();
getWidth();
getHeight();
I have two objects in a CSharp project, which presents rectangles. Now I want to calculate if one object intersects another one. The objects cannot rotate.
I've got the following methods:
getX();
getY();
getWidth();
getHeight();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然这个问题在技术上与其他问题重复,但我会提出一个比那里发布的更优雅的解决方案。
我会从边界框的角度来看待它。如果边界框比高度之和短且比宽度之和窄,则它们必须相交:
While this is technically a duplicate of that other question, I would propose a more elegant solution than what was posted there.
The way I would look at it would be from the perspective of the bounding box. If the bounding box is shorter than the sum of the heights AND skinnier than the sum of the widths, they must intersect: