Pymunk.utils.convex_hull() erzeugt einen Fehler
Verfasst: Sonntag 9. März 2014, 14:17
Hi Community,
ich habe ein Problem und schon viel selbst rumgeschraubt:
Wenn ich den convex_hull algorythmus von Pymunk (eine PE) auf eine liste von punkten anwenden will, wird ein Fehler erzeugt:
File "C:\Python33\lib\site-packages\pymunk\util.py", line 153, in convex_hull
points.sort(cmp = f)
TypeError: 'cmp' is an invalid keyword argument for this function
Hier ist die Funktion, die den Fehler wirft:
Source: Pymunk, util.py, convex_hull
Ich hab mir das angeschaut und bei points.sort() cmp mit key ersetzt (der docstring müsste überarbeitet werden.) --> Funktioniert.
Aber schon kommt der nächste Fehler:
File "C:\Python33\lib\site-packages\pymunk\util.py", line 153, in convex_hull
points.sort(key = f)
TypeError: is_left() missing 1 required positional argument: 'p2'
In der Tat fordert is_left drei punkte als Parameter...
Was mach ich falsch? Sollte ich den convex_hull Algorythmus selber schreiben, wenn das nicht funktioniert?
Danke,
jenderman
ich habe ein Problem und schon viel selbst rumgeschraubt:
Wenn ich den convex_hull algorythmus von Pymunk (eine PE) auf eine liste von punkten anwenden will, wird ein Fehler erzeugt:
File "C:\Python33\lib\site-packages\pymunk\util.py", line 153, in convex_hull
points.sort(cmp = f)
TypeError: 'cmp' is an invalid keyword argument for this function
Hier ist die Funktion, die den Fehler wirft:
Code: Alles auswählen
def convex_hull(points):
"""Create a convex hull from a list of points.
This function uses the Graham Scan Algorithm.
:return: Convex hull as a list of (x,y)
"""
assert len(points) > 2, "need at least 3 points to form a convex hull"
### Find lowest rightmost point
p0 = points[0]
for p in points[1:]:
if p[Y] < p0[Y]:
p0 = p
elif p[Y] == p0[Y] and p[X] > p0[X]:
p0 = p
points.remove(p0)
### Sort the points angularly about p0 as center
f = partial(is_left, p0)
points.sort(cmp = f)
points.reverse()
points.insert(0, p0)
### Find the hull points
hull = [p0, points[1]]
for p in points[2:]:
pt1 = hull[-1]
pt2 = hull[-2]
l = is_left(pt2, pt1, p)
if l > 0:
hull.append(p)
else:
while l <= 0 and len(hull) > 2:
hull.pop()
pt1 = hull[-1]
pt2 = hull[-2]
l = is_left(pt2, pt1, p)
hull.append(p)
return hull
Ich hab mir das angeschaut und bei points.sort() cmp mit key ersetzt (der docstring müsste überarbeitet werden.) --> Funktioniert.
Aber schon kommt der nächste Fehler:
File "C:\Python33\lib\site-packages\pymunk\util.py", line 153, in convex_hull
points.sort(key = f)
TypeError: is_left() missing 1 required positional argument: 'p2'
In der Tat fordert is_left drei punkte als Parameter...
Was mach ich falsch? Sollte ich den convex_hull Algorythmus selber schreiben, wenn das nicht funktioniert?
Danke,
jenderman