Skip to content

Commit c83d3bc

Browse files
authored
Merge pull request #65 from skef/prevint
Correct for (rare) bad tracking of previousIntersectionPoint
2 parents 0d83efc + 88e2afb commit c83d3bc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Lib/booleanOperations/flatten.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,29 @@ def tValueForPoint(self, point):
255255
else:
256256
raise NotImplementedError
257257

258+
def tValueToPoint(self, t):
259+
if self.segmentType == "curve":
260+
on1 = self.previousOnCurve
261+
off1 = self.points[0].coordinates
262+
off2 = self.points[1].coordinates
263+
on2 = self.points[2].coordinates
264+
return _getCubicPoint(t, on1, off1, off2, on2)
265+
elif self.segmentType == "line":
266+
return _getLinePoint(t, self.previousOnCurve, self.points[0].coordinates)
267+
elif self.segmentType == "qcurve":
268+
raise NotImplementedError
269+
else:
270+
raise NotImplementedError
271+
272+
def hasPoint(self, p):
273+
if p is None:
274+
return False
275+
for t in self.tValueForPoint(p):
276+
pp = self.tValueToPoint(t)
277+
if _distance(p, pp) < _approximateSegmentLength/100:
278+
return True
279+
return False
280+
258281

259282
class InputPoint:
260283

@@ -816,6 +839,13 @@ def reCurveSubSegments(self, inputContours):
816839
continue
817840
tValues = None
818841
lastPointWithAttributes = None
842+
# Occasionally our logic about the previous intersection
843+
# point can drift out of sync with the current segment.
844+
# So check here if it is on the current segment and if
845+
# not set it to None
846+
if not inputSegment.hasPoint(previousIntersectionPoint):
847+
previousIntersectionPoint = None
848+
819849
if flatSegment[0] == inputSegment.flat[0] and flatSegment[-1] != inputSegment.flat[-1]:
820850
# needed the first part of the segment
821851
# if previousIntersectionPoint is None:
@@ -1137,6 +1167,12 @@ def _mid(pt1, pt2):
11371167
(x0, y0), (x1, y1) = pt1, pt2
11381168
return 0.5 * (x0 + x1), 0.5 * (y0 + y1)
11391169

1170+
def _getLinePoint(t, pt0, pt1):
1171+
if t == 0:
1172+
return pt0
1173+
if t == 1:
1174+
return pt1
1175+
return pt0[0] + (pt1[0]-pt0[0]) * t, pt0[1] + (pt1[1]-pt0[1]) * t
11401176

11411177
def _getCubicPoint(t, pt0, pt1, pt2, pt3):
11421178
if t == 0:

0 commit comments

Comments
 (0)