Use a generator to generate the size list
Way faster
This commit is contained in:
parent
859cfea9d3
commit
cfa38b6216
1 changed files with 7 additions and 6 deletions
13
hull.py
13
hull.py
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
import operator
|
import operator
|
||||||
from utils import x,y,euclidian_distance
|
from utils import x,y,euclidian_distance,LOG,LOGN
|
||||||
|
|
||||||
# Based on the excellent article by Tom Switzer <thomas.switzer@gmail.com>
|
# Based on the excellent article by Tom Switzer <thomas.switzer@gmail.com>
|
||||||
# http://tomswitzer.net/2010/12/2d-convex-hulls-chans-algorithm/
|
# http://tomswitzer.net/2010/12/2d-convex-hulls-chans-algorithm/
|
||||||
|
|
@ -173,13 +173,11 @@ def convex_hull(points):
|
||||||
"""Returns the points on the convex hull of points in CCW order."""
|
"""Returns the points on the convex hull of points in CCW order."""
|
||||||
|
|
||||||
# Increasing guesses for the hull size.
|
# Increasing guesses for the hull size.
|
||||||
sizes = []
|
for guess in ( 2**(2**t) for t in range(len(points)) ):
|
||||||
for t in range(len(points)):
|
LOG( "Guess",guess)
|
||||||
sizes.append( 2 ** (2 ** t) )
|
|
||||||
|
|
||||||
for guess in sizes:
|
|
||||||
hulls = []
|
hulls = []
|
||||||
for i in range(0, len(points), guess):
|
for i in range(0, len(points), guess):
|
||||||
|
LOG(".")
|
||||||
# Split the points into chunks of (roughly) the guess.
|
# Split the points into chunks of (roughly) the guess.
|
||||||
chunk = points[i:i + guess]
|
chunk = points[i:i + guess]
|
||||||
# Find the corresponding convex hull of these chunks.
|
# Find the corresponding convex hull of these chunks.
|
||||||
|
|
@ -190,11 +188,14 @@ def convex_hull(points):
|
||||||
|
|
||||||
# Ensure we stop after no more than "guess" iterations.
|
# Ensure we stop after no more than "guess" iterations.
|
||||||
for __ in range(guess):
|
for __ in range(guess):
|
||||||
|
LOG("*")
|
||||||
pair = next_hull_pt_pair(hulls, hullpt_pairs[-1])
|
pair = next_hull_pt_pair(hulls, hullpt_pairs[-1])
|
||||||
if pair == hullpt_pairs[0]:
|
if pair == hullpt_pairs[0]:
|
||||||
# Return the points in sequence
|
# Return the points in sequence
|
||||||
|
LOGN("o")
|
||||||
return [hulls[h][i] for h,i in hullpt_pairs]
|
return [hulls[h][i] for h,i in hullpt_pairs]
|
||||||
hullpt_pairs.append(pair)
|
hullpt_pairs.append(pair)
|
||||||
|
LOGN("x")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue