#98 判断两个矩形是否重叠


  • 0

    function Rect(x, y, width, height) {
      return {
        x1: x,
        x2: x + width,
        y1: y,
        y2: y + height,
      }
    }
    const isOverlap = (rect1, rect2) => {
      const r1 = Rect(rect1.x, rect1.y, rect1.width, rect1.height);
      const r2 = Rect(rect2.x, rect2.y, rect2.width, rect2.height);
      if (r1.x1 > r2.x2 || r1.x2 < r2.x1 || r1.y1 > r2.y2 || r1.y2 < r2.y1) {
        return false;
      } else {
        return true;
      }
    }
    

  • 0

    const isOverlap = (a, b) => !(a.y+a.height<b.y||a.x>b.x+b.width||a.y>b.y+b.height||a.x+a.width<b.x)
    

    去掉不重叠就是重叠
    a在b的上右下左


  • 0

    const isOverlap = (rect1, rect2) =>{

    let rect1Xmax=rect1.x+rect1.width;
    
    let rect1Ymax=rect1.y+rect1.height;
    
    let rect2Xmax=rect2.x+rect2.width;
    
    let rect2Ymax=rect2.y+rect2.height;
    
    let r1=(rect1.x<=rect2.x&&rect2.x<=rect1Xmax||rect2.x<=rect1.x&&rect1.x<=rect2Xmax)
    let r2=(rect1.y<=rect2.y&&rect2.y<=rect1Ymax||rect2.y<=rect1.y&&rect1.y<=rect2Ymax) 
         
     return r1&&r2;
    

    }


  • 0

    const isOverlap = (rect1, rect2) => /* TODO */
    {
    let a=rect1.x
    let b=rect1.y
    let c=rect2.x
    let d=rect2.y
    if (c-a>rect1.width||d-b>rect1.height) {
    return false
    } else if(a-c>rect2.width||b-d>rect2.height){
    return false
    } else {
    return true
    }
    }


  • 0

    @Acthing
    换种写法而已,能不能提供新的思路。感觉这种方法是把所有不重叠的情况都举出来了


  • 0

    const isOverlap = (rect1, rect2) => {
    rect1.xEnd = rect1.x+rect1.width;
    rect1.yEnd = rect1.y+rect1.height;
    rect2.xEnd = rect2.x+rect2.width;
    rect2.yEnd = rect2.y+rect2.height;
    return !(rect1.xEnd < rect2.x || rect1.x > rect2.xEnd ||
    rect1.yEnd < rect2.y || rect1.y > rect2.yEnd);
    }


  • 0

    const isOverlap = (rect1, rect2) =>
    !(rect1.x > rect2.x + rect2.width || rect1.y > rect2.y + rect2.height || rect2.x > rect1.x + rect1.width || rect2.y > rect1.y + rect1.height);

    哈哈哈,一行代码搞定


  • 0

    const isOverlap = (rect1, rect2) => /* TODO */
    {
    if(Math.abs(rect1.x-rect2.x)>(rect1.x>rect2.x?rect2.width:rect1.width) || Math.abs(rect1.y-rect2.y)>(rect1.y>rect2.y?rect2.height:rect1.height))
    return false;
    else return true;


  • 0

    const isOverlap = (rect1, rect2) => {
      let arr=[rect1,rect2]
      let xJoin = Math.abs(rect1.x-rect2.x)<=arr.filter(item=>(item.x===Math.min(rect1.x,rect2.x)))[0].width;
      let yJoin = Math.abs(rect1.y-rect2.y)<=arr.filter(item=>(item.y===Math.min(rect1.y,rect2.y)))[0].height;
      return xJoin && yJoin
    }
    
    

  • 0

    let L1=rect1.x,R1=rect1.x+rect1.width,T1=rect1.y,B1=rect1.y+rect1.height;
    let L2=rect2.x,R2=rect2.x+rect2.width,T2=rect2.y,B2=rect2.y+rect2.height;
    if(!(R2<L1||L2>R1||B2<T1||T2>B1) ){
    return true
    }else{
    return false
    }


  • 1

    /**
     * 思路:
     * - (旧)如果r1的4个点都不在r2中,就不会重叠。但这个思路有重复判断的地方。
     * - (新)如果水平上重复、且竖直上也重复,就发生重叠。
     */
    
    const isOverlap = (rect1, rect2) => isXOverlap(rect1, rect2) && isYOverlap(rect1, rect2)
    
    /**
     * 判断水平上是否重叠
     * 即:r1的x是否落在r2的横边上,反之亦然
     */
    const isXOverlap = (rect1, rect2) => (rect1.x >= rect2.x && rect1.x <= rect2.x + rect2.width) ||
                                         (rect2.x >= rect1.x && rect2.x <= rect1.x + rect1.width)
    
    /**
     * 判断竖直上是否重叠
     * * 即:r1的y是否落在r2的竖边上,反之亦然
     */
    const isYOverlap = (rect1, rect2) => (rect1.y >= rect2.y && rect1.y <= rect2.y + rect2.height) ||
                                         (rect2.y >= rect1.y && rect2.y <= rect1.y + rect1.height)
    

  • 0

    const isOverlap = (rect1, rect2) => /* TODO */
    {
    const rect1Xleft=rect1.x,
    rect1Xright=rect1.x+rect1.width,
    rect2Xleft=rect2.x,
    rect2Xright=rect2.x+rect2.width,
    rect1Ytop=rect1.y,
    rect1Ybottom=rect1.y+rect1.height,
    rect2Ytop=rect2.y,
    rect2Ybottom=rect2.y+rect2.height,
    resultX=rect2Xright>=rect1Xleft&&rect2Xleft<=rect1Xright,
    resultY=rect2Ybottom>=rect1Ytop&&rect2Ytop<=rect1Ybottom;
    return resultX&&resultY;
    }


  • 0

    const isOverlap = (r1, r2) => {
      if (r1.x > r2.x + r2.width) return false;
      if (r1.y > r2.y + r2.height) return false;
      if (r1.x + r1.width < r2.x) return false;
      if (r1.y + r1.height < r2.y) return false;
      
      return true;
    }
    

  • 0

    提示信息:输入 {"x":100,"y":100,"width":100,"height":100}, {"x":200,"y":200,"width":100,"height":100},应该输出 true 而你的是 false
    不懂,宽高完全相等的两个图形,起始点偏移量为自身长度,这完全是平移呀,怎么会重叠呢?结果为啥应该是true呢?


  • 0

    0_1548834864988_#98 判断两个矩形是否重叠.png


  • 0

    @SimonMa
    /* *

    • ! 矩形一 的左下角 x/y 坐标 均小于等于 矩形二 的右上角 x/y 坐标 , 并且
    • ? 矩形二 的左下角 x/y 坐标 均小于等于 矩形一 的右上角 x/y 坐标
    • 即可说明 , 两个矩形 相互重叠
      */
    const isOverlap = (
        { x: x1, y: y1, width: w1, height: h1 },
        { x: x2, y: y2, width: w2, height: h2 }
    ) => x1 <= x2 + w2 && y1 <= y2 + h2 && x2 <= x1 + w1 && y2 <= y1 + h1
    

  • 0

    const isOverlap = (a, b) => {
     return  !(a.x+a.width<b.x||b.x+b.width<a.x||a.y+a.height<b.y||b.y+b.height<a.y)
    }

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待