我有兩個三維座標存儲爲六個int
值。 什麼是計算所描述的位置之間的float
距離的更有效的方法:計算2個整數座標點之間距離的最有效方法是?
Vector3.distance(new Vector3((float) a, (float) b, (float) c), new Vector3 ((float) x, (float) y, (float) z)) ;
- 實現取整數作爲參數的函數的正確數學和做所有強制轉換爲浮動,所有^ 2,和在平原C#平方根?
我有兩個三維座標存儲爲六個int
值。 什麼是計算所描述的位置之間的float
距離的更有效的方法:計算2個整數座標點之間距離的最有效方法是?
Vector3.distance(new Vector3((float) a, (float) b, (float) c), new Vector3 ((float) x, (float) y, (float) z)) ;
#1:在Unity中有一個函數:Vector3.Distance,正如您已經添加的那樣。這是最有效的方法(唯一更好的選擇是首先獲得浮點值而不是整數)。
#2: //unity private Vector3 Int2Vector3 (int x, int y, int z) { return new Vector3 ((float)Mathf.Sqrt(x), (float)Math.Sqrt(y), (float)Math.Sqrt(z)); } //plain c# private float Int2FloatSqrt (int a) { return (float)Math.Sqrt(a); }
編輯:這是團結的.Distance功能的更好的理解:public static float Distance(Vector3 a, Vector3 b) { Vector3 vector = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z); return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z); }
我添加[Vector2.Distance](https://docs.unity3d.com/ScriptReference/Vector2.Distance .html),如果你只操作'X'和'Y',這是最好的方法 – matiaslauriti