![](/img/trans.png)
[英]Calculate distance between two points in 3D space with two coordinate layers
[英]Most efficient way to calculate the distance between 2 integer coordinate points?
我有两个三维坐标存储为六个int
值。 计算上述位置之间的float
距离的更有效方法是:
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); }
//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); }
编辑 :这是Unity的.Distance函数,用于更好地理解:
public static float Distance(Vector3 a, Vector3 b) { Vector3 vector = new Vector3(ax - bx, ay - by, az - bz); return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.