简体   繁体   English

jQuery / canvas:检查矩形是否与另一个矩形交叉

[英]jQuery / canvas: check if rectangle crosses another rectangle

I have a grid on which users can draw rectangles (actually rooms in a house). 我有一个网格,用户可以在该网格上绘制矩形(实际上是房屋中的房间)。 Because rooms cannot cross each other, I try to check for conflicts before drawing the second room. 由于房间不能彼此交叉,因此我尝试在绘制第二个房间之前检查是否存在冲突。 So far I managed to check if the 'to-point' of the second rectangle is inside the first rectangle (=> return false). 到目前为止,我设法检查第二个矩形的“指向”是否在第一个矩形内(=>返回false)。 But the code has to 'return: false' also when the boundaries of the room cross another room. 但是当房间的边界越过另一个房间时,代码也必须“返回:false”。

I have set up an example page here 我在这里设置了一个示例页面

To try it, click anywhere in the grid and click somewhere else to create the first room. 要尝试,请单击网格中的任意位置,然后单击其他位置以创建第一个房间。 Then click outside this box and move the cursor to see the problem... 然后在此框外单击并移动光标以查看问题...

In the source code I have marked the concerning code (starts on line 175). 在源代码中,我已标记了相关代码(从第175行开始)。

Thanks in advance! 提前致谢!

You return false in the anonymous function called by the each statement :-) the checkConflict function always return true ! 您在每个语句调用的匿名函数中返回false :-) checkConflict函数始终返回true! Look : 看:

function checkConflict(fromx, fromy, tox, toy) {
    $.each(rooms, function() {
        var left1 = fromx;
        var left2 = $(this)[0];
        var right1 = tox;
        var right2 = $(this)[2];
        var top1 = fromy;
        var top2 = $(this)[1];
        var bottom1 = toy;
        var bottom2 = $(this)[3];

        if (bottom1 < top2) { return false; }
        if (top1 > bottom2) { return false; }
        if (right1 < left2) { return false; }
        if (left1 > right2) { return false; }
    });  
    return true;
}

This is easy to fix but I give you the updated code because your tests are not correct too ( you want to test if you are INSIDE the box, your previous code would always return false ) So this is probably something like this (you have to improve this code because it won't work perfectly either) : 这很容易解决,但是我给您提供了更新的代码,因为您的测试也不正确(您想测试是否在盒子内,您以前的代码将始终返回false),所以可能是这样的(您必须改进此代码,因为它也无法完美运行):

  function checkConflict(fromx, fromy, tox, toy) {
    var returnValue = true;
    $.each(rooms, function() {
       if (tox > this[0] &&
           tox < this[2] && 
           toy > this[1] && 
           toy < this[3]) 
        { returnValue = false; return false; }

        if (fromx > this[0] && 
            fromx < this[2] &&
            fromy > this[1] && 
            fromy < this[3]) 
        { returnValue = false; return false; }

    });
    return returnValue;
}

EDIT: I wrote the correct code. 编辑:我写了正确的代码。 I thought it would be easier but, a promise is a promise... There are probably ways to refactor the code but the headache is enough for now :-D 我认为这会更容易,但是,一个承诺就是一个承诺...可能有一些方法可以重构代码,但现在头痛不已:-D

I made a JSFiddle so you can see the result : http://jsfiddle.net/T4ta9/2/ 我做了一个JSFiddle,所以您可以看到结果: http : //jsfiddle.net/T4ta9/2/

function checkConflict(fromx, fromy, tox, toy) {
    var returnValue = true;
    $.each(rooms, function() {

         var squareLeft = Math.min( parseInt(this[0]) ,parseInt(this[2])) , 
            squareRight = Math.max(parseInt(this[0]) ,parseInt(this[2]))  , 
            squareTop = Math.min(parseInt(this[1]) ,parseInt(this[3])) ,
            squareBot = Math.max(parseInt(this[1]) ,parseInt(this[3])) ;

         //drawing inside a shape
         if ((fromx > squareLeft && fromx < squareRight && fromy > squareTop && fromy < squareBot) 
         && (tox > squareRight || tox < squareLeft || toy > squareBot || toy < squareTop)) {
        returnValue = false;
        return false;
    }

        // meet the bottom of the current square ?
        if (fromy >= squareBot && // we are below this square 
                ( 
                (toy < squareBot) &&  // and our destination is above the bottom of this square
                    ( 
                        (
                            fromx < squareRight && // we are drawing on the inner left side of this square
                            (tox > squareLeft || fromx > squareLeft)  // and our destination is on the left of this square, or we started to draw on the right part of this square
                        ) 
                        || 
                        (
                            fromx > squareLeft && // we are drawing on the inner right side of this square
                            (tox < squareRight || fromx < squareRight)  // and our destination is on 
                        )
                    )
                )
            ) 
        {
            returnValue = false;
            return false;
        }

        // meet the top of a square ?
        if (fromy <= squareTop && 
                ( 
                (toy > squareTop) &&  
                    ( 
                        (
                            fromx < squareRight && 
                            (tox > squareLeft || fromx > squareLeft) 
                        ) 
                        || 
                        (
                            fromx > squareLeft && 
                            (tox < squareRight || fromx < squareRight)  
                        )
                    )
                )
            ) 
        {
            returnValue = false;
            return false;
        }

        // meet the left of a square ?
        if (fromx <= squareLeft && 
                ( 
                (tox > squareLeft) &&  
                    ( 
                        (
                            fromy < squareBot && 
                            (toy > squareTop || fromy > squareTop) 
                        ) 
                        || 
                        (
                            fromy > squareTop && 
                            (toy < squareBot || fromy < squareBot)  
                        )
                    )
                )
            ) 
        {
            returnValue = false;
            return false;
        }

        // meet the right of a square ?
        if (fromx >= squareRight && 
                ( 
                (tox < squareRight) &&  
                    ( 
                        (
                            fromy < squareBot && 
                            (toy > squareTop || fromy > squareTop) 
                        ) 
                        || 
                        (
                            fromy > squareTop && 
                            (toy < squareBot || fromy < squareBot)  
                        )
                    )
                )
            ) 
        {
            returnValue = false;
            return false;
        }

    });
    return returnValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM