查找具有最小最大点的两个边界框之间的距离

时间:2020-06-26 08:14:05

标签: java algorithm geometry bounding-box

除了这里的上一个问题Calculate Smart Group of boxes with min max points之外,我尝试查找两个框之间的距离:

这是我的盒子课

public class BoundaryVolume {
    public Point3D min;
    public Point3D max;
}

第一步,我发现是否存在重叠,然后返回0 否则我会尝试找到距离

这是我的代码:

public static double getDistance(BoundaryVolume box1 , BoundaryVolume box2) {
    if(doOverlap(box1 ,box2)) return 0;
    double dis1 = box1.min.distance(box2.min);
    double dis2 = box1.min.distance(box2.max);
    double dis3 = box1.max.distance(box2.min);
    
    double dis4 = box1.max.distance(box2.max);
    double min_disatnce = dis1;
    if(dis2 < min_disatnce) min_disatnce = dis2;
    if(dis3 < min_disatnce) min_disatnce = dis3;
    if(dis4 < min_disatnce) min_disatnce = dis4;
    return min_disatnce;
    
}
public static boolean doOverlap(BoundaryVolume box1 , BoundaryVolume box2)  
{ 
    double x5  = Math.max(box1.min.get_x().get(), box2.min.get_x().get()); 
    double y5  = Math.max(box1.min.get_y().get(), box2.min.get_y().get()); 
    double x6  = Math.min(box1.max.get_x().get(), box2.max.get_x().get()); 
    double y6  = Math.min(box1.max.get_y().get(), box2.max.get_y().get()); 

  
    // no intersection 
    if (x5 > x6 || y5 > y6)  
    { 
        System.out.println("No intersection"); 
        return false; 
    } 
    System.out.println("Has intersection"); 
    return true; 
} 

我明白了: [(-350.0,-500.0,0.0),(350.0,200.0,0.0)]和[(-30.0,-680.0,-90.0),(130.0,-520.0,70.0)]之间的距离是378.02116342871597 < / strong> 看来这是错误的,

我试图找到这两个功能中不起作用的内容

1 个答案:

答案 0 :(得分:1)

enter image description here

您计算距离a。但是实际的最小距离是b或c。

您还只能检查beetwen左上角和右下角的距离: enter image description here

在此示例中,您计算​​a或b(相同距离)。当您计算c时,他的实际最小距离是d或e之后。

要寻找2维,只需搜索锥角x和y之间的较短距离。 的较短距离:

box1_max_x > box2_min_x
box1_max_y > box2_min_y
box2_max_y > box1_min_y
box2_max_y > box1_min_y
box1_max_x > box2_max_x
box1_max_y > box2_max_y
box2_min_y > box1_min_y
box2_min_y > box1_min_y