检测点何时何地退出矩形区域的算法



假设我们有一个矩形或正方形,我们知道其角(4个角)的x,y坐标。

还假设我们在该正方形内有一个点,我们知道它的坐标 (x,y)、速度 (km/h)、它的航向(航向以方向度为单位,0 表示北,180 表示南等)以及它具有这些属性的时间点(以秒为单位的纪元时间)。

我们如何计算该点将退出矩形的时间点(以秒为单位的纪元时间)以及出口的坐标 (x,y) ?

您需要先找到相交的边。制作沿两个坐标移动的方程并计算第一次相交时间。

请注意,对于地理坐标,您可能需要更复杂的计算,因为由纬度/纬度坐标定义的"矩形"实际上是地球表面上的弯曲梯形。查看此页面上的"给定起点和方位角的两条路径的交点"一章以获取旅行时间。

vx = V * Cos(heading + Pi/2)   //for y-axis north=0
vy = V * Sin(heading + Pi/2)
x = x0 + vx * t
y = y0 + vy * t
//potential border positions    
if vx > 0 then
   ex = x2
else
   ex = x1
if vy > 0 then
   ey = y2
else
   ey = y1
 //check for horizontal/vertical directions
if vx = 0 then
return cx = x0,  cy = ey, ct = (ey - y0) / vy
if vy = 0 then
    return cx = ex, cy = y0, ct = (ex - x0) / vx

//in general case find times of intersections with horizontal and vertical edge line
  tx = (ex - x0) / vx
  ty = (ey - y0) / vy
 //and get intersection for smaller parameter value
 if tx <= ty then 
    return cx = ex, cy = y0 + tx * vy, ct = tx
 else
    return  cx = x0 + ty * vx,  cy = ey,  ct = ty

最新更新