Searching the internet has not given a satisfactory solution for the following problem. Given a class Rectangle
defined as the following:
class Rectangle:
def __init__(self, x1, y1, x2, y2):
if x1 > x2 or y1 > y2:
raise ValueError('coordinates are invalid')
self.x1, self.y1, self.x2, self.y2 = x1, y1, x2, y2
@property
def width(self):
return self.x2 - self.x1
@property
def height(self):
return self.y2 - self.y1
def bounds(self, other):
return Rectangle(min(self.x1, other.x1), min(self.y1, other.y1),
max(self.x2, other.x2), max(self.y2, other.y2))
def intersect(self, other):
return self.x1 < other.x2 and self.x2 > other.x1 and \
self.y1 < other.y2 and self.y2 > other.y1
How would you create a method to get the intersection and a generator to get the difference of two rectangles? Presumably, a more complete implementation of the following methods are needed, but it is not clear to me what should be written.
def __and__(self, other):
if self.intersect(other):
# return a new rectangle that provides
# the intersection between self and other
return None
def __sub__(self, other):
take_away = self & other
if take_away is None:
return self
if take_away is self:
return None
return self.get_partitions(take_away)
def get_partitions(self, take_away):
# yield 1 or 3 rectangles that are not part of take_away
# alternative:
# yield 1 or 2 overlapping rectangles not part of take_away
Does anyone have an elegant implementation for these methods? My hope is to avoid writing code for every conceivable case that might be encountered.
Here is a complete solution for you.
Methods in the class are ordered illogically so that the important parts are visible without scrolling.
Intersection is based on SFML's implementation. It is proven correct and is not interesting to explain.
The difference, however, was a lot of fun to make.
Consider the following cases and compare them with corresponding examples at the bottom of the code. The method may return from 0 to 8 rectangles!
It works by finding all the vertical (
xs
) and horizontal (ys
) lines that go through our rectangle (all the black and grey lines on the picture).The coordinate sets are turned into
sorted
lists and takenpairwise
([a, b, c]
becomes[(a, b), (b, c)]
).The
product
of such horizontal and vertical segments gives us all the rectangles that we divided the original one into by these lines.All that remains is to
yield
all of these rectangles except the intersection.Oleh Prypin was extremely helpful with the provided code. The following is a refactored version of the same.
For the complete program the
Rectangle
class was used in and as a practical example of its usage: