我很难弄清楚两个直角棱镜是否接触或重叠。我只有两个直角棱镜的highX,Y,Z和lowX,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);
}


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

最佳答案

当且仅当坐标轴上的三对投影间隔分别重叠时,两个平行于坐标轴的矩形棱镜才会重叠。

因此,有一种“实用程序”方法可以检查间隔的重叠,这是有意义的,我们将其调用三次以检查棱镜的重叠。我们假设将在间隔的端点正确排序的情况下调用此方法:

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);
}


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

09-10 08:05