How to make selection box in Canvas to select any object it touches not only objects it embraces?

放肆的年华 提交于 2019-12-04 13:36:25
M Oehm

The current approach checks whether an object is completely contained in the selection rectangle. An interval obj is completely contained in another interval sel when:

    sel.start <= obj.start && obj.end <= sel.end

When you just want the objects to overlap, test:

    sel.start <= obj.end && obj.start <= sel.end

By interval, I mean a single dimension. You must, of course, test the condition for both x and y. Note the the second condition is the first one with their right-hand side swapped.

For your example, change the hit test from:

        //Are we inside the selction area?
        if (selRecXStart <= grpXStart
         && selRecXEnd >= grpXEnd
         && selRecYStart <= grpYStart
         && selRecYEnd >= grpYEnd)
        { ...

to:

        //Are we inside the selction area?
        if (selRecXStart <= grpXEnd
         && selRecXEnd >= grpXStart
         && selRecYStart <= grpYEnd
         && selRecYEnd >= grpYStart)
        { ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!