Skip to content

Commit 2dacbb6

Browse files
authored
Merge pull request #42 from anthrotype/restore-starting-point
[flatten] record position of contour's starting point and restore if possible
2 parents 9523a83 + 7482ba0 commit 2dacbb6

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Lib/booleanOperations/flatten.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class ContourPointDataPen:
306306

307307
def __init__(self):
308308
self._points = None
309+
self._foundStartingPoint = False
309310

310311
def getData(self):
311312
"""
@@ -343,6 +344,8 @@ def endPath(self):
343344

344345
def addPoint(self, pt, segmentType=None, smooth=False, name=None, **kwargs):
345346
assert segmentType != "move"
347+
if not self._foundStartingPoint and segmentType is not None:
348+
kwargs['startingPoint'] = self._foundStartingPoint = True
346349
data = InputPoint(
347350
coordinates=pt,
348351
segmentType=segmentType,
@@ -970,11 +973,18 @@ def drawPoints(self, pointPen):
970973
points.extend(segment.points)
971974

972975
hasOnCurve = False
973-
for point in points:
976+
originalStartingPoints = []
977+
for index, point in enumerate(points):
974978
if point.segmentType is not None:
975979
hasOnCurve = True
976-
break
977-
if hasOnCurve:
980+
if point.kwargs is not None and point.kwargs.get("startingPoint"):
981+
distanceFromOrigin = math.hypot(*point)
982+
originalStartingPoints.append((distanceFromOrigin, index))
983+
if originalStartingPoints:
984+
# use the original starting point that is closest to the origin
985+
startingPointIndex = sorted(originalStartingPoints)[0][1]
986+
points = points[startingPointIndex:] + points[:startingPointIndex]
987+
elif hasOnCurve:
978988
while points[0].segmentType is None:
979989
p = points.pop(0)
980990
points.append(p)

0 commit comments

Comments
 (0)