Skip to content

Commit

Permalink
lib.helpers.Rect: add new utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jplloyd committed Jul 29, 2020
1 parent 0ab6f4e commit ffbfca2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/helpers.py
Expand Up @@ -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 (
Expand All @@ -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:
Expand Down

0 comments on commit ffbfca2

Please sign in to comment.