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


  • 0
    administrators

    用一个对象的数据来表示一个矩形的位置和大小:

    {
      x: 100,
      y: 100,
      width: 150,
      height: 250
    }
    

    它表示一个宽为 150 高为 250 的矩形在页面上的 (100, 100) 的位置。

    请你完成一个函数 isOverlap 可以接受两个矩形作为参数,判断这两个矩形在页面上是否重叠。例如:

    const rect1 = { x: 100, y: 100, width: 100, height: 100 }
    const rect2 = { x: 150, y: 150, width: 100, height: 100 }
    isOverlap(rect1, rect2) // => true
    

  • 0

    const isOverlap = (rect1, rect2) => {
    let minX1=rect1.x;
    let maxX1=rect1.x+rect1.width;
    let minY1=rect1.y;
    let maxY1=rect1.y+rect1.height;
    let minX2=rect2.x;
    let maxX2=rect2.x+rect2.width;
    let minY2=rect2.y;
    let maxY2=rect2.y+rect2.height;
    if(minX2>maxX1||maxX2<minX1||minY2>maxY1||maxY2<minY1){
    return false;
    }
    return true;
    }


  • 1

    const isOverlap = (rect1, rect2) => {
      let r1 = rect1.x > rect2.x + rect2.width || rect1.y > rect2.y + rect2.height
      let r2 = rect2.x > rect1.x + rect1.width || rect2.y > rect1.y + rect1.height
      return !(r1 || r2);
    }
    

  • 0

    @WLDragon


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

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


  • 0

    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;
    }
    

  • 0

    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)
    效率更好一些,少一步


登录后回复
 

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