From 04d3f079dbf6da0a3e8141dae174919995500105 Mon Sep 17 00:00:00 2001 From: Peter Boraros Date: Sun, 23 Dec 2012 01:07:53 +0100 Subject: [PATCH] rainbow2d: nicer distance function !! --- host_python/rainbow2d.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/host_python/rainbow2d.py b/host_python/rainbow2d.py index 677d7f4..1c6c285 100755 --- a/host_python/rainbow2d.py +++ b/host_python/rainbow2d.py @@ -35,13 +35,23 @@ corr_b_v = [0.0, 0.13, 0.18, 1.0] def set_pixel_2d(bar, x, y, r, g, b): bar.set_pixel(WIDTH - x - 1 + y*WIDTH, r, g, b) +def combine(*fncs): + return lambda *args: reduce( + id, + reduce( + lambda acc,fnc:(fnc(*acc),), + reversed(fncs), + args) + ) + +def flip(f): + return lambda *a: f(*reversed(a)) + def cdist(center, point, scale): - dist = math.sqrt(reduce( - lambda a,b: a+b, - map(lambda (x,y): (x-y)**2, zip(center,point)) - )) - dist = dist*scale - return dist + from operator import add,sub + from math import sqrt + from functools import partial + return scale*sqrt(reduce(add, map(combine(partial(flip(pow),2),sub),center,point))) l = Ledbar(PIXELS)