Dimension classes

Last modified: September 16, 2022

Contents

This is a reference chapter for the various classes that manage coordinates, dimensions and bounding boxes.

Point

__init__(Int x, Int y)

Point stores an (x, y) coordinate point. On the C++ side, the coordinates are unsigned integers, so that you cannot use negative x or y values.

Most functions that take a Point as an argument can also take a 2-element sequence. For example, the following are all equivalent:

px = image.get(Point(5, 2))
px = image.get((5, 2))
px = image.get([5, 2])

From the numeric operators, only + and += are implemented, because subtraction would cause problems when the result is negative. On the C++ side, the comparison operator < is also implemented, which does not make real sense, but allows Points to be used as keys in maps and sets.

__init__

Point (Int x, Int y)

x

(int property)

The current x value

y

(int property)

The current y value

move

move (x, y)

Moves the point by the given x, y coordinate, i.e. the vector (x, y) is added to the point. The following two lines are equivalent:

p.move(x,y)
p += Point(x,y)

Size

Size stores a size (width, height). This is almost the same as Dim, but stores the number of columns/rows minus one:

width == ncols - 1

height == nrows - 1

__init__

Size (Int width, Int height)

width

(int property)

The width of an object is the right boundary minus the left. This is the same as the number of columns minus one.

height

(int property)

The height of an object is the lower boundary minus the upper boundary (remember that y is counted in the negative direction). This is the same as the number of rows minus one.

Dim

__init__(Int ncols, Int nrows)

Dim stores a dimension (ncols, nrows)

__init__

Dim (Int ncols, Int nrows)

ncols

(int property get/set)

the current number of columns

nrows

(int property get/set)

The current number of rows

Rect

There are a number of ways to initialize a Rect object:

  • Rect (Point upper_left, Point lower_right)
  • Rect (Point upper_left, Size size)
  • Rect (Point upper_left, Dim dim)
  • Rect (Rect rectangle)

The Rect class manages bounding boxes, and has a number of methods to modify and analyse those bounding boxes.

Position and dimensions

ul

(Point property)

The upper-left coordinate of the rectangle in logical coordinate space.

ul_x

(int property)

The left edge of the rectangle in logical coordinate space.

ul_y

(int property)

The upper edge of the rectangle in logical coordinate space.

ur

(Point property)

The upper-right coordinate of the rectangle in logical coordinate space.

ur_x

(int property)

The right edge of the rectangle in logical coordinate space.

ur_y

(int property)

The upper edge of the rectangle in logical coordinate space.

lr

(Point property)

The lower-right coordinate of the rectangle in logical coordinate space.

lr_x

(int property)

The right edge of the rectangle in logical coordinate space.

lr_y

(int property)

The lower edge of the rectangle in logical coordinate space.

ll

(Point property)

The lower-left coordinate of the rectangle in logical coordinate space.

ll_x

(int property)

The left edge of the rectangle in logical coordinate space.

ll_y

(int property)

The lower edge of the rectangle in logical coordinate space.

size

(Size property)

The size of the rectangle. Equivalent to Size(image.width, image.height).

nrows

(int property)

The number of rows in the rectangle.

ncols

(int property)

The number of columns in the rectangle.

width

(int property)

The width of the rectangle. Equivalent to ncols - 1.

height

(int property)

The height of the rectangle. Equivalent to nrows - 1.

offset_x

(int property)

The left edge of the rectangle in the logical coordinate space.

offset_y

(int property)

The upper edge of the rectable in the logical coordinate space.

Center

center

(Point property)

The coordinate at the exact center of the rectangle in the logical coordinate space.

center_x

(int property)

The x-location at the exact center of the rectangle in the logical coordinate space.

center_y

(int property)

The y-location at the exact center of the rectangle in the logical coordinate space.

Containment

contains_x

bool contains_x (Int x)

True if the rectangle contains the given x-value in logical coordinate space.

contains_y

bool contains_y (Int y)

True if the rectangle contains the given y-value in logical coordinate space.

contains_point

bool contains_point (Point point)

True if the rectangle contains the given Point in logical coordinate space

contains_rect

bool contains_rect (Rect other)

True if rectangle completely contains the given rectangle in logical coordinate space.

Intersection

intersects

bool intersects (Rect other)

True if rectangle intersects with the given rectangle.

intersects_x

bool intersects_x (Rect other)

True if rectangle intersects the given rectangle in the x direction (completely ignoring the y direction). (True if the two rectangles are merely "vertically aligned".)

intersects_y

bool intersects_y (Rect other)

True if rectangle intersects the given rectangle in the y direction (completely ignoring the x direction). (True if the two rectangles are merely "horizontally aligned".)

intersection

bool intersection (Rect other)

Returns a new Rect that is the intersection of self and the given Rect object.

Union

union

union (Rect other)

Expands the rectangle to include the given rectangle and itself.

union_rects

Rect union_rects (RectList rects)

Returns a new rectangle that encloses all of the given rectangles in a list.

Distance

distance_euclid

float distance_euclid (Rect other)

Returns the Euclidean distance between the center points of this rectangle and the given rectangle.

distance_bb

float distance_bb (Rect other)

Returns the closest (Euclidean) distance between the edges of this rectangle and the edges of the given rectangle.

distance_cx

int distance_cx (Rect other)

Returns the distance of the center points of this rectangle and the given rectangle in the horizontal direction.

distance_cy

int distance_cy (Rect other)

Returns the distance of the center points of this rectangle and the given rectangle in the vertical direction.