diff --git a/lib/helpers.py b/lib/helpers.py index 7f7486b6f..95073cdc3 100644 --- a/lib/helpers.py +++ b/lib/helpers.py @@ -118,6 +118,12 @@ def expand(self, border): self.x -= border self.y -= border + def expanded(self, border): + """Return a copy of this rectangle, expanded by a fixed border size.""" + copy = self.copy() + copy.expand(border) + return copy + def contains(self, other): """Returns true if this rectangle entirely contains another.""" return ( @@ -127,6 +133,17 @@ def contains(self, other): other.y + other.h <= self.y + self.h ) + def contains_pixel(self, x, y): + """Checks if pixel coordinates lie inside this rectangle""" + return (self.x <= x <= self.x + self.w - 1 and + self.y <= y <= self.y + self.h - 1) + + def clamped_point(self, x, y): + """Returns the given point, clamped to the area of this rectangle""" + cx = clamp(x, self.x, self.x + self.w) + cy = clamp(y, self.y, self.y + self.h) + return cx, cy + def __eq__(self, other): """Returns true if this rectangle is identical to another.""" try: