本文整理汇总了C#中UnityEngine.Vector2.SqrDist方法的典型用法代码示例。如果您正苦于以下问题:C# Vector2.SqrDist方法的具体用法?C# Vector2.SqrDist怎么用?C# Vector2.SqrDist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Vector2
的用法示例。
在下文中一共展示了Vector2.SqrDist方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DistanceToLineSegmentSquared
// Same as above, but avoid using Sqrt(), saves a new nanoseconds in cases where you only want
// to compare several distances to find the smallest or largest, but don't need the distance
public static float DistanceToLineSegmentSquared(Vector2 A, Vector2 B, Vector2 P, out Vector2 closestPoint)
{
// Compute length of line segment (squared) and handle special case of coincident points
float segmentLengthSquared = A.SqrDist(B);
if (segmentLengthSquared < 1E-7f) // Arbitrary "close enough for government work" value
{
closestPoint = A;
return P.SqrDist(closestPoint);
}
// Use the magic formula to compute the "projection" of this point on the infinite line
Vector2 lineSegment = B - A;
float t = Vector2.Dot(P - A, lineSegment) / segmentLengthSquared;
// Handle the two cases where the projection is not on the line segment, and the case where
// the projection is on the segment
if (t <= 0)
closestPoint = A;
else if (t >= 1)
closestPoint = B;
else
closestPoint = A + (lineSegment * t);
return P.SqrDist(closestPoint);
}