Created
April 22, 2014 21:24
-
-
Save fringedgentian/11194890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt, pow | |
def distance(a, b): | |
return sqrt(pow(a[0] - b[0],2) + pow(a[1] - b[1],2)) | |
def bruteMin(points, current=float("inf")): | |
if len(points) < 2: return current | |
else: | |
head = points[0] | |
del points[0] | |
newMin = min([distance(head, x) for x in points]) | |
newCurrent = min([newMin, current]) | |
return bruteMin(points, newCurrent) | |
def divideMin(points): | |
half = len(sorted(points))/2 | |
minimum = min([bruteMin(points[:half]), bruteMin(points[half:])]) | |
nearLine = filter(lambda x: x[0] > half - minimum and x[0] < half + minimum, points) | |
return min([bruteMin(nearLine), minimum]) | |
list1 = [(12,30), (40, 50), (5, 1), (12, 10), (3,4)] | |
print divideMin(list1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello,
I am searching for an algorithm like this and luckily found this working snippet.
After some unit test, I've found an issue.
You can duplicate the issue with replacing line 21 by
list1 = [(1, 496.5), (12,30), (40, 50), (5, 1), (12, 10), (3,4), (1, 496), (1, 497)] #bug
The correct output would be 0.5 but 1 was returned.
This algorithm needs points sorted allowing you split them into halves in line 17.
I fixed this by simply adding
points = sorted(points)
at the top of divideMin.
Hope this helps, it an old thread though.
: D