Rectangle Intersection

EPI 5.11

Geeks


Description:

Two rectangle, given bottom left point and top right point, determine if they overlap.

Example:

Note

Idea:

比对角点。

Code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Point{
    int x;
    int y;
};

struct Rectangle{
    Point bottomLeft;
    Point topRight;
    Rectangle(const Point& p1, const Point& p2): bottomLeft(p1), topRight(p2) {}
};

bool rectangleOverlap(const Rectangle& r1, const Rectangle& r2){
    if(r1.bottomLeft.x>r2.topRight.x || r1.topRight.x<r2.bottomLeft.x){
        return false;
    }
    if(r1.bottomLeft.y>r2.topRight.y || r1.topRight.y<r2.topRight.y){
        return false;
    }
    return true;
}

int main(){
    Rectangle r1({0,0}, {10,10});
    Rectangle r2({5,0}, {15,5});

    cout<<rectangleOverlap(r1, r2);
}

results matching ""

    No results matching ""