From 4af45c013ff54c932f58f86b409ece38b348b7ae Mon Sep 17 00:00:00 2001 From: NeunEinser Date: Fri, 25 Dec 2020 19:07:13 +0100 Subject: [PATCH 1/4] add rgb sphere visualisation --- rgb-spheres.py | 106 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 rgb-spheres.py diff --git a/rgb-spheres.py b/rgb-spheres.py new file mode 100644 index 0000000..3a3ec86 --- /dev/null +++ b/rgb-spheres.py @@ -0,0 +1,106 @@ +def xmaslight(): + # This is the code from my + + #NOTE THE LEDS ARE GRB COLOUR (NOT RGB) + + # Here are the libraries I am currently using: + import time + #from sim import board + #from sim import neopixel + import board + import neopixel + import re + import math + import random + import numpy + + # You are welcome to add any of these: + # import scipy + # import sys + + # If you want to have user changable values, they need to be entered from the command line + # so import sys sys and use sys.argv[0] etc + # some_value = int(sys.argv[0]) + + # IMPORT THE COORDINATES (please don't break this bit) + + #coordfilename = "./coords.txt" + coordfilename = "Python/coords.txt" + + fin = open(coordfilename,'r') + coords_raw = fin.readlines() + + coords_bits = [i.split(",") for i in coords_raw] + + coords = [] + + for slab in coords_bits: + new_coord = [] + for i in slab: + new_coord.append(int(re.sub(r'[^-\d]','', i))) + coords.append(new_coord) + + #set up the pixels (AKA 'LEDs') + PIXEL_COUNT = len(coords) # this should be 500 + + pixels = neopixel.NeoPixel(board.D18, PIXEL_COUNT, auto_write=False) + + + # YOU CAN EDIT FROM HERE DOWN + + # Calculates the distance of 2 vectors + def vdist(v1, v2): + if len(v1) != len(v2): + return -1 + + result = 0 + for i in range(len(v1)): + result += (v1[i] - v2[i]) ** 2 + return math.sqrt(result) + + # init the spheres. Each sphere originates at a random LED + sphere_origins = [ coords[random.randint(0, PIXEL_COUNT)], + coords[random.randint(0, PIXEL_COUNT)], + coords[random.randint(0, PIXEL_COUNT)] ] + radii = [0, 0, 0] + + # calculate maximum distance of any LED for each sphere's origin. + # Used to determine the max radius each sphere will ever receive + max_dists = [0, 0, 0] + for i in range(PIXEL_COUNT): + for s in range(3): + dist = vdist(coords[i], sphere_origins[s]) + if max_dists[s] < dist: + max_dists[s] = dist + + # The rate in which each sphere enlargens. When negative, the sphere is currently shrinking + increment_rates = [0, 0, 0] + for i in range(3): + increment_rates[i] = max_dists[i] / (40 + random.random() * 60) # between 40 and 100 frames + + # infinitly many frames. Wohoo. + while True: + for i in range(PIXEL_COUNT): + + # calculate color for current pixel. Each rgb (grb) color value is 255 * dist / max_dist + color = [0, 0, 0] + for s in range(3): + dist = abs(vdist(sphere_origins[s], coords[i]) - radii[s]) + color[s] = int(255 * dist / max_dists[s]) + + pixels[i] = color + pixels.show() + + # calculate radii for next iteration. + for s in range(3): + # Switch from enlarging to shrinking and vice versa, when needed + new_radius = radii[s] + increment_rates[s] + if new_radius >= max_dists[s] or new_radius <= 0: + increment_rates[s] = -increment_rates[s] + + radii[s] += increment_rates[s] + + return 'DONE' + +# yes, I just put this at the bottom so it auto runs +xmaslight() From 557b097fe8ac84a92ffe0a5672641b49b55d7b24 Mon Sep 17 00:00:00 2001 From: NeunEinser Date: Fri, 25 Dec 2020 21:10:43 +0100 Subject: [PATCH 2/4] Decreased randomness, fixed calculation --- rgb-spheres.py | 60 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/rgb-spheres.py b/rgb-spheres.py index 3a3ec86..6259acd 100644 --- a/rgb-spheres.py +++ b/rgb-spheres.py @@ -49,7 +49,7 @@ def xmaslight(): # YOU CAN EDIT FROM HERE DOWN # Calculates the distance of 2 vectors - def vdist(v1, v2): + def vdist(v1: list, v2: list): if len(v1) != len(v2): return -1 @@ -58,25 +58,55 @@ def vdist(v1, v2): result += (v1[i] - v2[i]) ** 2 return math.sqrt(result) - # init the spheres. Each sphere originates at a random LED - sphere_origins = [ coords[random.randint(0, PIXEL_COUNT)], - coords[random.randint(0, PIXEL_COUNT)], - coords[random.randint(0, PIXEL_COUNT)] ] - radii = [0, 0, 0] + # Find coordinate that maximizes the distance for a given sez of other coords + def find_furthest(points: list): + max_dist = 0 + cur_pnt = points[0] + for coord in coords: + dist = math.inf + for p in points: + p_dist = vdist(p, coord) + if p_dist < dist: + dist = p_dist + + if (dist > max_dist): + max_dist = dist + cur_pnt = coord + return cur_pnt + + + # init sphere origins. + # First sphere's origin is furthest from the coordinate system's origin + # Second sphere's origin is the LED with the greatest distance from the first sphere's origin + # Third sphere's origin is the LED where the distance for both other spheres is maximized. + sphere_origins = [] + sphere_origins.append(find_furthest([[0, 0, 0]])) + sphere_origins.append(find_furthest(sphere_origins)) + sphere_origins.append(find_furthest(sphere_origins)) + # calculate maximum distance of any LED for each sphere's origin. # Used to determine the max radius each sphere will ever receive max_dists = [0, 0, 0] - for i in range(PIXEL_COUNT): - for s in range(3): - dist = vdist(coords[i], sphere_origins[s]) - if max_dists[s] < dist: - max_dists[s] = dist + for coord in coords: + for i in range(3): + dist = vdist(coord, sphere_origins[i]) + if max_dists[i] < dist: + max_dists[i] = dist - # The rate in which each sphere enlargens. When negative, the sphere is currently shrinking + # The rate in which each sphere enlargens. When negative, the sphere is currently shrinking. increment_rates = [0, 0, 0] + # The radius of each sphere. Initial value is randomized + radii = [0, 0, 0] + + # set initial increment rates and radii for i in range(3): - increment_rates[i] = max_dists[i] / (40 + random.random() * 60) # between 40 and 100 frames + # Frames per cycle for current sphere + frames = i * 30 + 120 + increment_rates[i] = max_dists[i] / frames + + # Random start radius + radii[i] = random.random() * frames * increment_rates[i] # infinitly many frames. Wohoo. while True: @@ -86,14 +116,14 @@ def vdist(v1, v2): color = [0, 0, 0] for s in range(3): dist = abs(vdist(sphere_origins[s], coords[i]) - radii[s]) - color[s] = int(255 * dist / max_dists[s]) + color[s] = int(255 * (1 - dist / max_dists[s]) ** 2) pixels[i] = color pixels.show() # calculate radii for next iteration. for s in range(3): - # Switch from enlarging to shrinking and vice versa, when needed + # Switch from enlarging to shrinking and vice versa, as needed new_radius = radii[s] + increment_rates[s] if new_radius >= max_dists[s] or new_radius <= 0: increment_rates[s] = -increment_rates[s] From 20d1a828a6755c82ec1849cb4b8ffff79c5415a0 Mon Sep 17 00:00:00 2001 From: NeunEinser Date: Fri, 25 Dec 2020 21:46:34 +0100 Subject: [PATCH 3/4] tweak some values --- rgb-spheres.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/rgb-spheres.py b/rgb-spheres.py index 6259acd..12e097a 100644 --- a/rgb-spheres.py +++ b/rgb-spheres.py @@ -12,9 +12,9 @@ def xmaslight(): import re import math import random - import numpy # You are welcome to add any of these: + # import numpy # import scipy # import sys @@ -84,7 +84,6 @@ def find_furthest(points: list): sphere_origins.append(find_furthest(sphere_origins)) sphere_origins.append(find_furthest(sphere_origins)) - # calculate maximum distance of any LED for each sphere's origin. # Used to determine the max radius each sphere will ever receive max_dists = [0, 0, 0] @@ -102,7 +101,7 @@ def find_furthest(points: list): # set initial increment rates and radii for i in range(3): # Frames per cycle for current sphere - frames = i * 30 + 120 + frames = i * 40 + 120 increment_rates[i] = max_dists[i] / frames # Random start radius @@ -116,11 +115,10 @@ def find_furthest(points: list): color = [0, 0, 0] for s in range(3): dist = abs(vdist(sphere_origins[s], coords[i]) - radii[s]) - color[s] = int(255 * (1 - dist / max_dists[s]) ** 2) + color[s] = int(255 * (1 - dist / max_dists[s]) ** 3) pixels[i] = color pixels.show() - # calculate radii for next iteration. for s in range(3): # Switch from enlarging to shrinking and vice versa, as needed @@ -129,7 +127,6 @@ def find_furthest(points: list): increment_rates[s] = -increment_rates[s] radii[s] += increment_rates[s] - return 'DONE' # yes, I just put this at the bottom so it auto runs From 3f30c488f986c0d937360d890cdd8e2d30e7537b Mon Sep 17 00:00:00 2001 From: NeunEinser Date: Wed, 13 Jan 2021 19:28:26 +0100 Subject: [PATCH 4/4] Move rgb-spheres to examples --- rgb-spheres.py => examples/rgb-spheres.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rgb-spheres.py => examples/rgb-spheres.py (100%) diff --git a/rgb-spheres.py b/examples/rgb-spheres.py similarity index 100% rename from rgb-spheres.py rename to examples/rgb-spheres.py