计算两个矩形重叠面积

725 阅读1分钟

代码如下:

judgeRectCoincidence(box1, box2) {
    const w1 = box1.x1 - box1.x;
    const w2 = box2.x1 - box2.x;
    const h1 = box1.y1 - box1.y;
    const h2 = box2.y1 - box2.y;

    const endx = Math.max(box1.x1, box2.x1);
    const startx = Math.min(box1.x, box2.x);

    const width = w1 + w2 - (endx - startx);

    const endy = Math.max(box1.y1, box2.y1);
    const starty = Math.min(box1.y, box2.y);

    const height = h1 + h2 - (endy - starty);

    if (width > 0 && height > 0) {
        const area = width * height;
        const ratio = 0.7;

        if (area / this.data.questArea >= ratio) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
},