Box¶
A Box is a rectangular area defined by two coordinates:
- the top-left corner of the rectangle: the min coord,
- the bottom-right corner of the rectangle: the max coord.
To instantiate a Box
, you can do:
>>> b1 = Box(Coord(5, 6), Coord(7, 8))
>>> b2 = Box(Coord(5, 6))
>>> b3 = Box(1, 2, 2, 3)
>>> b4 = Box(1, 2)
>>> b5 = Box(b1)
Box objects have a string representation à la Excel:
>>> for box in b1, b2, b3, b4, b5:
... print(box)
E6:G8
E6
A2:B3
A2
E6:G8
You can calculate the width and height of boxes:
>>> b1 = Box(Coord(5, 6), Coord(6, 8))
>>> b1.width, b1.height
(2, 3)
>>> b2 = Box(Coord(5, 6))
>>> b2.width, b2.height
(1, 1)
You can determine if a Coord is included in a Box:
>>> top_left = Coord(5, 6)
>>> top_right = Coord(6, 6)
>>> bottom_left = Coord(5, 8)
>>> bottom_right = Coord(6, 8)
>>> b1 = Box(top_left, bottom_right)
>>> top_left in b1
True
>>> top_right in b1
True
>>> bottom_left in b1
True
>>> bottom_right in b1
True
>>> Coord(7, 6) in b1
False
>>> (5, 7) in b1
True
You can determine if two boxes intersect each other, or are disjoints:
>>> b1 = Box(Coord(5, 6), Coord(6, 8))
>>> b2 = Box(Coord(6, 6), Coord(6, 7))
>>> b3 = Box(Coord(7, 6), Coord(7, 8))
>>> b2.intersect(b3)
False
>>> b1.isdisjoint(b2)
False
>>> b2.isdisjoint(b1)
False
>>> b1.isdisjoint(b3)
True
>>> b3.isdisjoint(b1)
True
-
class
benker.box.
Box
¶ Bases:
benker.box.BoxTuple
A Box is a rectangular area defined by two coordinates:
- the top-left corner of the rectangle: the min coord,
- the bottom-right corner of the rectangle: the max coord.
Usage:
>>> from benker.box import Box >>> box = Box(1, 1, 5, 3) >>> box Box(min=Coord(x=1, y=1), max=Coord(x=5, y=3))
-
height
¶
-
intersect
(that)¶
-
intersection
(*others)¶ Return the intersection of self and all the boxes.
Usage:
>>> from benker.box import Box >>> from benker.coord import Coord >>> b1 = Box(Coord(3, 2), Coord(6, 4)) >>> b2 = Box(Coord(4, 3), Coord(5, 7)) >>> b1.intersection(b2) Box(min=Coord(x=4, y=3), max=Coord(x=5, y=4)) >>> b1 & b2 Box(min=Coord(x=4, y=3), max=Coord(x=5, y=4))
Parameters: others – collections of boxes Returns: The inner box of all the boxes. Raises: ValueError – if the two boxes are disjoint.
-
isdisjoint
(that)¶
-
move_to
(coord)¶
-
resize
(size)¶
-
size
¶
-
transform
(coord=None, size=None)¶
-
union
(*others)¶ Return the union of self and all the boxes.
Usage:
>>> from benker.box import Box >>> from benker.coord import Coord >>> b1 = Box(Coord(3, 2), Coord(6, 4)) >>> b2 = Box(Coord(4, 3), Coord(5, 7)) >>> b1.union(b2) Box(min=Coord(x=3, y=2), max=Coord(x=6, y=7)) >>> b1 | b2 Box(min=Coord(x=3, y=2), max=Coord(x=6, y=7))
Parameters: others – collections of boxes Returns: The bounding box of all the boxes.
-
width
¶