Mercurial > skaapsteker
comparison 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 |
comparison
equal
deleted
inserted
replaced
618:72865593bdc7 | 619:4ffa9d159588 |
---|---|
1 import operator | |
2 import functools | |
3 | |
4 | |
5 def mktuple(thing): | |
6 if isinstance(thing, (list, tuple)): | |
7 return tuple(thing) | |
8 return (thing, thing) | |
9 | |
10 | |
11 def coord_op(fun, coord, operand): | |
12 operand = mktuple(operand) | |
13 return (fun(coord[0], operand[0]), | |
14 fun(coord[1], operand[1])) | |
15 | |
16 | |
17 def mk_cop(op): | |
18 return functools.partial(coord_op, op) | |
19 | |
20 | |
21 def mk_cuop(op): | |
22 return lambda coord: (op(coord[0]), op(coord[1])) | |
23 | |
24 | |
25 cadd = mk_cop(operator.add) | |
26 csub = mk_cop(operator.sub) | |
27 cmul = mk_cop(operator.mul) | |
28 cdiv = mk_cop(operator.div) | |
29 cclamp = mk_cop(lambda a, b: max(min(a, b), -b)) | |
30 | |
31 cint = mk_cuop(int) | |
32 cneg = mk_cuop(lambda a: -a) | |
33 cabs = mk_cuop(abs) | |
34 |