I have a program that, among many other things, checks to see if a Rectangle is at all overlapping with another rectangle - meaning, if any of the points of one rectangle is inside another given rectangle, they are overlapping. A rectangle is defined by its top-left point and its bottom-right point - I think this is more commonly known as a "bounding box". Here is the code I have for checking this:
def overlaps(self, other): """ Return true if a box overlaps with this box. A Box class has 8: self.left left side self.top "top" side self.right right side self.bottom bottom side self.lt left-top point self.rb right-bottom point self.rt = right-top point self.lb left-bottom point A "point" is an x,y coordinate. """ if(self.lt.x > other.rb.x or other.lt.x > self.rb.x): return False elif(self.rb.y < other.lt.y or other.rb.y < self.lt.y): return False else: return True
If you are wondering, that this basically does is check two things:
Is one rectangle above the top edge of the other rectangle?
Is one rectangle on the left side of the left edge of the other rectangle?
If any of those are true, the function returns false.
So, this works fine (if you have any suggestions, let me know. However, now I want to calculate how much "percentage" overlap there is. Essentially, there are some boxes I want to "filter out" if they don't overlap with, say, a 40% percent overlap. I'm not too sure how to do this efficiently thought.
My idea was to find the "Point" of the other box that is inside the self box, then just do some simple math to figure out the area inside that box - but this only works if there is only on point - if there are two or three it gets a bit more complicated. Not too sure how to attack this. Any help would be appreciated!