简体   繁体   English

检查相邻矩形两侧的“重叠”量

[英]Check amount of 'overlap' of two sides of adjacent rectangles

I have two rectangles that are guaranteed not to overlap.我有两个保证不重叠的矩形。

However, I need to know if they are next to each other and if they are that they touch each other by two or more units.但是,我需要知道它们是否彼此相邻,以及它们是否以两个或更多单位相互接触。 For example, the following rectangles touch at 4 units.例如,以下矩形相距 4 个单位。

在此处输入图片说明

In the above example I am comparing the following:在上面的例子中,我比较了以下内容:

rect1 = {
   x: 4,
   y: 4,
   width: 3,
   height: 5
}

and

rect2 = {
   x: 7,
   y: 1,
   width: 4,
   height: 7
}

I am currently trying to accomplish this by several nested if/else statements, but I know there would be a more adequate way.我目前正试图通过几个嵌套的 if/else 语句来实现这一点,但我知道会有更合适的方法。

What is an efficient way of accomplishing this?实现这一目标的有效方法是什么?

Disclaimer:免责声明:

I used the term overlap in the title as I wasn't sure how else to describe it even though they do not actually overlap.我在标题中使用了术语overlap ,因为即使它们实际上并不重叠,我也不知道如何描述它。

Here's a simple version:这是一个简单的版本:

function rectOverlaps2(r1, r2) {
    let overlapX, overlapY;

    if ( r1.x <= r2.x ) {
        overlapX = r1.x + r1.width - r2.x;
    } else {
        overlapX = r2.x + r2.width - r1.x;
    }

    if ( r1.y <= r2.y ) {
        overlapY = r1.y + r1.height - r2.y;
    } else {
        overlapY = r2.y + r2.height - r1.y;
    }

    return (overlapX == 0 && overlapY >= 2) || (overlapY == 0 && overlapX >= 2);
}

Added: Just realized that there is an edge case - if the overlapping dimension (width or height) of one rectangle is just 1 unit, then the overlap should not be counted.补充:刚刚意识到有一个边缘情况——如果一个矩形的重叠尺寸(宽度或高度)只有1个单位,则不应计算重叠。 Since this looks like a homework question, fixing this will be left as an exercise to the reader.由于这看起来像是一个家庭作业问题,因此将其作为练习留给读者。

here's my solution for this problem.这是我对这个问题的解决方案。 My idea is to create an array that contains all numbers between the start of the rectangle and its end in the Y direction, for example.例如,我的想法是创建一个数组,其中包含矩形开始和 Y 方向结束之间的所有数字。 Then if the other rectangle contains any of the indices in between, that means they are 'overlapping'然后,如果另一个矩形包含介于两者之间的任何索引,则意味着它们“重叠”

I didn't try any performance optimization, but that's the general idea.我没有尝试任何性能优化,但这是总体思路。

rect1 = {
   x: 4,
   y: 4,
   width: 3,
   height: 5
}

rect2 = {
   x: 7,
   y: 1,
   width: 4,
   height: 7
}


function createMap(start, end) {
  return Array(end - start + 1).fill().map((_, idx) => start + idx)
}

function getEndPoints(rect){
  const endX = rect.x + rect.width;
  const endY = rect.y + rect.height;
  return {endX, endY}; 
}

function getOverlap(r1, r2){
  const r1Range = createMap(r1.y, r1.endY);
  const r2Range = createMap(r2.y, r2.endY);
  const verticalOverlap = r1Range.filter(val => !r2Range.includes(val));
  console.log(`These two rectangles overlap by: ${verticalOverlap.length} squares`)
  // Then just do the same for horizontal
}

rect1 = {...rect1, getEndpoints(rect1)}
rect2 = {...rect2, getEndpoints(rect2)}

getOverlap(rect1, rect2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM