annotate skaapsteker/utils.py @ 627:35919d12b792

Path-based collision minimisation and axis-projection backout.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 07 May 2011 20:28:06 +0200
parents 4ffa9d159588
children 0675f390653c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
619
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
1 import operator
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2 import functools
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
3
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
4
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
5 def mktuple(thing):
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
6 if isinstance(thing, (list, tuple)):
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
7 return tuple(thing)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
8 return (thing, thing)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
9
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
10
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
11 def coord_op(fun, coord, operand):
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
12 operand = mktuple(operand)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
13 return (fun(coord[0], operand[0]),
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
14 fun(coord[1], operand[1]))
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
15
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
16
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
17 def mk_cop(op):
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
18 return functools.partial(coord_op, op)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
19
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
20
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
21 def mk_cuop(op):
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
22 return lambda coord: (op(coord[0]), op(coord[1]))
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
23
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
24
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
25 cadd = mk_cop(operator.add)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
26 csub = mk_cop(operator.sub)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
27 cmul = mk_cop(operator.mul)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
28 cdiv = mk_cop(operator.div)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
29 cclamp = mk_cop(lambda a, b: max(min(a, b), -b))
627
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
30 cabsmax = mk_cop(lambda a, b: a if abs(a) > abs(b) else b)
619
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
31
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
32 cint = mk_cuop(int)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
33 cneg = mk_cuop(lambda a: -a)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
34 cabs = mk_cuop(abs)
4ffa9d159588 Some coordinate operators, to reduce foo_x, foo_y everywhere.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
35
627
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
36
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
37 def rect_projection(rect1, rect2):
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
38 if not rect1.colliderect(rect2):
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
39 # No collision?
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
40 return (0, 0)
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
41
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
42 if rect1.center[0] < rect2.center[0]:
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
43 x_projection = rect2.left - rect1.right
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
44 else:
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
45 x_projection = rect2.right - rect1.left
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
46
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
47 if rect1.center[1] < rect2.center[1]:
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
48 y_projection = rect2.top - rect1.bottom
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
49 else:
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
50 y_projection = rect2.bottom - rect1.top
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
51
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
52 if abs(x_projection) < abs(y_projection):
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
53 return (x_projection, 0)
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
54 return (0, y_projection)
35919d12b792 Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents: 619
diff changeset
55