Mercurial > skaapsteker
diff skaapsteker/utils.py @ 619:4ffa9d159588
Some coordinate operators, to reduce foo_x, foo_y everywhere.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Fri, 06 May 2011 16:37:43 +0200 |
parents | |
children | 35919d12b792 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/skaapsteker/utils.py Fri May 06 16:37:43 2011 +0200 @@ -0,0 +1,34 @@ +import operator +import functools + + +def mktuple(thing): + if isinstance(thing, (list, tuple)): + return tuple(thing) + return (thing, thing) + + +def coord_op(fun, coord, operand): + operand = mktuple(operand) + return (fun(coord[0], operand[0]), + fun(coord[1], operand[1])) + + +def mk_cop(op): + return functools.partial(coord_op, op) + + +def mk_cuop(op): + return lambda coord: (op(coord[0]), op(coord[1])) + + +cadd = mk_cop(operator.add) +csub = mk_cop(operator.sub) +cmul = mk_cop(operator.mul) +cdiv = mk_cop(operator.div) +cclamp = mk_cop(lambda a, b: max(min(a, b), -b)) + +cint = mk_cuop(int) +cneg = mk_cuop(lambda a: -a) +cabs = mk_cuop(abs) +