1

I am trying to use Clipper, an open source polygon clipping library, to clip an open polygon with a closed polygon.

I am using the python wrapper of pyclipper. My code is as follows:

import pyclipper

subj = [[-10, 5], [20, 5]]

clip = [[0, 0], [0, 10], [10, 10], [10, 0]]

pc = pyclipper.Pyclipper()

pc.AddPath(clip, pyclipper.PT_CLIP, True)
pc.AddPath(subj, pyclipper.PT_SUBJECT, False)

solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD)

print(solution)

It seems to run up until the Execute function, then it just quits with no error message. What am I doing wrong?

Adam C
  • 167
  • 1
  • 7

1 Answers1

0

try changing your list of list for tuples, and the most important, to don't have a type error, use Pyclipper::Execute2:

import pyclipper

subj = [(-10, 5), (20, 5)]

clip = [(0, 0), (0, 10), (10, 10), (10, 0)]

pc = pyclipper.Pyclipper()

pc.AddPath(clip, pyclipper.PT_CLIP, True)
pc.AddPath(subj, pyclipper.PT_SUBJECT, False)

solution = pc.Execute2(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)


print([i.Contour for i in solution.Childs])

[[[10, 5], [0, 5]]]

the last line is equivalent, in your case, to:

solution = pc.Execute2(pyclipper.CT_INTERSECTION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD)
developer_hatch
  • 15,898
  • 3
  • 42
  • 75