0

我正在运行一个进程,只有在视口中可见或部分可见的图像与之相关。以下代码有效,如果 img 的任何部分在屏幕上,则返回 true。但是有没有更简洁的方式来表达同样的逻辑呢?

//Can't figure out an easier way to do this!
    return (imgLeft>=winData.l && imgLeft<winData.r && imgTop>=winData.t && imgTop<winData.b) || //TL somewhere on screen
        (imgRight>=winData.l && imgRight<winData.r && imgTop>=winData.t && imgTop<winData.b) || //TR somewhere on screen
        (imgLeft>=winData.l && imgLeft<winData.r && imgBottom>=winData.t && imgBottom<winData.b) || //BL somewhere on screen
        (imgRight>=winData.l && imgRight<winData.r && imgBottom>=winData.t && imgBottom<winData.b) || //BR somewhere on screen
        (imgLeft<winData.l && imgRight>winData.r && imgTop>=winData.t && imgTop<winData.b) || //L offscreen L and R offscreen R, top on screen
        (imgLeft<winData.l && imgRight>winData.r && imgBottom>=winData.t && imgBottom<winData.b) || //L offscreen L and R offscreen R, bottom on screen
        (imgTop<winData.t && imgBottom>winData.b && imgLeft>=winData.l && imgLeft<winData.r) || //T offscreen T and B offscreen B, left on screen
        (imgTop<winData.t && imgBottom>winData.b && imgRight>=winData.l && imgRight<winData.r) || //T offscreen T and B offscreen B, right on screen
        (imgLeft<winData.l && imgRight>winData.r && imgTop<winData.t && imgBottom>winData.b) //All sides offscreen
4

1 回答 1

0

好吧,我做了一顿饭……我想那天还有太多其他事情要考虑:)

return imgLeft < winRight &&
       imgRight > winLeft &&
       imgTop < winBottom &&
       imgBottom > winTop;
于 2012-09-07T23:33:21.767 回答