21emotion
(21emotion)
January 17, 2023, 3:16am
1
I’d like to sort each room and make sorted list.
If ‘start room’ is specified as ‘room_1’, I would like to sort the room list of the selected ‘allRoom’ into ‘room_1, room_2, room_3, room_4’ in the order they are connected.
Please also comment on the code I wrote.
nearestPoint_question.3dm (120.8 KB)
nearestPoint_question.gh (9.0 KB)
Hello
Find attached two methods, one with bruteforce list sorting, the second one much more compact and efficient with System.Linq.
You may also want to have a look at the ClosestPoint method from RhinoCommon ?
//bruteforce method
List<int> sortedindices = new List<int>();
List<double> sorteddist = new List<double>();
List<Point3d> sortedcloudPoints = new List<Point3d>();
for (int k = 0; k < cCount; k++)
{
double mindist = dist[0];
int thej = 0;
for (int j = 1; j < dist.Count; j++)
{
if (dist[j] < mindist)
{
thej = j;
mindist = dist[j];
}
}
sortedindices.Add(indices[thej]);
sorteddist.Add(dist[thej]);
sortedcloudPoints.Add(cloudPoints[thej]);
indices.RemoveAt(thej);
dist.RemoveAt(thej);
cloudPoints.RemoveAt(thej);
}
//linq method
var sorted = dist.Zip(indices.Zip(cloudPoints, (ind, p) => new { Index = ind, Point = p }), (d, x) => new { Dist = d, x.Index, x.Point })
.OrderBy(x => x.Dist);
dist = sorted.Select(x => x.Dist).ToList();
indices = sorted.Select(x => x.Index).ToList();
cloudPoints = sorted.Select(x => x.Point).ToList();
//ClosestPt method
PointCloud pc = new PointCloud(cloudPoints);
int the_index = pc.ClosestPoint(basePoint);
nearestPoint_question.gh (11.3 KB)
1 Like
21emotion
(21emotion)
January 18, 2023, 3:03am
3
I always feel that there are amazing masters in the world.
You solved my difficulties perfectly.
Thank you so much.