Developer's Guide: Rect Objects

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...).

### Constructors

`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.

`PointsToRect(sequence)`

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

### Attributes

A rect object has four (read only) attributes:

`left`
`bottom`
`right`
`top`

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

### Methods

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

`contains_point(PointSpec)`
`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.

`contains_rect(r)`

  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.

`overlaps(r)`

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

  `InfinityRect` and `EmptyRect` overlap all other rects and each other.

`translated(PointSpec)`
`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, self.top + y)

  For `InfinityRect` and `EmptyRect`, return self.

`grown(amount)`

  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, self.top + amount)`

  For `InfinityRect` and `EmptyRect`, return self.

`center()`

  Return the center point of self as a point object.

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

### Operators

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

`len(rect)`

  Always returns 4.

`rect[i]`

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

  For other values of i raise an IndexError exception.

`tuple(rect)`

  Return the coordinates of rect as a tuple

  `(rect.left, rect.bottom, rect.right, rect.top)`.

`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.

### Functions

`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.

### Constants

`InfinityRect`

  Represents the entire 2D plane.

`EmptyRect`

  The empty rect.

`UnitRect`

  `Rect(0, 0, 1, 1)`

`RectType`

  The rect type object.

Point Objects | Coordinate Systems | Transformation Objects