public static XMVector Normalize(XMVector v) { XMVector result = XMVector2.Length(v); float length = result.X; // Prevent divide by zero if (length > 0) { length = 1.0f / length; } result.X = v.X * length; result.Y = v.Y * length; result.Z = v.Z * length; result.W = v.W * length; return(result); }
public static XMVector LinePointDistance(XMVector linePoint1, XMVector linePoint2, XMVector point) { //// Given a vector PointVector from LinePoint1 to Point and a vector //// LineVector from LinePoint1 to LinePoint2, the scaled distance //// PointProjectionScale from LinePoint1 to the perpendicular projection //// of PointVector onto the line is defined as: //// //// PointProjectionScale = dot(PointVector, LineVector) / LengthSq(LineVector) XMVector pointVector = XMVector.Subtract(point, linePoint1); XMVector lineVector = XMVector.Subtract(linePoint2, linePoint1); XMVector lengthSq = XMVector2.LengthSquare(lineVector); XMVector pointProjectionScale = XMVector2.Dot(pointVector, lineVector); pointProjectionScale = XMVector.Divide(pointProjectionScale, lengthSq); XMVector distanceVector = XMVector.Multiply(lineVector, pointProjectionScale); distanceVector = XMVector.Subtract(pointVector, distanceVector); return(XMVector2.Length(distanceVector)); }