summaryrefslogtreecommitdiffstats
path: root/fish_render.py
diff options
context:
space:
mode:
authorAaron Perley <aaron.perley@gmail.com>2016-10-09 18:00:10 -0400
committerAaron Perley <aaron.perley@gmail.com>2016-10-09 18:00:10 -0400
commitf05ccbe166fd2501be3502f81f0562e73291fa04 (patch)
treecbc4eb9b8de594533e4f8b262385652b0dfd0309 /fish_render.py
parentUpdate simulator to use pygame instead of tkinter (diff)
downloadbridge-f05ccbe166fd2501be3502f81f0562e73291fa04.tar.zst
Refactor bridge, fish show, and sunrise show code
Diffstat (limited to 'fish_render.py')
-rw-r--r--fish_render.py123
1 files changed, 0 insertions, 123 deletions
diff --git a/fish_render.py b/fish_render.py
deleted file mode 100644
index 18aecac..0000000
--- a/fish_render.py
+++ /dev/null
@@ -1,123 +0,0 @@
-"""Rendering classes and helper functions."""
-from __future__ import division, unicode_literals
-import color
-import random
-import constants
-from constants import *
-import math
-
-class Fish(object):
- """Object displayed on the bridge, probably a fish."""
- def __init__(self, x, y, width, height, color, step):
- """Create fish with default parameters."""
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.color = color
- self.speed = step
- self.step = step
-
- def update(self):
- """Swim gently to the other side of the bridge."""
- self.x += self.step
- if self.x > 200:
- self.x = 0
- self.y = 0.25 + 0.25 * math.sin(math.pi * self.x / 10)
- #theta = random.random() * math.pi / 2 - math.pi / 4
- #self.y += 0.01 * math.sin(theta)
- #print(self.y)
-
-
-class Light(object):
- """Represent a bridge light and its state."""
- def __init__(self, x, y, light):
- """Store all facts about a light."""
- self.light = light
- self.color = color.black
- self.transitioning_to = self.color
- self.step = 0
- self.x = x
- self.y = y
-
- def update(self, fish):
- """Change color depending on where the fish is."""
- if abs(self.transitioning_to - self.color) <= self.step:
- self.transitioning_to = pick_HSV().to_RGB()
- self.step = (random_change()
- * abs(self.color - self.transitioning_to))
- new_background = transition(
- self.color, self.transitioning_to, self.step
- )
- coverage = compute_coverage(fish, self.x, self.y)
- #coverage *= coverage
- new_color = coverage * fish.color + (1 - coverage) * new_background
- #new_color = new_background
- self.color = new_background
- if self.x == 30 and self.y == 0:
- pass
- #print(self.color.r(), self.color.g(), self.color.b())
- self.light.setRGBRaw(
- *map(lambda x: clamp(0, x, 1), new_color.components)
- )
-
-
-class Bridge(object):
- """Store objects on the bridge."""
- def __init__(self, lights, fish, rig):
- """Initialize with lights and fish."""
- self.lights = lights
- self.fish = fish
- self.rig = rig
-
- def update(self):
- """Compute fish coverages and push lights to bridge."""
- for x, light_col in enumerate(self.lights):
- for y, light in enumerate(light_col):
- self.fish.update()
- light.update(self.fish)
- self.rig.updateOnce()
-
-
-def compute_coverage(fish, x, y):
- """Calculate the coverage of a fish over a box."""
- middle_of_fish = fish.x + fish.width / 2
- middle_of_panel = x + 0.5
- diff = abs(middle_of_fish - middle_of_panel) / (fish.width / 2)
- scale = 1 - diff
- min_x = min(max(x, fish.x), x + 1)
- max_x = max(min(x+1, fish.x + fish.width), x)
- min_y = min(max(y, fish.y), y + 1)
- max_y = max(min(y+1, fish.y + fish.height), y)
- coverage = (max_x - min_x) * (max_y - min_y)
- return coverage * scale
-
-
-def pick_HSV():
- """Return a random color in an appropriate interval."""
- hue = random.random() * (constants.MAX_HUE - constants.MIN_HUE) + constants.MIN_HUE
- saturation = random.random() * (MAX_SATURATION - MIN_SATURATION) + MIN_SATURATION
- value = random.random() * (MAX_VALUE - MIN_VALUE) + MIN_VALUE
- return color.HSV(hue, saturation, value)
-
-
-def transition(start, end, step):
- """Return a vector equal to (end - start) * step + start."""
- if start == end:
- return start
- diff_vector = end - start
- step_vector = diff_vector / abs(diff_vector) * step
- result = step_vector + start
- return result
-
-
-def random_change():
- """Return a random amount that a light should change each frame."""
- min_time = int(constants.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))