1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
5 def convexHull(pointList):
6 def _isRightTurn((p, q, r)):
7 sum1 = q[0]*r[1] + p[0]*q[1] + r[0]*p[1]
8 sum2 = q[0]*p[1] + r[0]*q[1] + p[0]*r[1]
19 points = unique.keys()
22 # Build upper half of the hull.
23 upper = [points[0], points[1]]
26 while len(upper) > 2 and not _isRightTurn(upper[-3:]):
29 # Build lower half of the hull.
31 lower = [points[0], points[1]]
34 while len(lower) > 2 and not _isRightTurn(lower[-3:]):
41 return numpy.array(upper + lower, numpy.float32) - numpy.array([0.0,0.0], numpy.float32)
43 def minkowskiHull(a, b):
44 points = numpy.zeros((len(a) * len(b), 2))
45 for n in xrange(0, len(a)):
46 for m in xrange(0, len(b)):
47 points[n * len(b) + m] = a[n] + b[m]
48 return convexHull(points.copy())
50 def projectPoly(poly, normal):
51 pMin = numpy.dot(normal, poly[0])
53 for n in xrange(1 , len(poly)):
54 p = numpy.dot(normal, poly[n])
59 def polygonCollision(polyA, polyB):
60 for n in xrange(0, len(polyA)):
63 normal = (p1 - p0)[::-1]
64 normal[1] = -normal[1]
65 normal /= numpy.linalg.norm(normal)
66 aMin, aMax = projectPoly(polyA, normal)
67 bMin, bMax = projectPoly(polyB, normal)
72 for n in xrange(0, len(polyB)):
75 normal = (p1 - p0)[::-1]
76 normal[1] = -normal[1]
77 normal /= numpy.linalg.norm(normal)
78 aMin, aMax = projectPoly(polyA, normal)
79 bMin, bMax = projectPoly(polyB, normal)