pyclipper

1.4.0 · active · verified Thu Apr 09

Pyclipper is a Cython wrapper that exposes the public functions and classes of the C++ translation of the Angus Johnson's Clipper library (version 6.4.2). It provides robust algorithms for 2D polygon clipping (intersection, union, difference, XOR) and offsetting (inflating/deflating). The library is actively maintained, with the latest version being 1.4.0, and releases occur as needed for updates or Python compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic polygon clipping and offsetting using `pyclipper`. It sets up subject and clip polygons, performs an intersection operation, and then an inward offset on a single polygon. All coordinates are integers, as required by the underlying Clipper library.

import pyclipper

# Subject polygon (a list of paths, where a path is a list of (x, y) tuples)
subj = (
    ((180, 200), (260, 200), (260, 150), (180, 150)),
    ((215, 160), (230, 190), (200, 190))
)

# Clip polygon
clip = ((190, 210), (240, 210), (240, 130), (190, 130))

# Initialize Clipper object
pc = pyclipper.Pyclipper()

# Add paths to the Clipper object
pc.AddPath(clip, pyclipper.PT_CLIP, True) # Clip path is a closed polygon
pc.AddPaths(subj, pyclipper.PT_SUBJECT, True) # Subject paths are closed polygons

# Execute the clipping operation (intersection in this case)
solution = pc.Execute(
    pyclipper.CT_INTERSECTION,
    pyclipper.PFT_EVENODD, # Fill type for subject polygons
    pyclipper.PFT_EVENODD  # Fill type for clip polygons
)

print(f"Intersection solution: {solution}")

# Example of offsetting
subj_offset = ((180, 200), (260, 200), (260, 150), (180, 150))
pco = pyclipper.PyclipperOffset()
pco.AddPath(subj_offset, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
solution_offset = pco.Execute(-7.0) # Offset by -7 units (inward)

print(f"Offset solution: {solution_offset}")

view raw JSON →