在我的代码初始化时,我定义了 3 个 Vector3 位置。
Vector3 A = new Vector3(377.999f, 326.656f, 103.566f);
Vector3 B = new Vector3(1137.728f, -981.551f, 46.416f);
Vector3 C = new Vector3(-1223.76f, -905.604f, 12.326f);
然后我为上面定义的每个 Vector3 都有浮动对象。
float Store_distance1 = World.GetDistance(my.Position, A); //Distance from my.pos to A
float Store_distance2 = World.GetDistance(my.Position, B); //Distance from my.pos to B
float Store_distance3 = World.GetDistance(my.Position, C); //Distance from my.pos to C
现在,在调用它时,在一个方法中,我定义如下。
private void SelectNearestStore()
{
List<float> NearestStore = new List <float>
{
Store_distance1, Store_distance2, Store_distance3
};
for (int i = 0; i < NearestStore.Count; i++)
{
if (NearestStore.Min() == NearestStore[i])
{
Console.write("Nearest Store is " + NearestStore[i].ToString() + " Meters Away");
Console.write("Location of the Store is " + //Vector3 property of A or B or C)
}
}
}
假设 Store_distance2 是最低值,
在这种情况下,我在第一个 console.writeline() 的 for 循环中得到正确的浮点数。
如何让 B 值显示在第二个控制台 writeline() 上?
您需要一个Dictionary<Vector3, float>
保存向量及其与您的位置的距离:
var map = new Dictionary<Vector3, float>();
map[A] = World.GetDistance(my.Position, A); //Distance from my.pos to A
map[B] = World.GetDistance(my.Position, B); //Distance from my.pos to B
map[C] = World.GetDistance(my.Position, C); //Distance from my.pos to C
现在您可以轻松地打印每个向量及其距离:
var min = map.Min(x => x.Value);
foreach(var kv in map)
{
if (kv.Value == min)
{
Console.write("Nearest Store is " + min + " Meters Away");
Console.write("Location of the Store is " + kv.Key.ToString();
}
}
最后一个Console.WriteLine
肯定假设它ToString
被覆盖了Vector3
,我不确定。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句