From 4bcb82c0f295d79d3d247252e7e98b2d986ae821 Mon Sep 17 00:00:00 2001 From: adamhrv Date: Sun, 6 Jan 2019 17:16:18 +0100 Subject: externalize drawing, cleanup --- megapixels/app/models/bbox.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'megapixels/app/models') diff --git a/megapixels/app/models/bbox.py b/megapixels/app/models/bbox.py index 55a92512..04ee4a70 100644 --- a/megapixels/app/models/bbox.py +++ b/megapixels/app/models/bbox.py @@ -29,6 +29,7 @@ class BBox: def __init__(self, x1, y1, x2, y2): """Represents a bounding box and provides methods for accessing and modifying + All values are normalized unless otherwise specified :param x1: normalized left coord :param y1: normalized top coord :param x2: normalized right coord @@ -40,8 +41,8 @@ class BBox: self._y2 = y2 self._width = x2 - x1 self._height = y2 - y1 - self._cx = x1 + (self._width // 2) - self._cy = y1 + (self._height // 2) + self._cx = x1 + (self._width / 2) + self._cy = y1 + (self._height / 2) self._tl = (x1, y1) self._br = (x2, y2) self._rect = (self._x1, self._y1, self._x2, self._y2) @@ -111,7 +112,14 @@ class BBox: # # ----------------------------------------------------------------- # # Utils - # def constrain(self, dim): + def contains(self, pt_norm): + '''Returns Checks if this BBox contains the normalized point + :param pt: (int|float, int|float) x, y + :returns (bool) + ''' + x, y = pt_norm + return (x > self._x1 and x < self._x2 and y > self._y1 and y < self._y2) + def distance(self, b): a = self dcx = self._cx - b.cx @@ -168,6 +176,12 @@ class BBox: # ----------------------------------------------------------------- # Convert to + def to_square(self, bounds): + '''Forces bbox to square dimensions + :param bounds: (int, int) w, h of the image + :returns (BBox) in square ratio + ''' + def to_dim(self, dim): """scale is (w, h) is tuple of dimensions""" w, h = dim -- cgit v1.2.3-70-g09d2