加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何计算两个矩形之间的距离? (上下文:Lua中的一个游戏)

发布时间:2020-12-15 00:27:19 所属栏目:大数据 来源:网络整理
导读:给定两个矩形x,y,宽度,高度(以像素为单位)和旋转值(以度为单位) – 如何计算其轮廓彼此距离最近的距离? 背景:在一个用Lua写的游戏中,我随机生成地图,但是要确保某些矩形彼此不太近 – 这是需要的,因为如果矩形进入某个近距离位置,地图变得无法解决,因为一
给定两个矩形x,y,宽度,高度(以像素为单位)和旋转值(以度为单位) – 如何计算其轮廓彼此距离最近的距离?

背景:在一个用Lua写的游戏中,我随机生成地图,但是要确保某些矩形彼此不太近 – 这是需要的,因为如果矩形进入某个近距离位置,地图变得无法解决,因为一个球需要通过它们之间.速度不是一个巨大的问题,因为我没有很多矩形,地图只是每级生成一次.我在StackOverflow上发现的以前的链接是this和this

提前谢谢了!

解决方法

Agnius的答案依赖于DistanceBetweenLineSegments()函数.这是一个案例分析:
(1) Check if the rects intersect. If so,the distance between them is 0.
(2) If not,think of r2 as the center of a telephone key pad,#5.
(3) r1 may be fully in one of the extreme quadrants (#1,#3,#7,or #9). If so
    the distance is the distance from one rect corner to another (e.g.,if r1 is
    in quadrant #1,the distance is the distance from the lower-right corner of
    r1 to the upper-left corner of r2).
(4) Otherwise r1 is to the left,right,above,or below r2 and the distance is
    the distance between the relevant sides (e.g.,if r1 is above,the distance
    is the distance between r1's low y and r2's high y).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读