> For the complete documentation index, see [llms.txt](https://bhabs.gitbook.io/prepbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bhabs.gitbook.io/prepbook/workrelatedcoding/rectangle-overlap.md).

# Rectangle Overlap

## [Calculate overlapped area between two rectangles](https://stackoverflow.com/questions/27152904/calculate-overlapped-area-between-two-rectangles)

```
 print("Valid IOU found for: ", rectGT, rectPD)
 Ax = max(rectGT[0], rectPD[0])
 Ay = max(rectGT[1], rectPD[1])
 Cx = min(rectGT[2], rectPD[2])
 Cy = min(rectGT[3], rectPD[3])

 IOU = abs(Cx - Ax) * abs(Cy - Ay)
```

## [Find if two rectangles overlap](https://www.geeksforgeeks.org/find-two-rectangles-overlap/)

```
Assume that the (0,0) reference coordinate is at the top left corner
  # In Python:
    # ---------------------------------------------------------
    # Check if 2 rectangles overlap
    # ---------------------------------------------------------
    def doOverlap(self, rectGT, rectPD): #Point l1, Point r1, Point l2, Point r2)
        # If one rectangle is on left side of other
        if rectGT[0] > rectPD[2] or rectPD[0] > rectGT[2]: #(l1.x > r2.x || l2.x > r1.x)
            return -1

        # If one rectangle is above other
        if rectGT[1] > rectPD[3] or rectPD[1] > rectGT[3]: #(l1.y < r2.y || l2.y < r1.y)
            return -1

        return 1



# In C++
Assume that the (0,0) reference coordinate is at the top left corner


#include<bits/stdc++.h>

struct Point
{
    int x, y;
};

// Returns true if two rectangles (l1, r1) and (l2, r2) overlap
bool doOverlap(Point l1, Point r1, Point l2, Point r2)
{
    // If one rectangle is on left side of other
    if (l1.x > r2.x || l2.x > r1.x)
        return false;

    // If one rectangle is above other 
    // (this is true when we assume (0,0) ref coordinate is at the bottom left corner
    //if (l1.y < r2.y || l2.y < r1.y)
        //return false;

    // Amit: (this is true when we assume (0,0) ref coordinate is at the top left corner
    if (l1.y > r2.y || l2.y > r1.y)
        return false;



    return true;
}

/* Driver program to test above function */
int main()
{
    Point l1 = {0, 10}, r1 = {10, 0};
    Point l2 = {5, 5}, r2 = {15, 0};
    if (doOverlap(l1, r1, l2, r2))
        printf("Rectangles Overlap");
    else
        printf("Rectangles Don't Overlap");
    return 0;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://bhabs.gitbook.io/prepbook/workrelatedcoding/rectangle-overlap.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
