检测线段是否与正方形相交
有人有一个简单的算法吗?不需要旋转什么的。只需查找由两点组成的线段是否与正方形相交
Anyone have a simple algorithm for this? No need for rotation or anything. Just finding if a line segment made from two points intersects a square
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码应该可以解决问题。它检查线与边相交的位置,然后检查它是否在正方形的宽度内。返回相交数。
注意:此代码是理论上的,可能不正确,因为它尚未经过测试
This code should do the trick. It checks where the line intersects the sides, then checks if that is within the width of the square. The number of intesections is returned.
NB: this code is theoretical and may not be correct, as it has not been tested
您可以通过投射向量并计算它穿过的边数来完成此操作。
如果它穿过的边是偶数,则它在物体外部,如果它穿过的边是奇数,则它在物体内部。
这适用于所有闭合多边形。
You can do this by casting a vector and counting the number of edges it crosses.
If the edges it crosses are even, it is outside the object, if the edges it crosses are odd, it is inside.
This works for all closed polygons.
方法如下:
- 按 x 坐标对正方形的顶点进行排序
- 按 x 坐标对线的端点进行排序
- 计算从线的 minX 端到中间两个(通过 x 坐标)方形顶点中的每一个的角度
- 计算线的角度
- 如果线的角度在可能的角度内,您所要做的就是检查长度,即线的 maxX 末端 >正方形的 minX 顶点
如果正方形直接面向线,这可能会中断,在这种情况下,我只需通过检查正方形的第一条边来对其进行特殊处理。
Here's a way:
- sort the vertex points of the square by x-coord
- sort the endpoint of the line by x-coord
- calculate angle from the minX end of the line to each of the middle two (by x-coord) square vertices
- calculate angle of the line
- if the line's angle is within the possible angles, all you have to do is a length check, is maxX end of the line > minX vertex of the square
This will probably break if the square is directly facing the line, in that case I'd just special-case it by checking the first edge of the square.