c# - Finding n number of closest points in a list -


c# beginner here, working grasshopper rhino object types little strange.

i trying find n number of closest points within list given point. able find closest point using following function, cannot adapt retrieve multiple closest points.

public vec3d closestpoint(vec3d a, list<vec3d>points){   list<float> distancelist = new list<float>();   (int = 0; < points.count(); i++){     float distancefloat = a.distanceto(points[i]);     distancelist.add(distancefloat);   }   int smallestindex = distancelist.indexof(distancelist.min());   return points[smallestindex]; } 

how adapt code below function, n how many closest points find?

public vec3d closestpoints(vec3d a, list<vec3d>points, int n){    }  

you can use linq result:

public ienumerable<vec3d> closestpoints(vec3d a, list<vec3d>points, int n) =>    points.select(point => new    {       point = point,       distance = a.distanceto(point)    }).   orderby(x = x.distance).   take(n).   select(x => x.point); 

note in case, other method becomes:

public vec3d closestpoint(vec3d a, list<vec3d>points) =>   closestpoints(a, points, 1).firstordefault(); 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -