繁体   English   中英

计算2个整数坐标点之间的距离的最有效方法?

[英]Most efficient way to calculate the distance between 2 integer coordinate points?

我有两个三维坐标存储为六个int值。 计算上述位置之间的float距离的更有效方法是:

  1. Vector3.distance( new Vector3( (float) a, (float) b, (float) c), new Vector3 ( (float) x, (float) y, (float) z)) ;
  2. 在以整数为参数的函数中实现适当的数学运算,并进行所有类型转换以浮点,所有^ 2和纯c#的平方根?

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM