Rectangle¶
-
class
geometry.
Rect
(x: Union[int, float], y: Union[int, float], width: Union[int, float], height: Union[int, float], user_data: Any = None)¶ An axis-aligned rectangle defined by a center point, width and height. An optional user data object may also be given.
>>> Rect(0, 1, 10, 20).x 0
>>> Rect(0, 1, 10, 20).y 1
>>> Rect(0, 1, 10, 20).width 10
>>> Rect(0, 1, 10, 20).height 20
The string representation has the following meaning:
[<left edge>:<right edge>, <bottom edge>:<top edge>]
>>> Rect(0, 0, 10, 20) [-5:5, -10:10]
>>> Rect(0, 0, 2, 4, 'user data') [-1:1, -2:2] 'user data'
>>> Rect(0, 0, 10, 20).top_left (-5, 10)
>>> Rect(0, 0, 10, 20).bottom_center (0, -10)
-
classmethod
__class_getitem__
(init_tuple: Union[Tuple[Union[slice, int, float], Union[slice, int, float], Any], Tuple[Union[slice, int, float], Union[slice, int, float]]]) → geometry.rect.Rect¶ Construct a rect using slice notation.
>>> Rect[0:2, 0:4] [0:2, 0:4]
>>> Rect[2, 4] [-1:1, -2:2]
>>> Rect[0:2, 0:4, 'red'] [0:2, 0:4] 'red'
-
static
from_size
(width: Union[int, float], height: Union[int, float], user: Any = None) → geometry.rect.Rect¶ Construct a rect with a given width and height. The center point will be (0, 0).
>>> Rect.from_size(4, 6) [-2:2, -3:3]
-
static
from_edges
(left: Union[int, float], right: Union[int, float], bottom: Union[int, float], top: Union[int, float], user_data: Any = None) → geometry.rect.Rect¶ Construct a rect from the given edge coordinates
>>> Rect.from_edges(1, 3, 2, 4) [1:3, 2:4]
-
property
bottom
¶ The bottom edge of the rect.
>>> from geometry import Rect >>> r = Rect[1:2, 3:4] >>> r.bottom 3.0
Assigning the edge translates the whole rect.
>>> r.bottom = 10 >>> r [1:2, 10:11]
-
copy
(user_data: Any = <object object>) → Self¶ Copy the rect or any other shape. You can specify the copy behaviour for the user data object.
The default is a shallow copy
>>> from geometry import Rect >>> r = Rect[0:1, 0:1, ['my', 'user', 'data']] >>> r.copy(Rect.shallow_copy) [0:1, 0:1] ['my', 'user', 'data']
You can force a deep copy
>>> r = Rect[0:1, 0:1, [...]] >>> r.copy(Rect.deep_copy) [0:1, 0:1] [...]
You can pass the user data without copying
>>> r = Rect[0:1, 0:1, [...]] >>> r.copy(Rect.keep) [0:1, 0:1] [...]
And you can supply a completely new user data object
>>> r = Rect[0:1, 0:1, [...]] >>> r.copy('completely new') [0:1, 0:1] 'completely new'
-
intersection
(rect: geometry.rect.Rect) → Optional[geometry.rect.Rect]¶ Calculate the intersecting rectangle between this and the given rectangle.
If the rectangles don’t intersect,
None
is returned+---------+ | | | +----+----+ | | | | +----+----+ | | | +---------+
>>> Rect[0:2, 0:4].intersection(Rect[1:3, 2:6]) [1:2, 2:4]
>>> Rect[0:1, 0:1].intersection(Rect[1:2, 1:2]) is None True
>>> Rect[0:1, 0:1].intersection(Rect[2:3, 2:3]) is None True
-
is_inside_of
(rect: geometry.rect.BaseRect) → bool¶ Check if this rectangle is inside the other rectangle.
>>> Rect[1, 1].is_inside_of(Rect[2, 2]) True
>>> Rect[2, 2].is_inside_of(Rect[2, 2]) True
>>> Rect[1, 2].is_inside_of(Rect[2, 1]) False
You can also use the in operator
>>> Rect[1, 1] in Rect[2, 2] True
>>> Rect[1, 1] not in Rect[2, 2] False
-
property
left
¶ The left edge of the rect.
>>> from geometry import Rect >>> r = Rect[1:2, 3:4] >>> r.left 1.0
Assigning the edge translates the whole rect.
>>> r.left = 10 >>> r [10:11, 3:4]
-
property
long_side
¶ Return the longer side of the rectangle
>>> Rect[0:3, 0:2].long_side 3
>>> Rect[0:2, 0:3].long_side 3
-
property
right
¶ The right edge of the rect.
>>> from geometry import Rect >>> r = Rect[1:2, 3:4] >>> r.right 2.0
Assigning the edge translates the whole rect.
>>> r.right = 10 >>> r [9:10, 3:4]
-
property
short_side
¶ Return the shorter side of the rectangle
>>> Rect[0:3, 0:2].short_side 2
>>> Rect[0:2, 0:3].short_side 2
-
stretch
(*relative: Union[geometry.handles.EdgeHandle, geometry.handles.PointHandle, geometry.handles.MultiHandle], **absolute: Union[int, float, geometry.point.Point]) → Self¶ Stretch the given edges or corners of the rectangle. I.e. move the given edge or corner while leaving the opposite edge or corner intact.
You can stretch the edges to an absolute coordinate.
>>> Rect[2:4, 4:6].stretch(left=0) [0:4, ...]
>>> Rect[2:4, 4:6].stretch(right=6) [2:6, ...]
>>> r = Rect[2:4, 4:6]; r2 = r.copy() >>> r.stretch(left=0, right=6) == r2.stretch(left=0).stretch(right=6) True
>>> Rect[2:4, 4:6].stretch(bottom=0) [2:4, 0:6]
>>> Rect[2:4, 4:6].stretch(top=8) [2:4, 4:8]
You can stretch the edges relatively. Here, positive numbers will always stretch away from the center.
>>> from geometry import left >>> Rect[2:4, 4:6].stretch(left + 1) [1:4, 4:6]
>>> from geometry import right, bottom, top >>> Rect[2:5, 4:7].stretch(left - 1, right - 1, bottom - 1, top - 1) [3:4, 5:6]
It is also possible to stretch the corners like this:
>>> Rect[2:4, 4:6].stretch(bottom_left=Point(0, 0)) [0:4, 0:6]
>>> from geometry import bottom_left >>> Rect[2:4, 4:6].stretch(bottom_left + Point(1, 2)) [1:4, 2:6]
As a shortcut you can also pass a scalar instead of a vector in the relative case.
>>> Rect[2:4, 4:6].stretch(bottom_left + 2) [0:4, 2:6]
Here is another shortcut for stretching every edge out
>>> from geometry import out >>> Rect[2:4, 4:6].stretch(out + 1) [1:5, 3:7]
And inwards:
>>> from geometry import in_ >>> Rect[2:5, 4:7].stretch(in_ + 1) [3:4, 5:6]
-
property
top
¶ The top edge of the rect.
>>> from geometry import Rect >>> r = Rect[1:2, 3:4] >>> r.top 4.0
Assigning the edge translates the whole rect.
>>> r.top = 10 >>> r [1:2, 9:10]
-
translate
(**absolute: Union[int, float, geometry.point.Point, geometry.handles.EdgeHandle, geometry.handles.PointHandle]) → geometry.translate.CanTranslate¶ Translates the whole shape.
>>> from geometry import Rect, right >>> r = Rect[2, 4] >>> r.translate(bottom_left=Point(0, 0)) # move the bottom left to the origin [0:2, 0:4]
>>> r.translate(center=Point(0, 0)) # move the center to the origin [-1:1, -2:2]
>>> r.translate(left=right) # move by 2 (width) to the right [1:3, -2:2]
>>> r.translate(left=right + 1) # move by 3 (width + 1) to the right [4:6, -2:2]
-
union
(rect: geometry.rect.Rect) → Optional[geometry.rect.Rect]¶ Calculate the union rectangle of this and the given rectangle.
+----+-------+ | | | +----+ | | +----+ | | | +-------+----+
>>> Rect[0:1, 0:1].union(Rect[2:3, 2:3]) [0:3, 0:3]
-
classmethod