diff --git a/src/beziers/cubicbezier.py b/src/beziers/cubicbezier.py index 4eed4c2..9bc9d0a 100644 --- a/src/beziers/cubicbezier.py +++ b/src/beziers/cubicbezier.py @@ -117,9 +117,9 @@ def flatten(self, degree=8) -> List[Line]: return [Line(self[0], self[3])] samples = self.regularSample(self.length / degree) for i in range(1, len(samples)): - l = Line(samples[i - 1], samples[i]) - l._orig = self - ss.append(l) + line = Line(samples[i - 1], samples[i]) + line._orig = self + ss.append(line) return ss def _findRoots(self, dimension: str) -> List[float]: @@ -268,10 +268,10 @@ def hasLoop(self) -> bool: d3 = 3 * a3 d2 = d3 - a2 d1 = d2 - a2 + a1 - l = math.sqrt(d1 * d1 + d2 * d2 + d3 * d3) + distance = math.sqrt(d1 * d1 + d2 * d2 + d3 * d3) s = 0 - if l != 0: - s = 1 / l + if distance != 0: + s = 1 / distance d1 *= s d2 *= s d3 *= s diff --git a/src/beziers/path/__init__.py b/src/beziers/path/__init__.py index 7bf6eb7..cd8f9b6 100644 --- a/src/beziers/path/__init__.py +++ b/src/beziers/path/__init__.py @@ -220,7 +220,7 @@ def plot(self, ax, **kwargs): kwargs["lw"] = 2 if "fill" not in kwargs: kwargs["fill"] = False - drawNodes = "drawNodes" not in kwargs or kwargs["drawNodes"] != False + drawNodes = "drawNodes" not in kwargs or kwargs["drawNodes"] is not False if "drawNodes" in kwargs: kwargs.pop("drawNodes") patch = patches.PathPatch(path, **kwargs) @@ -236,31 +236,30 @@ def plot(self, ax, **kwargs): ax.set_ylim(bounds.bottom, bounds.top) if drawNodes: nl = self.asNodelist() - for i in range(0, len(nl)): - n = nl[i] + for i, n in enumerate(nl): if n.type == "offcurve": circle = plt.Circle( (n.x, n.y), 2, fill=True, color="black", alpha=0.5 ) ax.add_artist(circle) if i + 1 < len(nl) and nl[i + 1].type != "offcurve": - l = Line2D( + line = Line2D( [n.x, nl[i + 1].x], [n.y, nl[i + 1].y], linewidth=2, color="black", alpha=0.3, ) - ax.add_artist(l) + ax.add_artist(line) if i - 0 >= 0 and nl[i - 1].type != "offcurve": - l = Line2D( + line = Line2D( [n.x, nl[i - 1].x], [n.y, nl[i - 1].y], linewidth=2, color="black", alpha=0.3, ) - ax.add_artist(l) + ax.add_artist(line) else: circle = plt.Circle((n.x, n.y), 3, color="black", alpha=0.3) ax.add_artist(circle) @@ -641,8 +640,6 @@ def constantBrush(t): if not callable(brush): brush = constantBrush - c = brush(0).centroid - from itertools import tee def pairwise(iterable): @@ -660,15 +657,13 @@ def pairwise(iterable): concave_hull = unary_union(polys) ll = [] for x, y in pairwise(concave_hull.exterior.coords): - l = Line(Point(x[0], x[1]), Point(y[0], y[1])) - ll.append(l) + ll.append(Line(Point(x[0], x[1]), Point(y[0], y[1]))) paths = [BezierPath.fromSegments(ll)] for interior in concave_hull.interiors: ll = [] for x, y in pairwise(interior.coords): - l = Line(Point(x[0], x[1]), Point(y[0], y[1])) - ll.append(l) + ll.append(Line(Point(x[0], x[1]), Point(y[0], y[1]))) paths.append(BezierPath.fromSegments(ll)) return paths diff --git a/src/beziers/point.py b/src/beziers/point.py index cf62425..7ee7527 100644 --- a/src/beziers/point.py +++ b/src/beziers/point.py @@ -4,7 +4,7 @@ if typing.TYPE_CHECKING: from beziers.affinetransformation import AffineTransformation - Number = typing.Union[int, float] +Number = typing.Union[int, float] class Point(object): diff --git a/src/beziers/quadraticbezier.py b/src/beziers/quadraticbezier.py index 0a40ec3..73c9cc6 100644 --- a/src/beziers/quadraticbezier.py +++ b/src/beziers/quadraticbezier.py @@ -75,9 +75,9 @@ def flatten(self, degree=8): return [Line(self[0], self[2])] samples = self.sample(self.length / degree) for i in range(1, len(samples)): - l = Line(samples[i - 1], samples[i]) - l._orig = self - ss.append(l) + line = Line(samples[i - 1], samples[i]) + line._orig = self + ss.append(line) return ss def _findRoots(self, dimension): diff --git a/src/beziers/utils/booleanoperationsmixin.py b/src/beziers/utils/booleanoperationsmixin.py index 85aa080..a174a33 100644 --- a/src/beziers/utils/booleanoperationsmixin.py +++ b/src/beziers/utils/booleanoperationsmixin.py @@ -15,9 +15,15 @@ def getSelfIntersections(self): segs = self.asSegments() intersections = [] for seg in segs: - l = seg.hasLoop - if l and l[0] > 0 and l[0] < 1 and l[1] > 0 and l[0] < 1: - intersections.append(Intersection(seg, l[0], seg, l[1])) + loops = seg.hasLoop + if ( + loops + and loops[0] > 0 + and loops[0] < 1 + and loops[1] > 0 + and loops[1] < 1 + ): + intersections.append(Intersection(seg, loops[0], seg, loops[1])) for i1 in range(0, len(segs)): for i2 in range(i1 + 1, len(segs)): for i in segs[i1].intersections(segs[i2]): @@ -28,7 +34,7 @@ def getSelfIntersections(self): def removeOverlap(self): """Resolves a path's self-intersections by 'walking around the outside'.""" if not self.closed: - raise "Can only remove overlap on closed paths" + raise ValueError("Can only remove overlap on closed paths") splitlist = [] splitpoints = {} @@ -42,22 +48,19 @@ def roundoff(point): self.splitAtPoints(splitlist) # Trace path segs = self.asSegments() - for i in range(0, len(segs)): - seg = segs[i] + for i, seg in enumerate(segs): if i < len(segs) - 1: seg.next = segs[i + 1] else: seg.next = segs[0] seg.visited = False - segWinding = self.windingNumberOfPoint(seg.pointAtTime(0.5)) - seg.windingNumber = segWinding + seg.windingNumber = self.windingNumberOfPoint(seg.pointAtTime(0.5)) if roundoff(seg.end) in splitpoints: splitpoints[roundoff(seg.end)]["in"].append(seg) if roundoff(seg.start) in splitpoints: splitpoints[roundoff(seg.start)]["out"].append(seg) newsegs = [] - copying = True - logging.debug("Split points:", splitpoints) + logging.debug("Split points: %s", splitpoints) seg = segs[0] while not seg.visited: logging.debug("Starting at %s, visiting %s" % (seg.start, seg)) @@ -168,7 +171,6 @@ def pairwise(points): for curpoint, nextpoint in zip(a, b): yield curpoint, nextpoint - newpaths = [] from beziers.path import BezierPath for p in paths: diff --git a/src/beziers/utils/curvedistance.py b/src/beziers/utils/curvedistance.py index dfb88ba..aae8426 100644 --- a/src/beziers/utils/curvedistance.py +++ b/src/beziers/utils/curvedistance.py @@ -133,7 +133,7 @@ def minDist(self, uinterval=(0, 1), vinterval=(0, 1), epsilon=0.001): if drk < alpha: isOutside = False if not minDRK or drk < minDRK: - minDrk = drk + minDRK = drk minIJ = (r, k) if isOutside: return [alpha, umid, vmid] diff --git a/src/beziers/utils/linesweep.py b/src/beziers/utils/linesweep.py index 6adbb45..41c92ed 100644 --- a/src/beziers/utils/linesweep.py +++ b/src/beziers/utils/linesweep.py @@ -14,8 +14,8 @@ def bbox_intersections(seta, setb): instructions = [] intersections = [] - def add_to(o, bounds, l): - l.append((o, bounds)) + def add_to(o, bounds, lst): + lst.append((o, bounds)) if l == active_a: other = active_b else: @@ -24,8 +24,8 @@ def add_to(o, bounds, l): if bounds.overlaps(bounds2): intersections.append((o, o2)) - def remove_from(o, bounds, l): - dequefilter(l, lambda i: i[0] != o) + def remove_from(o, bounds, lst): + dequefilter(lst, lambda i: i[0] != o) for a in seta: bounds = a.bounds() @@ -64,6 +64,6 @@ def remove_from(o, bounds, l): for p in right: p.clone().plot(ax, color="red") intersections = bbox_intersections(left, right) - for l, r in intersections: - [p.plot(ax, fill="green") for p in l.intersection(r)] + for line, r in intersections: + [p.plot(ax, fill="green") for p in line.intersection(r)] plt.show()