public static XMVector ClampLengthV(XMVector v, XMVector lengthMin, XMVector lengthMax) { Debug.Assert(lengthMin.Y == lengthMin.X, "Reviewed"); Debug.Assert(lengthMax.Y == lengthMax.X, "Reviewed"); Debug.Assert(XMVector2.GreaterOrEqual(lengthMin, XMGlobalConstants.Zero), "Reviewed"); Debug.Assert(XMVector2.GreaterOrEqual(lengthMax, XMGlobalConstants.Zero), "Reviewed"); Debug.Assert(XMVector2.GreaterOrEqual(lengthMax, lengthMin), "Reviewed"); XMVector lengthSq = XMVector2.LengthSquare(v); XMVector zero = XMVector.Zero; XMVector reciprocalLength = lengthSq.ReciprocalSqrt(); XMVector infiniteLength = XMVector.EqualInt(lengthSq, XMGlobalConstants.Infinity); XMVector zeroLength = XMVector.Equal(lengthSq, zero); XMVector length = XMVector.Multiply(lengthSq, reciprocalLength); XMVector normal = XMVector.Multiply(v, reciprocalLength); XMVector select = XMVector.EqualInt(infiniteLength, zeroLength); length = XMVector.Select(lengthSq, length, select); normal = XMVector.Select(lengthSq, normal, select); XMVector controlMax = XMVector.Greater(length, lengthMax); XMVector controlMin = XMVector.Less(length, lengthMin); XMVector clampLength = XMVector.Select(length, lengthMax, controlMax); clampLength = XMVector.Select(clampLength, lengthMin, controlMin); XMVector result = XMVector.Multiply(normal, clampLength); // Preserve the original vector (with no precision loss) if the length falls within the given range XMVector control = XMVector.EqualInt(controlMax, controlMin); result = XMVector.Select(result, v, control); 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)); }
public static XMVector Length(XMVector v) { return(XMVector2.LengthSquare(v) .Sqrt()); }
public static XMVector ReciprocalLength(XMVector v) { return(XMVector2.LengthSquare(v) .ReciprocalSqrt()); }