当前位置: 首页>>代码示例>>C#>>正文


C# Vector2.SqrDist方法代码示例

本文整理汇总了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);
        }
开发者ID:cdeeg,项目名称:ChickenTikkaMassacre,代码行数:26,代码来源:Maths.cs


注:本文中的UnityEngine.Vector2.SqrDist方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。