summaryrefslogtreecommitdiffstats
path: root/fish_render.py
diff options
context:
space:
mode:
Diffstat (limited to 'fish_render.py')
-rw-r--r--fish_render.py28
1 files changed, 20 insertions, 8 deletions
diff --git a/fish_render.py b/fish_render.py
index becd8c1..6d24167 100644
--- a/fish_render.py
+++ b/fish_render.py
@@ -13,6 +13,7 @@ class Fish(object):
self.height = height
self.color = color
self.speed = step
+ self.step = step
def update(self):
"""Swim gently to the other side of the bridge."""
@@ -21,12 +22,12 @@ class Fish(object):
class Light(object):
"""Represent a bridge light and its state."""
- def __init__(self, x, y, light, start_color, step):
+ def __init__(self, x, y, light):
"""Store all facts about a light."""
self.light = light
- self.color = start_color
- self.transitioning_to = start_color
- self.step = step
+ self.color = color.black
+ self.transitioning_to = self.color
+ self.step = 0
self.x = x
self.y = y
@@ -40,7 +41,9 @@ class Light(object):
)
coverage = compute_coverage(fish, self.x, self.y)
new_color = coverage * fish.color + (1 - coverage) * new_background
- self.light.setRGBRaw(*new_color.components)
+ self.light.setRGBRaw(
+ *map(lambda x: clamp(0, x, 1), new_color.components)
+ )
class Bridge(object):
@@ -55,8 +58,8 @@ class Bridge(object):
"""Compute fish coverages and push lights to bridge."""
for x, light_col in enumerate(self.lights):
for y, light in enumerate(light_col):
- fish.update()
- light.update(fish)
+ self.fish.update()
+ light.update(self.fish)
self.rig.updateOnce()
@@ -80,7 +83,11 @@ def pick_HSV():
def transition(start, end, step):
"""Return a vector equal to (end - start) * step + start."""
- return (end - start) / abs(end - start) * step + start
+ if start == end:
+ return start
+ diff_vector = end - start
+ step_vector = diff_vector / abs(diff_vector) * step
+ return step_vector + start
def random_change():
@@ -88,3 +95,8 @@ def random_change():
min_time = int(MIN_TIME * FRAME_RATE)
max_time = int(MAX_TIME * FRAME_RATE)
return 1 / random.randint(min_time, max_time)
+
+
+def clamp(low, x, high):
+ """Return v such that low <= v <= high and |x - v| is minimal."""
+ return max(low, min(x, high))