diff options
| author | George Steel <george.steel@gmail.com> | 2023-01-29 14:44:43 -0500 |
|---|---|---|
| committer | George Steel <george.steel@gmail.com> | 2023-01-29 14:44:43 -0500 |
| commit | 68848365b7398d9ef3dfb1f1dc10a3567da5dd69 (patch) | |
| tree | 832a7f017edd5539d17dde41f2a696fa234cc538 /lib | |
| parent | c2c256727bc0c6663d553024783941e25448e06c (diff) | |
tidy path_to_curves
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/stitches/running_stitch.py | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/stitches/running_stitch.py b/lib/stitches/running_stitch.py index 13083149..f3ca8a29 100644 --- a/lib/stitches/running_stitch.py +++ b/lib/stitches/running_stitch.py @@ -183,7 +183,7 @@ def take_stitch(start: Point, points: typing.Sequence[Point], idx: int, stitch_l return points[-1], None -def stitch_curve_even(points: typing.Sequence[Point], stitch_length: float, tolerance: float): +def stitch_curve_evenly(points: typing.Sequence[Point], stitch_length: float, tolerance: float): # Will split a straight line into even-length stitches while still handling curves correctly. # Includes end point but not start point. if len(points) < 2: @@ -216,23 +216,29 @@ def path_to_curves(points: typing.List[Point], min_len: float): if len(points) < 3: return [points] curves = [] + last = 0 last_seg = points[1] - points[0] seg_len = last_seg.length() for i in range(1, len(points) - 1): + # vectors of the last and next segments a = last_seg b = points[i + 1] - points[i] aabb = (a * a) * (b * b) abab = (a * b) * abs(a * b) + + # Test if the turn angle from vectors a to b is more than 45 degrees + # Uses the property of inner products that abab = ± aabb * cos(angle(a,b))**2 if aabb > 0 and abab < 0.5 * aabb: - # inner angle of at most 135 deg if seg_len >= min_len: curves.append(points[last: i + 1]) last = i seg_len = 0 + if b * b > 0: last_seg = b seg_len += b.length() + curves.append(points[last:]) return curves @@ -242,7 +248,7 @@ def running_stitch(points, stitch_length, tolerance): stitches = [points[0]] for curve in path_to_curves(points, 2 * tolerance): # segments longer than twice the tollerance will usually be forced by it, so set that as the minimum for corner detection - stitches.extend(stitch_curve_even(curve, stitch_length, tolerance)) + stitches.extend(stitch_curve_evenly(curve, stitch_length, tolerance)) return stitches |
