diff options
| -rw-r--r-- | algorithms.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/algorithms.py b/algorithms.py index 6e01287..8a6e2d3 100644 --- a/algorithms.py +++ b/algorithms.py @@ -1,3 +1,4 @@ +import colorsys import heapq import math @@ -16,6 +17,39 @@ def length_haversine(p1, p2): return 6372797.560856 * c # return the distance in meters +def chunks(lst, n): + """ Split lst into chunks of size n. + + The final chunk will have size equal to len(lst) % n + """ + for i in range(0, len(lst), n): + yield lst[i:min(i+n, len(lst))] + + +def lerp_color(start_color, end_color, t): + """ Lerp between two colors by a factor t. + + Colors are assumed to be in the form "#RRGGBB" + """ + def lerp(a, b, t): + return a + (b - a) * t + + start = [int(c, 16)/255 for c in chunks(start_color[1:], 2)] + end = [int(c, 16)/255 for c in chunks(end_color[1:], 2)] + + start = colorsys.rgb_to_hsv(*start) + end = colorsys.rgb_to_hsv(*end) + + color = [lerp(c1, c2, t) for (c1, c2) in zip(start, end)] + color = colorsys.hsv_to_rgb(*color) + + return "#{:02x}{:02x}{:02x}".format( + int(color[0] * 255), + int(color[1] * 255), + int(color[2] * 255), + ) + + def get_closest_node_id(nodes, source_node): """ Search through all nodes and return the id of the node that is closest to 'source_node'. """ |
