Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

矩形重叠 #321

Open
Sunny-117 opened this issue Nov 8, 2022 · 3 comments
Open

矩形重叠 #321

Sunny-117 opened this issue Nov 8, 2022 · 3 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@lxy-Jason
Copy link
Contributor

/**
 * @param {number[]} rec1
 * @param {number[]} rec2
 * @return {boolean}
 */
var isRectangleOverlap = function (rec1, rec2) {
    //逆向思维,找不重叠的情况
    return !(
        rec1[2] <= rec2[0] || //矩形1的最右边小于2的最左边
        rec1[3] <= rec2[1] || //最上边小于2的最下边
        rec1[0] >= rec2[2] || //最右边大于2的最右边
        rec1[1] >= rec2[3]  //最下边大于2的最上边
    )
};

@Achetto
Copy link

Achetto commented Dec 8, 2022

小学数学题

/**
 * @param {number[]} rec1
 * @param {number[]} rec2
 * @return {boolean}
 */
var isRectangleOverlap = function(rec1, rec2) {
  // 找到两个矩形的中点
  let px1 = (rec1[0] + rec1[2])/2;
  let py1 = (rec1[1] + rec1[3])/2;
  let px2 = (rec2[0] + rec2[2])/2;
  let py2 = (rec2[1] + rec2[3])/2;
  // 获取两个矩形宽高的一半
  let d1 = px1 - rec1[0];
  let d2 = px2 - rec2[0];
  let d3 = py1 - rec1[1];
  let d4 = py2 - rec2[1];
  // 两个中点在的距离
  let dx = Math.abs(px1 - px2);
  let dy = Math.abs(py1 - py2);
  // 判断条件
  if (dx < d1 + d2 && dy < d3 + d4) return true;
  return false;
};

@gswysy
Copy link

gswysy commented Apr 16, 2024

/**
 * @param {number[]} rec1
 * @param {number[]} rec2
 * @return {boolean}
 */
var isRectangleOverlap = function(rec1, rec2) {
    if(rec1[0]<rec2[2]&&rec1[1]<rec2[3]&&rec1[2]>rec2[0]&&rec1[3]>rec2[1]) return true
    return false
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants