Skip to content

Instantly share code, notes, and snippets.

@andrewgiessel
Last active December 28, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewgiessel/7567879 to your computer and use it in GitHub Desktop.
Save andrewgiessel/7567879 to your computer and use it in GitHub Desktop.
use of scipy.spatial.kdtree to get all points w/i specified distance from point
# imports
import numpy as np
import scipy as scipy
# construct static 2d distance tree
size = 256
x, y = np.mgrid[0:size, 0:size]
distance_tree = spatial.KDTree(zip(x.ravel(), y.ravel()))
# simple use case
point_of_interest = [size/2, size/2]
points_away = 5
a = distance_tree.query_ball_point(point_of_interest, points_away)
nearby = [points[p] for p in a] # all points 5 away from center of 256/256 array, inclusive
# note, you can use set operations to do things like get a ring of points a given distance from a point
a = set(distance_tree.query_ball_point(point_of_interest, 10))
b = set(distance_tree.query_ball_point(point_of_interest, 15))
nearby = [points[p] for p in b-a] # all points between 10 and 15 away from center of 256x256 array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment