Last active
September 12, 2017 04:17
-
-
Save arbennett/fdb93b9e3977fa39d2670179813505ae 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
var | |
x : array[0..99] of single; | |
y : array[0..99] of single; | |
i : Integer; | |
N : Integer; | |
s : Integer; | |
nn : Integer; | |
x_s : single; | |
y_s : single; | |
x_dist : single; | |
y_dist : single; | |
dist : single; | |
nearest_dist : single; | |
nearest_idx : Integer; | |
begin | |
N := 100; | |
// initialize coordinates | |
for i := 1 to N do | |
begin | |
x[i] := Random; | |
y[i] := Random; | |
end; | |
// find nearest neighbor of one | |
s := Random(N); | |
x_s := x[s]; | |
y_s := y[s]; | |
nearest_dist := 10; | |
for i := 1 to N do | |
begin | |
if i <> s then | |
begin | |
x_dist := x_s - x[i]; | |
y_dist := y_s - y[i]; | |
dist := Sqrt(x_dist*x_dist + y_dist*y_dist); | |
if nearest_dist > dist then | |
begin | |
nearest_dist := dist; | |
nearest_idx := i; | |
end; | |
end; | |
end; | |
end. |
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
#!/usr/bin/env python | |
import numpy as np | |
import scipy.spatial as sp | |
def main(): | |
N = 10 | |
x = np.random.random((N, 2)) | |
tree = sp.cKDTree(x) | |
for i in range(N): | |
dist, idx = tree.query(x[i], k=2, distance_upper_bound=4) | |
print(dist[-1], idx[-1]) | |
if __name__ == '__main__': | |
main() |
Author
arbennett
commented
Sep 12, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment