Last active
April 24, 2022 20:45
-
-
Save marcbln/c2c8df577556b979253a182f5be2e8d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# demo of polygon shrinking / growing (called inward/outward polygon offseting) | |
# using pyclipper | |
# 04/2022 created | |
from typing import List, Tuple | |
import pyclipper | |
import matplotlib.pyplot as plt | |
def plotPolygon(points, color='r', lineWidth=1): | |
x, y = map(list, zip(*points)) | |
x.append(x[0]) | |
y.append(y[0]) | |
plt.plot(x, y, marker='o', color=color, linewidth=lineWidth) | |
def plotPolygons(polgons, color, lineWidth): | |
for pl in polgons: | |
plotPolygon(pl, color, lineWidth=lineWidth) | |
# ---------- MAIN ------------ | |
point_list = ( | |
(348, 257), (364, 148), (322, 148), (326, 241), | |
(295, 219), (258, 88), (440, 129), (370, 196), | |
(472, 275) | |
) | |
pco = pyclipper.PyclipperOffset() | |
pco.AddPath(point_list, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON) | |
contours_list_shrink: List[List[Tuple[int, int]]] = pco.Execute(-20.0) # "-" --> shrink the outline | |
contours_list_grow: List[List[Tuple[int, int]]] = pco.Execute(20.0) # grow the outline | |
# ---- plot | |
plotPolygon(point_list, 'b', 1) | |
plotPolygons(contours_list_shrink, 'r', 3) | |
plotPolygons(contours_list_grow, 'orange', 3) | |
plt.axis("off") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment