Java 查找矩形是否与直线接触

Java 查找矩形是否与直线接触,java,algorithm,geometry,intersection,Java,Algorithm,Geometry,Intersection,我有一个名为Rectangle的类,它由两对整数组成,表示矩形左上角和右下角的XY坐标。我还有一个称为Quadrant的类,它包含正方形区域上最左边、最中间和最右边的点的X值(分别为leftX、midX和rightX),以及Y平面上顶部、中间和底部的类似值(botY、midY、topY)。我想确定给定的矩形是否接触给定象限的x或y平面的中间部分。现在,这是我这样做的代码: boolean isIntersect(Rectangle rect, Quadrant node) {

我有一个名为Rectangle的类,它由两对整数组成,表示矩形左上角和右下角的XY坐标。我还有一个称为Quadrant的类,它包含正方形区域上最左边、最中间和最右边的点的X值(分别为leftX、midX和rightX),以及Y平面上顶部、中间和底部的类似值(botY、midY、topY)。我想确定给定的矩形是否接触给定象限的x或y平面的中间部分。现在,这是我这样做的代码:

boolean isIntersect(Rectangle rect, Quadrant node)
    {

        boolean xOver = false;
        boolean yOver = false;




        if((rect.bottomRight.x >=node.midX && rect.topLeft.x <= node.midX) ||
            (rect.bottomRight.x <=node.midX && rect.topLeft.x >= node.midX))

        {
            xOver = true;
        }

        if((rect.bottomRight.y <= node.midY && rect.topLeft.y >= node.midY) ||
            (rect.bottomRight.y >= node.midY && rect.topLeft.y <= node.midY))
        {

            yOver = true;
        }



        if (yOver || xOver == true)

        {
            System.out.println("intersection found");
            return true;
        }

        else
        {
            System.out.println("no intersection found");
            return false;
        }

    }
布尔isIntersect(矩形矩形,象限节点)
{
布尔xOver=false;
布尔yOver=false;

如果((rect.bottomRight.x>=node.midX&&rect.topLeft.x=node.midY&&rect.topLeft.y您的算法非常棒。在这里,它的实现更加简洁;您使用的布尔函数比需要的多得多

boolean isIntersect(Rectangle rect, Quadrant node)
{
    return (rect.bottomRight.x >=node.midX && rect.topLeft.x <= node.midX)
        || (rect.bottomRight.y <= node.midY && rect.topLeft.y >= node.midY)
}
布尔isIntersect(矩形矩形,象限节点)
{

返回(Reut.ButoRealth.x>=Nodo.MIDX & & Rcto.toPrft.x,所以矩形是Axe Palalle?您可能会考虑在您认为拥有最多(或全部)的角情况之后,将其发布到CoDeEvIEW.StAccExchange)上。
bottomRight.x <= node.midX && topLeft.x >= node.midX