Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created December 7, 2018 20:14
Show Gist options
  • Select an option

  • Save typemytype/4ad64ad5d4f72b6662d74f418cd67c44 to your computer and use it in GitHub Desktop.

Select an option

Save typemytype/4ad64ad5d4f72b6662d74f418cd67c44 to your computer and use it in GitHub Desktop.
import pyclipper
from booleanOperations.booleanOperationManager import _addContour, _operationMap, _fillTypeMap
def clipExecute(subjectContours, clipContours, operation, subjectFillType="nonZero",
clipFillType="nonZero", addBoundsTest=True):
pc = pyclipper.Pyclipper()
for i, subjectContour in enumerate(subjectContours):
_addContour(clipperPath=pc, contour=subjectContour, fillType=pyclipper.PT_SUBJECT, contourCount=i)
for j, clipContour in enumerate(clipContours):
_addContour(clipperPath=pc, contour=clipContour, fillType=pyclipper.PT_CLIP, contourCount=i)
if addBoundsTest:
bounds = pc.GetBounds()
if (bounds.bottom, bounds.left, bounds.top, bounds.right) == (0, 0, 0, 0):
# do nothing if there are no paths
return []
try:
solution = pc.Execute(_operationMap[operation],
_fillTypeMap[subjectFillType],
_fillTypeMap[clipFillType])
except pyclipper.ClipperException as exc:
raise ExecutionError(exc)
return [[tuple(p) for p in path] for path in solution]
glyph = CurrentGlyph()
subjectContours = []
for contour in glyph:
subjectContours.append([(p.x, p.y) for p in contour.points])
r = pyclipper.Area(subjectContours[-1])
import timeit
NUMBER = 1000
def bounds():
clipExecute(subjectContours, [], "union")
r = timeit.Timer(bounds).timeit(number=NUMBER)
print(r)
def nobounds():
clipExecute(subjectContours, [], "union", addBoundsTest=False)
r = timeit.Timer(nobounds).timeit(number=NUMBER)
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment