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

前端之家收集整理的这篇文章主要介绍了如何计算两个矩形之间的距离? (上下文:Lua中的一个游戏)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给定两个矩形x,y,宽度,高度(以像素为单位)和旋转值(以度为单位) – 如何计算其轮廓彼此距离最近的距离?

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

提前谢谢了!

解决方法

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).
原文链接:https://www.f2er.com/lua/274485.html

猜你在找的Lua相关文章