Skip to content

Commit 3489e9c

Browse files
committed
avoid black disks and zero divides
1 parent d4ce06d commit 3489e9c

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

curve.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def any_from_params(cls, params):
2929
subcls = cls.subcurves[alg]
3030
return subcls.from_params(params)
3131

32+
def complexity(self):
33+
return 0
34+
3235
def points(self, dims, dt=0.01):
3336
pass
3437

render.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,24 @@ def __init__(self, gray=0, **kwargs):
5656
assert self.alpha == 1
5757

5858
def draw_curve(self, ctx, curve, scale):
59-
ctx.set_source_rgb(self.gray, self.gray, self.gray)
60-
for i, (x, y) in enumerate(curve.points(["x", "y"], scale=scale, dt=self.dt)):
61-
if i == 0:
62-
ctx.move_to(x, y)
63-
else:
64-
ctx.line_to(x, y)
65-
ctx.stroke()
59+
if curve.complexity() < 25_000:
60+
ctx.set_source_rgb(self.gray, self.gray, self.gray)
61+
for i, (x, y) in enumerate(curve.points(["x", "y"], scale=scale, dt=self.dt)):
62+
if i == 0:
63+
ctx.move_to(x, y)
64+
else:
65+
ctx.line_to(x, y)
66+
ctx.stroke()
67+
else:
68+
ctx.set_source_rgb(0.75, 0.75, 0.75)
69+
w = scale * 0.25
70+
ctx.move_to(-w, -w)
71+
ctx.line_to(-w, w)
72+
ctx.line_to(w, w)
73+
ctx.line_to(w, -w)
74+
ctx.close_path()
75+
ctx.fill()
76+
6677

6778

6879
class ColorLine(Render):

spirograph.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ def adjacent_teeth(t):
3232
teeth.remove(t)
3333
return teeth
3434

35+
def adjacent_nonzero(v):
36+
"""
37+
Four adjacent values, but not zero.
38+
"""
39+
vals = [v - 2, v - 1, v + 1, v + 2]
40+
if 0 in vals:
41+
vals.remove(0)
42+
if v > 0:
43+
vals = [v - 3] + vals
44+
else:
45+
vals = vals + [v + 3]
46+
assert len(vals) == 4
47+
return vals
3548

3649
@dataclass
3750
class Gear(Parameterized):
@@ -80,7 +93,7 @@ class Spirograph(Curve):
8093
name="last_gear_speed_denominator",
8194
key="zd",
8295
default=1,
83-
adjacent_step=1,
96+
adjacent=adjacent_nonzero,
8497
)
8598
max_cycles = None
8699

@@ -123,6 +136,9 @@ def make_random(cls, rnd):
123136
curve.last_speed_denom = rnd.choice([1, 2])
124137
return curve
125138

139+
def complexity(self):
140+
return self._cycles() * 100
141+
126142
def _make_circles(self):
127143
if self.circles is not None:
128144
return self.circles

webapp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def spirographs():
124124
while len(thumbs) < 30:
125125
try:
126126
curve = Spirograph.make_random(random)
127-
if curve._cycles() < 50:
127+
if curve.complexity() < 5000:
128128
thumbs.append(Thumb(curve, size=size))
129129
except ImpossibleCurve:
130130
pass

0 commit comments

Comments
 (0)