向量表示的是方向和大小,与位置距离无关
三维空间的表示如下
在unity3d中采用的struct来描述的Vector3
namespace UnityEngine
{
public struct Vector3
{
public float x;
public float y;
public float z;
}
}
向量的长度:向量的大小(或长度)称为向量的模
public float magnitude
{
get
{
return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}
public float sqrMagnitude
{
get
{
return this.x * this.x + this.y * this.y + this.z * this.z;
}
}
三维空间中两点的距离
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);
}
向量加法