Skip to main content

A library for unions, intersections, subtractions, and xors of rectangles.

Project description

rects

Tests

A library for unions, intersections, subtractions, and xors of rectangles.

The result of set operations on two rects are shown by the green regions below:

rects

Regions

Let's say we have the following rects:

    +------------+   +--------+
    |            |   |    B   |
    |        +-----------+    |
    |    A   |     C     |    |
    |        +-----------+----+
    |            |
    +------------+

And we want to represent the region A + B - C:

    +------------+   +--------+
    |            |   |        |
    |        +---+   +---+    |
    |        |           |    |
    |        +---+       +----+
    |            |
    +------------+

One method is to divide the area into a series of mutually exclusive horizontal bands:

    +------------+   +--------+
    |            |   |        |
    +--------+---+   +---+----+
    | a      | b         | c  | d   <- 2nd band with walls at a, b, c, d.
    +--------+---+       +----+
    |            |
    +------------+

Each band is a vertical interval and a list of walls. Each contiguous pair of walls indicates a new rect in the band. A Region is a list of sorted, mutually exclusive bands.

Using rects

To use rects, construct an initial Region, r, from some rect and iteratively +, -, &, or ^ with other regions:

>>> from rects import *
>>> r = Region.from_rect(Rect(0, 0, 100, 200))
>>> s = Region.from_rect(Rect(10, 25, 40, 75))
>>> t = Region.from_rect(Rect(45, 75, 50, 125))
>>> r
Region(bands=[Band(y1=0, y2=100, walls=[0, 200])])
>>> v = r - s - t
>>> v
Region(bands=[
    Band(y1=0, y2=10, walls=[0, 200]),
    Band(y1=10, y2=45, walls=[0, 25, 100, 200]),
    Band(y1=45, y2=50, walls=[0, 25]),
    Band(y1=50, y2=95, walls=[0, 75]),
    Band(y1=95, y2=100, walls=[0, 200])
])
>>> list(v.rects())
[
    Rect(y=0, x=0, height=10, width=200),
    Rect(y=10, x=0, height=35, width=25),
    Rect(y=10, x=100, height=35, width=100),
    Rect(y=45, x=0, height=5, width=25),
    Rect(y=50, x=0, height=45, width=75),
    Rect(y=95, x=0, height=5, width=200)
]
>>> v.bbox
Rect(y=0, x=0, height=100, width=200)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rects-0.2.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

rects-0.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file rects-0.2.0.tar.gz.

File metadata

  • Download URL: rects-0.2.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for rects-0.2.0.tar.gz
Algorithm Hash digest
SHA256 18dd9a16b9acca259b65750a51255b5a7529454ef2cbd605db793c9585636435
MD5 d41727947e23ca02f89aaa7bf8f45f8d
BLAKE2b-256 16309cdca35a327d59e11ba72074fe0327914a40d0ac1abf36a8e370b220703e

See more details on using hashes here.

File details

Details for the file rects-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: rects-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for rects-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 367b085b6f2b4cbaa09d1a1d6464856e4d80f719f0f86c0d79b289d501bb17e5
MD5 091c8663b55fa4695879ff96b93fb381
BLAKE2b-256 a7767479dba4c2e0c6aa7976d3691e14aaa2424c4d757783697751763c270b03

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page