Intersection of overlapping rectangles

221 views Asked by At

i am an industrial engineer so you know my coding isn`t that good thats why i need your help. my problem is that i need to know first the area of intersection between two rectangles so that to check if there is overlapping occurring, this has to be done for 6 rectangles i need to check if they overlap. my second problem is that i have 6 rectangles inside a large warehouse with defined boundaries, i want to maximize the utilized area. how can i write a code to do so. i have used the bellow code which is online but i dont know how to use it to check for the 6 rectangles.

# Python program to check if rectangles overlap 
class Point: 
    def __init__(self, x, y): 
        self.x = x 
        self.y = y 
  
# Returns true if two rectangles(l1, r1)  
# and (l2, r2) overlap 
def doOverlap(l1, r1, l2, r2): 
      
    # If one rectangle is on left side of other 
    if(l1.x >= r2.x or l2.x >= r1.x): 
        return False
  
    # If one rectangle is above other 
    if(l1.y <= r2.y or l2.y <= r1.y): 
        return False
  
    return True
  
# Driver Code 
if __name__ == "__main__": 
    l1 = Point(0, 10) 
    r1 = Point(10, 0) 
    l2 = Point(5, 5) 
    r2 = Point(15, 0) 
  
    if(doOverlap(l1, r1, l2, r2)): 
        print("Rectangles Overlap") 
    else: 
        print("Rectangles Don't Overlap") 
1

There are 1 answers

0
hiro protagonist On

this may help you getting started:

a Rectangle is definded here with a base_point (which is the lower left point) and a size (how much it extends in x and y).

from dataclasses import dataclass

@dataclass
class Point:
    x: float
    y: float

@dataclass
class Rectangle:
    base_point: Point
    size: Point

    def x_overlap(self, other: "Rectangle") -> bool:
        if self.base_point.x < other.base_point.x:
            lower = self
            higher = other
        else:
            lower = other
            higher = self

        if lower.base_point.x + lower.size.x >= higher.base_point.x:
            return True
        else:
            return False

    def y_overlap(self, other: "Rectangle") -> bool:
        if self.base_point.y < other.base_point.y:
            lower = self
            higher = other
        else:
            lower = other
            higher = self

        if lower.base_point.y + lower.size.y >= higher.base_point.y:
            return True
        else:
            return False

    def overlap(self, other: "Rectangle") -> bool:
        return self.x_overlap(other) and self.y_overlap(other)


r0 = Rectangle(base_point=Point(x=0, y=0), size=Point(x=3, y=2))
r1 = Rectangle(base_point=Point(x=2, y=1), size=Point(x=3, y=2))

print(r0.overlap(r1))

rectangles overlap if they do overlap on the x and on the y axis. the code is probably overly designed... you may want to simplify it to suit your needs. but the way it is - i think - it showcases the idea well.

in order to find if more rectangles overlap you have to compare all of them agains one another...