Benjamin Song
Benjamin Song

Reputation: 7

Given coordinates of two rectangles, find out if the rectangles overlap or not

I'm having a problem with completing this code. I'm not sure how to implement 'struct' in here for the rectangle points p1.x, p2.x and p1.y, and p2.y. How would I continue to go about this problem?

Should I use CGPoint?

struct coordinates
{
    int x, y;
};

BOOL rectOverlap (int p1, int p2, int q1, int q2)
// getting error: "member reference base type 'int' is not a structure or union"
// on the "if" statement.
{

    if (p1.x > q2.x || q2.x > p1.x || p1.y < q2.y || q2.y < p1.y) {        //this line error
        return false;
    } else {
        return true;
    }
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //nothing here yet.
    }
    return 0;
}

Upvotes: 0

Views: 72

Answers (1)

gsempe
gsempe

Reputation: 5499

In objc there is a struct to define a rectangle called CGRect.
You can create one with the method CGRectMake
Then you should use the method CGRectIntersectsRect which determines if two CGRect overlap or not. Its documentation is here

Upvotes: 2

Related Questions