Skip to content

Commit

Permalink
Ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Jun 11, 2024
1 parent e937796 commit 82c0d94
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 41 deletions.
12 changes: 6 additions & 6 deletions src/beziers/cubicbezier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand Down
21 changes: 8 additions & 13 deletions src/beziers/path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -641,8 +640,6 @@ def constantBrush(t):
if not callable(brush):
brush = constantBrush

c = brush(0).centroid

from itertools import tee

def pairwise(iterable):
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/beziers/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions src/beziers/quadraticbezier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 13 additions & 11 deletions src/beziers/utils/booleanoperationsmixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand All @@ -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 = {}

Expand All @@ -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))
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/beziers/utils/curvedistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 6 additions & 6 deletions src/beziers/utils/linesweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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()

0 comments on commit 82c0d94

Please sign in to comment.