unity中,连续路径上的点排序

 1         /// <summary>
 2         /// 路径点排序
 3         /// </summary>
 4         /// <param name="start">路径起点</param>
 5         /// <param name="input">路径上所有的点</param>
 6         /// <param name="output">空的列表,保存排序后的路径</param>
 7         public static void SortToPath(Vector3 start, List<Vector3> input, List<Vector3> output)
 8         {
 9             var currentNearestDistance = float.PositiveInfinity;
10             var nearestIdx = -1;
11             for (int i = 0; i < input.Count; i++)
12             {
13                 var dis = Vector3.Distance(start, input[i]);
14                 if (dis < currentNearestDistance)
15                 {
16                     currentNearestDistance = dis;
17                     nearestIdx = i;
18                 }
19             }
20             if (nearestIdx >= 0)
21             {
22                 output.Add(input[nearestIdx]);
23                 input.RemoveAt(nearestIdx);
24                 SortToPath(output[output.Count - 1], input, output);
25             }
26         }

 

posted @ 2024-02-01 10:19  zerozabuu  阅读(15)  评论(0编辑  收藏  举报