Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import pyclipper
"""
General Suggestions:
- Contours should only be sent here if they actually overlap.
This can be checked easily using contour bounds.
- Only perform operations on closed contours.
- contours must have an on curve point
- some kind of a log
"""
_operationMap = {
"union": pyclipper.CT_UNION,
"intersection": pyclipper.CT_INTERSECTION,
"difference": pyclipper.CT_DIFFERENCE,
"xor": pyclipper.CT_XOR,
}
_fillTypeMap = {
"evenOdd": pyclipper.PFT_EVENODD,
"nonZero": pyclipper.PFT_NONZERO,
# we keep the misspelling for compatibility with earlier versions
"noneZero": pyclipper.PFT_NONZERO,
}
def clipExecute(subjectContours, clipContours, operation, subjectFillType="nonZero",
clipFillType="nonZero"):
pc = pyclipper.Pyclipper()
polygon1 : list of arrays
Vertices of the second polygon in counterclockwise order.
Returns
-------
intersection : list of arrays
Vertices of the intersection in counterclockwise order.
"""
from pyclipper import Pyclipper, PT_CLIP, PT_SUBJECT, CT_INTERSECTION
from pyclipper import scale_to_clipper, scale_from_clipper
# could be accelerated by removing the scale_to/from_clipper()
subj, clip = (polygon1,), polygon2
pc = Pyclipper()
pc.AddPath(scale_to_clipper(clip), PT_CLIP)
pc.AddPaths(scale_to_clipper(subj), PT_SUBJECT)
solution = pc.Execute(CT_INTERSECTION)
if not solution:
return []
return scale_from_clipper(solution)[0]
def clip_poly_pair(pc, p, q):
""""
pc: an instance of pyclipper.Pyclipper.
p: the polygon by which to clip other polygon q.
pc and p may be held fixed through use of functools.partial so that
multiple q may be clipped by p.
"""
pc.Clear()
pc.AddPath(q, pyclipper.PT_SUBJECT, True)
pc.AddPath(p, pyclipper.PT_CLIP, True)
clip_polys = pc.Execute(clip_type=pyclipper.CT_INTERSECTION)
return clip_polys
def clip(subj, clip_paths, subj_closed=True):
if not subj:
return []
if not clip_paths:
return []
pc = pyclipper.Pyclipper()
if subj:
subj = pyclipper.scale_to_clipper(subj, SCALING_FACTOR)
pc.AddPaths(subj, pyclipper.PT_SUBJECT, subj_closed)
if clip_paths:
clip_paths = pyclipper.scale_to_clipper(clip_paths, SCALING_FACTOR)
pc.AddPaths(clip_paths, pyclipper.PT_CLIP, True)
out_tree = pc.Execute2(pyclipper.CT_INTERSECTION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD)
outpaths = pyclipper.PolyTreeToPaths(out_tree)
outpaths = pyclipper.scale_from_clipper(outpaths, SCALING_FACTOR)
return outpaths
polygon1 : list of arrays
Vertices of the second polygon in counterclockwise order.
Returns
-------
intersection : list of arrays
Vertices of the intersection in counterclockwise order.
"""
from pyclipper import Pyclipper, PT_CLIP, PT_SUBJECT, CT_INTERSECTION
from pyclipper import scale_to_clipper, scale_from_clipper
# could be accelerated by removing the scale_to/from_clipper()
subj, clip = (polygon1,), polygon2
pc = Pyclipper()
pc.AddPath(scale_to_clipper(clip), PT_CLIP)
pc.AddPaths(scale_to_clipper(subj), PT_SUBJECT)
solution = pc.Execute(CT_INTERSECTION)
if not solution:
return []
return scale_from_clipper(solution)[0]