prepbook
  • Introduction
  • Some common stuff
    • python __repr__
    • HackerRank input tips
  • Data Structures and Algorithms
    • Breadth first search
    • Depth First Search
    • Dijkstra
    • A* Search Algorithm
    • Binary Search
    • python counter
    • Sorting
      • Merge Sort
      • Quick Sort
    • Priority Queue
  • Multiprocessing vs Threading
  • Common Coding
    • Find loop in lin list
    • Maximum sum subarray
  • Coding
    • Valid palindrome
    • Palindrome number
    • Remove duplicates from sorted array
    • Island perimeter
    • Serialize and Deserialize Binary Tree
    • Valid Soduku
    • Word Pattern
    • Word Pattern II
    • Group Anagrams
    • Implement Trie
    • Deep copy list with random node
    • Palindrome Permutation
    • Combination Sum
    • Clone Graph
    • Generate parenthesis
    • Fibonacci Number
    • LRU Cache
    • Merge two sorted arrays in place
    • Hamming Distance
    • Merge K sorted arrays
    • Kth smalles element in BST
    • Kth largest element in an array
    • Remove duplicates from sorted list
    • Power of 2
    • Nested list weight sum
    • SIngle number in a list
    • Factor combinations
    • Delete node from BST
  • hacker Rank
    • Coding
      • print staircase
      • Drawing book
      • Challenge 0
      • Min-Max sum
  • WorkRelatedCoding
    • Rectangle Overlap
  • Python tips
Powered by GitBook
On this page
  • Calculate overlapped area between two rectangles
  • Find if two rectangles overlap

Was this helpful?

  1. WorkRelatedCoding

Rectangle Overlap

PreviousWorkRelatedCodingNextPython tips

Last updated 5 years ago

Was this helpful?

 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)

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;
}
Calculate overlapped area between two rectangles
Find if two rectangles overlap