确定2个矩形棱镜是否接触/交叉

时间:2013-01-22 00:41:13

标签: java math shapes

我在弄清楚2个矩形棱镜是否接触或重叠时遇到了一些麻烦。我只有两个矩形棱镜的高X,Y,Z和低X,Y,Z。以下是我到目前为止的情况:

    public boolean overlaps(AreaSelection other) {
    boolean Xs = (lowX <= other.getHighestX()) && (other.getLowestX() <= highX);
    boolean Ys = (lowY <= other.getHighestY()) && (other.getLowestY() <= highY); 
    boolean Zs = (lowZ <= other.getHighestZ()) && (other.getLowestZ() <= highZ);
    return (Xs && Ys && Zs);        
}

有谁知道这是否正确?如果没有,解决方案是什么?谢谢!

2 个答案:

答案 0 :(得分:2)

当且仅当坐标轴上的三对投影间隔全部重叠时,两个边缘平行于坐标轴的矩形棱镜重叠。

因此有一个“实用程序”方法检查间隔的重叠是有意义的,我们调用三次以检查棱镜的重叠。我们假设将使用正确排序的间隔'端点来调用此方法:

public boolean overlap_1d(double aLow, double aHigh, double bLow, double bHigh)
{
    if (aLow <= bLow) return (bLow <= aHigh);
    /* else aLow > bLow */
    return (aLow <= bHigh);
}

原始代码将成为:

public boolean overlaps(AreaSelection other)
{
    boolean Xs = overlap_1D(lowX,highX,other.getLowestX(),other.getHighestX());
    boolean Ys = overlap_1D(lowY,highY,other.getLowestY(),other.getHighestY());
    boolean Zs = overlap_1D(lowZ,highZ,other.getLowestZ(),other.getHighestZ());
    return (Xs && Ys && Zs);        
}

请注意,在此方法中,重叠可能包含单个交点(无论是一维还是三维)。

答案 1 :(得分:1)

如果棱镜没有旋转,你的方法是正确的,否则没有办法计算它们是否与这些值重叠。