计算一个向量的值
var vectorMagnitude = Math.sqrt(Math.pow(vector.x, 2) +
Math.pow(vector.y, 2));
单位向量
var vectorMagnitude = Math.sqrt(Math.pow(vector.x, 2) +
Math.pow(vector.y, 2)),
unitVector = new Vector();
unitVector.x = vector.x / vectorMagnitude;
unitVector.y = vector.y / vectorMagnitude;
向量相加
var vectorSum = new Vector();
vectorSum.x = vectorOne.x + vectorTwo.x;
vectorSum.y = vectorOne.y + vectorTwo.y;
向量相减
var vectorSubtraction = new Vector();
vectorSubtraction.x = vectorOne.x - vectorTwo.x;
vectorSubtraction.y = vectorOne.y - vectorTwo.y;
向量的乘积
向量的乘积是个标量,它代表了两个向量是否在同一个方向
>0 同方向
<0 反方向
var dotProduct = vectorOne.x * vectorTwo.x + vectorOne.y * vectorTwo.y;