检查两个矩形是否重叠或边缘是否接触

Check if two rectangles overlap or the edges touch

本文关键字:是否 重叠 边缘 接触 两个 检查      更新时间:2023-10-16

我得到lon和lat的地理坐标,如西北和东南lon/lat。所以,当我得到它们时,我翻译成公制。所以在这里我得到了我的第一个矩形,north_west和south_east坐标,然后我有具有北、南、西和东坐标的相同公制系统的边界矩形。我需要在边缘接触内的交叉点上检查这两个矩形,我写了什么:

bool filter(TilePosition const& tile_position) const noexcept override 
{
    MetricBoundingBox tile_bounds{tile_position};
    bool cond1 = (tile_bounds.north <= south_east_.lon);
    bool cond2 = (tile_bounds.south >= north_west_.lon);
    bool cond3 = (tile_bounds.west <= south_east_.lat);
    bool cond4 = (tile_bounds.east >= north_west_.lat);
    return cond1 && cond2 && cond3 && cond4;
} 

不确定我做得对不对?我的要求是,如果第二个矩形的边界与第一个矩形的边界相交,则必须保留第二个矩形。否则,过滤。

您的解决方案不正确。

除了混淆纬度和经度的含义之外,您还有一个基本问题,即为笛卡尔坐标空间设计的算法在圆柱坐标中不起作用,因为经度是周期性的。 对于靠近子午线的矩形,情况可能看起来不错,但是当您在环绕点(+/- 180度经度)附近开始测试时,此方法将失败。

一个简单的解决方法是将穿过包装点的每个矩形分成两个不交叉的矩形。