|
| 1 | +""" |
| 2 | +Day 22 - Snowflakes |
| 3 | +
|
| 4 | +Falling snowflakes against a purple-blue background. |
| 5 | +Mimics the snowfall effect you can download with the app. |
| 6 | +""" |
| 7 | + |
| 8 | +from xled_plus.samples.sample_setup import * |
| 9 | + |
| 10 | +x = 0.75 ** 0.5 |
| 11 | + |
| 12 | +snowflake1 = [ |
| 13 | + ([0.0, -1.0], [0.0, 1.0], 0.0), |
| 14 | + ([-x, -0.5], [x, 0.5], 0.0), |
| 15 | + ([-x, 0.5], [x, -0.5], 0.0), |
| 16 | + ([0.0, -0.7], [-0.3*x, -0.85], 0.0), |
| 17 | + ([0.0, -0.7], [0.3*x, -0.85], 0.0), |
| 18 | + ([0.0, 0.7], [-0.3*x, 0.85], 0.0), |
| 19 | + ([0.0, 0.7], [0.3*x, 0.85], 0.0), |
| 20 | + ([-0.7*x, -0.35], [-0.7*x, -0.65], 0.0), |
| 21 | + ([-0.7*x, 0.35], [-0.7*x, 0.65], 0.0), |
| 22 | + ([0.7*x, -0.35], [0.7*x, -0.65], 0.0), |
| 23 | + ([0.7*x, 0.35], [0.7*x, 0.65], 0.0), |
| 24 | + ([-0.7*x, -0.35], [-x, -0.2], 0.0), |
| 25 | + ([-0.7*x, 0.35], [-x, 0.2], 0.0), |
| 26 | + ([0.7*x, -0.35], [x, -0.2], 0.0), |
| 27 | + ([0.7*x, 0.35], [x, 0.2], 0.0), |
| 28 | +] |
| 29 | + |
| 30 | +snowflake2 = [ |
| 31 | + ([0.0, -1.0], [0.0, 1.0], 0.0), |
| 32 | + ([-x, -0.5], [x, 0.5], 0.0), |
| 33 | + ([-x, 0.5], [x, -0.5], 0.0), |
| 34 | + ([0.0, -0.5], [-0.5*x, -0.25], 0.0), |
| 35 | + ([0.0, -0.5], [0.5*x, -0.25], 0.0), |
| 36 | + ([0.0, 0.5], [-0.5*x, 0.25], 0.0), |
| 37 | + ([0.0, 0.5], [0.5*x, 0.25], 0.0), |
| 38 | + ([0.5*x, 0.25], [0.5*x, -0.25], 0.0), |
| 39 | + ([-0.5*x, 0.25], [-0.5*x, -0.25], 0.0), |
| 40 | +] |
| 41 | + |
| 42 | +snowflake3 = [ |
| 43 | + ([0.0, -0.5], [0.0, 0.5], 0.0), |
| 44 | + ([-0.5*x, -0.25], [0.5*x, 0.25], 0.0), |
| 45 | + ([-0.5*x, 0.25], [0.5*x, -0.25], 0.0), |
| 46 | +] |
| 47 | + |
| 48 | +snowflake4 = [ |
| 49 | + ([0.0, -0.3], [-0.3*x, -0.15], 0.0), |
| 50 | + ([0.0, -0.3], [0.3*x, -0.15], 0.0), |
| 51 | + ([0.0, 0.3], [-0.3*x, 0.15], 0.0), |
| 52 | + ([0.0, 0.3], [0.3*x, 0.15], 0.0), |
| 53 | + ([0.3*x, 0.15], [0.3*x, -0.15], 0.0), |
| 54 | + ([-0.3*x, 0.15], [-0.3*x, -0.15], 0.0), |
| 55 | +] |
| 56 | + |
| 57 | +class Snowflake(Lineart2D): |
| 58 | + def __init__(self, segs, pos, angle, size, color): |
| 59 | + super(Snowflake, self).__init__() |
| 60 | + for seg in segs: |
| 61 | + self.add_segment(*seg) |
| 62 | + self.color = color |
| 63 | + self.size = size |
| 64 | + self.off = [0.0, 0.0] |
| 65 | + self.angle = 0.0 |
| 66 | + self.speed = (0.0, 0.0) |
| 67 | + self.torque = 0.0 |
| 68 | + self.change_pos(pos) |
| 69 | + self.change_angle(angle) |
| 70 | + |
| 71 | + def change_angle(self, deltaangle): |
| 72 | + self.angle += deltaangle |
| 73 | + ca = m.cos(self.angle * m.pi / 180.0) / self.size |
| 74 | + sa = m.sin(self.angle * m.pi / 180.0) / self.size |
| 75 | + self.mat = [[ca, sa], [-sa, ca]] |
| 76 | + |
| 77 | + def change_pos(self, delta): |
| 78 | + self.off = [self.off[0] - delta[0], self.off[1] - delta[1]] |
| 79 | + |
| 80 | + def update(self, step): |
| 81 | + self.change_pos((step * self.speed[0], step * self.speed[1])) |
| 82 | + self.change_angle(step * self.torque) |
| 83 | + |
| 84 | +class SnowingScene(MovingShapesScene): |
| 85 | + def __init__(self, ctr): |
| 86 | + super(SnowingScene, self).__init__(ctr) |
| 87 | + self.freq = 0.6 |
| 88 | + self.shapetypes = [snowflake1, snowflake2, snowflake3, snowflake4] |
| 89 | + self.shapeprobs = [0.15, 0.15, 0.3, 0.4] |
| 90 | + cf1 = random_hsl_color_func(hue=[0.06,0.12], sat=1.0, light=[0.2,0.6]) |
| 91 | + cf2 = random_hsl_color_func(hue=[0.96,0.06], sat=0.7, light=0.0) |
| 92 | + self.shapecolfuncs = [cf1, cf1, cf2, cf2] |
| 93 | + self.preferred_frames = 640 |
| 94 | + |
| 95 | + def create(self): |
| 96 | + tp = randomdiscrete(self.shapeprobs) |
| 97 | + col = self.shapecolfuncs[tp]() |
| 98 | + dist = 1.0 + random() + (tp // 2)*2 |
| 99 | + speed = 0.02 / dist |
| 100 | + rot = (random() - 0.5) * 6.0 |
| 101 | + offset = (random() * 2 - 1.0, 1.5) |
| 102 | + vec = (0.0, -speed) |
| 103 | + sf = Snowflake(self.shapetypes[tp], offset, 0.0, 0.3, col) |
| 104 | + sf.speed = vec |
| 105 | + sf.torque = rot |
| 106 | + sf.set_depth(dist) |
| 107 | + sf.lw = 0.15 |
| 108 | + sf.duetime = self.time + int(3.0 / speed) |
| 109 | + return sf |
| 110 | + |
| 111 | + def getnext(self): |
| 112 | + pat1 = super(SnowingScene, self).getnext() |
| 113 | + hue = (0.78 + 0.44 * abs(0.5 - (float(self.time) / 160) % 1.0)) % 1.0 |
| 114 | + patbg = ctr.make_layout_pattern(lambda pos: hsl_color(hue, 1.0, -0.92), style="centered") |
| 115 | + vec = self.getoccupancy() |
| 116 | + pat = [pat1[i] if vec[i] else patbg[i] for i in range(self.ctr.num_leds)] |
| 117 | + return pat |
| 118 | + |
| 119 | +ctr = setup_control() |
| 120 | +ctr.adjust_layout_aspect(1.0) # How many times wider than high is the led installation? |
| 121 | +SnowingScene(ctr).launch_movie() |
0 commit comments