IRect

IRect is a rectangular bounding box similar to Rect, except that all corner coordinates are integers. IRect is used to specify an area of pixels, e.g. to receive image data during rendering. Otherwise, many similarities exist, e.g. considerations concerning emptiness and finiteness of rectangles also apply to IRects.

Attribute / Method Short Description
IRect.contains() checks containment of another object
IRect.getArea() calculate rectangle area
IRect.getRect() return a Rect with same coordinates
IRect.getRectArea() calculate rectangle area
IRect.intersect() common part with another rectangle
IRect.intersects() checks for non-empty intersection
IRect.normalize() makes a rectangle finite
IRect.bottom_left bottom left point, synonym bl
IRect.bottom_right bottom right point, synonym br
IRect.height height of the rectangle
IRect.isEmpty whether rectangle is empty
IRect.isInfinite whether rectangle is infinite
IRect.rect equals result of method getRect()
IRect.top_left top left point, synonym tl
IRect.top_right top_right point, synonym tr
IRect.width width of the rectangle
IRect.x0 X-coordinate of the top left corner
IRect.x1 X-coordinate of the bottom right corner
IRect.y0 Y-coordinate of the top left corner
IRect.y1 Y-coordinate of the bottom right corner

Class API

class IRect
__init__(self)
__init__(self, x0, y0, x1, y1)
__init__(self, irect)
__init__(self, list)

Overloaded constructors. Also see examples below and those for the Rect class.

If another irect is specified, a new copy will be made.

If list is specified, it must be a Python sequence type of 4 integers. Non-integer numbers will be truncated, non-numeric entries will raise an exception.

The other parameters mean integer coordinates.

getRect()

A convenience function returning a Rect with the same coordinates. Also available as attribute rect.

Return type:Rect
getRectArea([unit])
getArea([unit])

Calculates the area of the rectangle and, with no parameter, equals abs(IRect). Like an empty rectangle, the area of an infinite rectangle is also zero.

Parameters:unit (str) – Specify required unit: respective squares of px (pixels, default), in (inches), cm (centimeters), or mm (millimeters).
Return type:float
intersect(ir)

The intersection (common rectangular area) of the current rectangle and ir is calculated and replaces the current rectangle. If either rectangle is empty, the result is also empty. If one of the rectangles is infinite, the other one is taken as the result - and hence also infinite if both rectangles were infinite.

Parameters:ir (IRect) – Second rectangle.
contains(x)

Checks whether x is contained in the rectangle. It may be an IRect, Rect,``Point`` or number. If x is an empty rectangle, this is always true. Conversely, if the rectangle is empty this is always False, if x is not an empty rectangle and not a number. If x is a number, it will be checked to be one of the four components. x in irect and irect.contains(x) are equivalent.

Parameters:x (IRect or Rect or Point or int) – the object to check.
Return type:bool
intersects(r)

Checks whether the rectangle and r (IRect or Rect) have a non-empty rectangle in common. This will always be False if either is infinite or empty.

Parameters:r (IRect or Rect) – the rectangle to check.
Return type:bool
normalize()

Make the rectangle finite. This is done by shuffling rectangle corners. After this, the bottom right corner will indeed be south-eastern to the top left one. See Rect for a more details.

top_left
tl

Equals Point(x0, y0).

Type:Point
top_right
tr

Equals Point(x1, y0).

Type:Point
bottom_left
bl

Equals Point(x0, y1).

Type:Point
bottom_right
br

Equals Point(x1, y1).

Type:Point
width

Contains the width of the bounding box. Equals x1 - x0.

Type:int
height

Contains the height of the bounding box. Equals y1 - y0.

Type:int
x0

X-coordinate of the left corners.

Type:int
y0

Y-coordinate of the top corners.

Type:int
x1

X-coordinate of the right corners.

Type:int
y1

Y-coordinate of the bottom corners.

Type:int
isInfinite

True if rectangle is infinite, False otherwise.

Type:bool
isEmpty

True if rectangle is empty, False otherwise.

Type:bool

Remark

A rectangle’s coordinates can also be accessed via index, e.g. r.x0 == r[0], and the tuple() and list() functions yield sequence objects of its components.

IRect Algebra

Algebra provides handy ways to perform inclusion and intersection checks between Rects, IRects and Points. For a general background, see chapter Operator Algebra for Geometry Objects.

Examples

Example 1:

>>> ir = fitz.IRect(10, 10, 410, 610)
>>> ir
fitz.IRect(10, 10, 410, 610)
>>> ir.height
600
>>> ir.width
400
>>> ir.getArea('mm')     # calculate area in square millimeters
29868.51852

Example 2:

>>> m = fitz.Matrix(45)
>>> ir = fitz.IRect(10, 10, 410, 610)
>>> ir * m                          # rotate rectangle by 45 degrees
fitz.IRect(-425, 14, 283, 722)
>>>
>>> ir | fitz.Point(5, 5)           # enlarge rectangle to contain a point
fitz.IRect(5, 5, 410, 610)
>>>
>>> ir + 5                          # shift the rect by 5 points
fitz.IRect(15, 15, 415, 615)
>>>
>>> ir & fitz.Rect(0.0, 0.0, 15.0, 15.0)
fitz.IRect(10, 10, 15, 15)
>>> ir /= (1, 2, 3, 4, 5, 6)        # divide by a matrix
>>> ir
fitz.IRect(-14, 0, 4, 8)

Example 3:

>>> # test whether two rectangle are disjoint
>>> if not r1.intersects(r2): print("disjoint rectangles")
>>>
>>> # test whether r2 containes x (x is point-like or rect-like)
>>> if r2.contains(x): print("x is contained in r2")
>>>
>>> # or even simpler:
>>> if x in r2: print("x is contained in r2")