summaryrefslogtreecommitdiffstats
path: root/fish_render.py
blob: 49240166568c4477c2c1a893a038bb81ea7ce98c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Rendering classes and helper functions."""
from __future__ import division, unicode_literals
import color
import random

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


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)
        new_color = coverage * fish.color + (1 - coverage) * new_background
        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."""
    min_x = max(x, fish.x)
    max_x = min(x+1, fish.x + fish.width)
    min_y = max(y, fish.y)
    max_y = min(y+1, fish.y + fish.height)
    coverage = (max_x - min_x) * (max_y - min_y)
    return coverage


def pick_HSV():
    """Return a random color in an appropriate interval."""
    hue = random.random() * (MAX_HUE - MIN_HUE) + MIN_HUE
    saturation = random.random()
    value = random.random()
    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
    return step_vector + start


def random_change():
    """Return a random amount that a light should change each frame."""
    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))