Prev Up Next
Point Objects Coordinate Systems Transformation Objects

Rect Objects

Rect objects, or rects for short, represent rectangular areas of the drawing whose edges are parallel to the coordinate axes. They are represented by four floats for the positions of the left, bottom, right and top edges. Rects are kept in a normalized state where the left coordinate a less than the right coordinate and the bottom coordinate less than the top coordinate.

The bounding rects and coord rects of the graphics objects are such rect objects.

Rect objects provide efficient methods for boolean operations (union and intersection of rects) and tests for overlapping rects and contained points.

There are two special rect objects: InfinityRect and EmptyRect. InfinityRect represents the entire 2D plane while EmptyRect is an empty rect. These objects behave in special ways: both overlap every other rect, InfinityRect contains all points while EmptyRect contains no point, and so on. The particular coordinates of these objects are irrelevant.

Rect objects are immutable.

Note: these rect objects have nothing to do with the rectangle primitive (apart from the fact that the rectangle primitive has a bounding box...).


Rect(left, bottom, right, top)
Rect(llcorner, urcorner)

Return a new Rect object with the coordinates left, bottom, right and top or the opposite corners llcorner and urcorner given as points objects. The rectangle is automatically normalized, so you can swap left/right or top/bottom or choose any opposite corners.


Return the smallest rect that contains all points in sequence. sequence must be a sequence of PointSpecs. If sequence is empty return EmptyRect


A rect object has four (read only) attributes:


The left, bottom, right and top coordinate of the rect as python floats. For InfinityRect and EmptyRect these have no real meaning.


A rect object has these methods (self refers to the rect whose method is called):

contains_point(x, y)

Return true, if the point given by PointSpec or x and y is inside of self, false otherwise.

For InfinityRect this is always true, for EmptyRect always false.


Return true, if the rect r lies completely inside of self. A rect contains itself.

InfinityRect contains all other rects and EmptyRect is contained in all other rects.


Return true if self and the rect r overlap. A rect overlaps itself.

InfinityRect and EmptyRect overlap all other rects and each other.

translated(x, y)

Return self translated by the vector PointSpec or x and y. Equivalent to

Rect(self.left + x, self.bottom + y, self.right + x, + y)

For InfinityRect and EmptyRect, return self.


Return a rect that is larger than self by amount in every direction. Equivalent to

Rect(self.left - amount, self.bottom - amount, self.right + amount, + amount)

For InfinityRect and EmptyRect, return self.


Return the center point of self as a point object.

For InfinityRect and EmptyRect, return Point(0, 0).


Rect objects implement the sequence protocol for Python objects. This allows the following operations:


Always returns 4.


If i is one of 0, 1, 2 or 3, this is the same as rect.left rect.bottom, rect.right or respectively.

For other values of i raise an IndexError exception.


Return the coordinates of rect as a tuple

(rect.left, rect.bottom, rect.right,

left, bottom, right, top = rect

Unpack the rect rect. Equivalent to

left, bottom, right, top = tuple(rect)

rect is a rect object, len is the builtin function.


UnionRects(rect1, rect2)

Return the union of the rects rect1 and rect2. The union is the smallest rect containing both rects.

If one the arguments is InfinityRect return InfinityRect, if one of the arguments is EmptyRect return the other argument.

IntersectRects(rect1, rect2)

Return the intersection of the rects rect1 and rect2. The intersection is the largest rect contained in both rects.

If one the arguments is EmptyRect return EmptyRect, if one of the arguments is InfinityRect return the other argument.



Represents the entire 2D plane.


The empty rect.


Rect(0, 0, 1, 1)


The rect type object.

Point Objects Coordinate Systems Transformation Objects
Prev Up Next