Skip to content

Instantly share code, notes, and snippets.

@wezu
Last active April 20, 2018 09:18
Show Gist options
  • Save wezu/2634feb1bf038a209acb1163096d66a2 to your computer and use it in GitHub Desktop.
Save wezu/2634feb1bf038a209acb1163096d66a2 to your computer and use it in GitHub Desktop.
from panda3d.core import *
from random import uniform
import sys
import bisect
if sys.version_info >= (3, 3):
from time import perf_counter as timer
else:
if sys.platform == "win32":
from time import clock as timer
else:
from time import time as timer
def nearest(data, point, threshold=0.5):
minimal=None
best=None
for v in data:
d=(v-point).length()
if d < threshold:
return v
if minimal is None:
minimal = d
if d <= minimal:
minimal=d
best=v
return best
#create some data
data=[]
for x in range(100):
for y in range(100):
for z in range(100):
data.append(Vec3(x,y,z))
#some random point to find
search_for=Vec3(uniform(0,100),uniform(0,100),uniform(0,100))
#search and time it
print('Starting search...')
start = timer()
#I need something better for this!!!
#found=sorted(data, key=lambda v: (v-search_for).length())[0] #~1.5sec
found=nearest(data, search_for) #3x faster, but still ~0.5 sec
end = timer()
print('{0} is nearest to {1}, found in {2}'.format(found,search_for, end-start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment