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


  • 0

    const isOverlap = (rect1, rect2) => {
        function compute (a, b) {
            if (((b.x >= a.x && b.x <= a.x + a.width) ||
              (b.x + b.width >= a.x && b.x + b.width <= a.x + a.width)) &&
              (b.y <= a.y + a.height && b.y + b.height >= a.y)) {
                return true
            }
            return false
        }
        return compute(rect1, rect2) || compute(rect2, rect1)
    }
    

    代码多了一点,总算通过了


  • -2

    const isOverlap = (rect1, rect2) => 
    {
      const xFlag = rect1.x > rect2.x ? (rect1.x - rect2.x) <= rect2.width : (rect2.x - rect1.x) <= rect1.width
      const yFlag = rect1.y > rect2.y ? (rect1.y - rect2.y) <= rect2.height : (rect2.y - rect1.y) <= rect1.height
      return xFlag && yFlag;
    }
    

  • -1

    const isOverlap = (rect1, rect2) => {
    const l1 = {x:rect1.x,y:rect1.y}
    const r1 = {x:rect1.x+rect1.width,y:rect1.y+rect1.height}
    const l2 = {x:rect2.x,y:rect2.y}
    const r2 = {x:rect2.x+rect2.width,y:rect2.y+rect2.height}
    if(
    l1.x>r2.x ||
    l2.x>r1.x ||
    l1.y<r2.y ||
    r1.y>l2.y
    ) return false
    return true
    }
    请问这个代码有什么问题啊,显示Unexpected token, expected , (2:19)


  • 0

    function getLayer(rect) {
      return {
        x: rect.x,
        y: rect.y,
        m: rect.x + rect.width,
        n: rect.y + rect.height,
      }
    }
    
    function isMaxThan(x, y) {
      return x > y;
    }
    
    function values (list) {
      return Object.keys(list).map(item => list[item]);
    }
    
    function everyMatch(list1, list2, compare) {
        return list1.every((item, index) => {
            return compare(item, list2[index]);
        })
    }
    
    function everyMatchRetry(list1, list2, compare) {
    	return everyMatch(list1, list2, compare) || everyMatch(list2, list1, compare)
    }
    
    const isOverlap = (rect1, rect2) => 
      everyMatchRetry(values(getLayer(rect1)), values(getLayer(rect2)), isMaxThan)
    

  • 0

    0_1502785580047_QQ截图20170815161627.jpg

    写了一会觉得脑子有点乱,分析不相交的所有情况感觉比较清晰。

    const isOverlap = (rect1, rect2) => {
      if (rect1.y + rect1.height < rect2.y) {
        return false
      } else if (rect1.x > rect2.x + rect2.width) {
        return false
      } else if (rect1.y > rect2.y + rect2.height) {
        return false
      } else if (rect1.x + rect1.width < rect2.x) {
        return false
      }
      return true
    }
    

  • 0

    const isOverlap = (rect1, rect2) =>{

    let one=Object.values(rect1),
    two=Object.values(rect2);

    return one.every((value,index,array)=>{
    return value==two[index]
    })
    }

    逻辑是没问题的数组的值必须一样,然而总是提交报错一 一,代码不被支持么


  • 0

    @WLDragon
    const isOverlap = (r1, r2) =>!(r1.x>r2.x+r2.width||r2.x>r1.x+r1.width||r1.y+r1.height<r2.y||r2.y+r2.height<r1.y)
    效率更好一些,少一步


  • 0

    @hao#98 判断两个矩形是否重叠 中说:

    2
    const isOverlap = (rect1, rect2) => {
    const l1 = {x:rect1.x,y:rect1.y}
    const r1 = {x:rect1.x+rect1.width,y:rect1.y+rect1.height}
    const l2 = {x:rect2.x,y:rect2.y}
    const r2 = {x:rect2.x+rect2.width,y:rect2.y+rect2.height}
    if(
    l1.x>r2.x ||

    l2.x>r1.x ||

    l1.y>r2.y ||

    l2.y>r1.y

    ) return false
    return true
    }


  • 0

    const rect1 = { x: 100, y: 100, width: 100, height: 100 }
    const rect2 = { x: 150, y: 150, width: 100, height: 100 }
    isOverlap(rect1, rect2)
    function isOverlap(rect1, rect2){
        function rect(x,y,width,height){
                return {
                    x:x ||100,
                    y:y ||100,
                    width:width ||100,
                    height:height ||100
                }
        }
        if(
            rect1.x + rect1.width >= rect2.x &&
            rect1.x <= rect2.x + rect2.width &&
            rect1.y + rect1.height >= rect2.y &&
            rect1.y <= rect2.y + rect2.height
        ){ return true}else{return false}
    }

  • 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)
    

登录后回复
 

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