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


C# Vector2.GetNormal方法代码示例

本文整理汇总了C#中System.Vector2.GetNormal方法的典型用法代码示例。如果您正苦于以下问题:C# Vector2.GetNormal方法的具体用法?C# Vector2.GetNormal怎么用?C# Vector2.GetNormal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Vector2的用法示例。


在下文中一共展示了Vector2.GetNormal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: IntersectionIterator

        public override IEnumerable IntersectionIterator(Ray ray)
        {
            double radiusSquared = radius * radius;

            Vector2 rayOrigin = new Vector2(ray.origin);
            Vector2 rayDirectionXY = new Vector2(ray.direction);
            Vector2 rayDirection = rayDirectionXY.GetNormal();
            Vector2 thisPosition = Vector2.Zero;
            Vector2 deltaFromShpereCenterToRayOrigin = rayOrigin - thisPosition;
            double distanceFromCircleCenterToRayOrigin = Vector2.Dot(deltaFromShpereCenterToRayOrigin, rayDirection);
            double lengthFromRayOrginToCircleCenterSquared = Vector2.Dot(deltaFromShpereCenterToRayOrigin, deltaFromShpereCenterToRayOrigin);
            double lengthFromRayOrigintoNearEdgeOfCircleSquared = lengthFromRayOrginToCircleCenterSquared - radiusSquared;
            double distanceFromCircleCenterToRaySquared = distanceFromCircleCenterToRayOrigin * distanceFromCircleCenterToRayOrigin;
            double amountCircleCenterToRayIsGreaterThanRayOriginToEdgeSquared = distanceFromCircleCenterToRaySquared - lengthFromRayOrigintoNearEdgeOfCircleSquared;

            if (amountCircleCenterToRayIsGreaterThanRayOriginToEdgeSquared > 0)
            {
                double distanceFromRayOriginToCircleCenter = -distanceFromCircleCenterToRayOrigin;
                double amountCircleCenterToRayIsGreaterThanRayOriginToEdge = Math.Sqrt(amountCircleCenterToRayIsGreaterThanRayOriginToEdgeSquared);
                double scaleRatio = ray.direction.Length / rayDirectionXY.Length;

                if ((ray.intersectionType & IntersectionType.FrontFace) == IntersectionType.FrontFace)
                {
                    IntersectInfo info = new IntersectInfo();
                    info.hitType = IntersectionType.FrontFace;
                    info.closestHitObject = this;
                    double distanceToFrontHit = (distanceFromRayOriginToCircleCenter - amountCircleCenterToRayIsGreaterThanRayOriginToEdge) * scaleRatio;
                    info.distanceToHit = distanceToFrontHit;
                    info.hitPosition = ray.origin + ray.direction * info.distanceToHit;
                    if (info.hitPosition.z > -height / 2 && info.hitPosition.z < height / 2)
                    {
                        info.normalAtHit = new Vector3(info.hitPosition.x, info.hitPosition.y, 0).GetNormal();
                        yield return info;
                    }
                }


                if ((ray.intersectionType & IntersectionType.BackFace) == IntersectionType.BackFace)
                {
                    IntersectInfo info = new IntersectInfo();
                    info.hitType = IntersectionType.BackFace;
                    info.closestHitObject = this;
                    double distanceToBackHit = (distanceFromRayOriginToCircleCenter + amountCircleCenterToRayIsGreaterThanRayOriginToEdge) * scaleRatio;
                    info.distanceToHit = distanceToBackHit;
                    info.hitPosition = ray.origin + ray.direction * info.distanceToHit;
                    if (info.hitPosition.z > -height / 2 && info.hitPosition.z < height / 2)
                    {
                        info.normalAtHit = -(new Vector3(info.hitPosition.x, info.hitPosition.y, 0).GetNormal());
                        yield return info;
                    }
                }

                {
                    bool inFrontOfTopFace;
                    double testDistanceToHit = topPlane.GetDistanceToIntersection(ray, out inFrontOfTopFace);
                    Vector3 topHitPosition = ray.origin + ray.direction * testDistanceToHit;

                    if (topHitPosition.x * topHitPosition.x + topHitPosition.y * topHitPosition.y < topRadius * topRadius)
                    {
                        if ((ray.intersectionType & IntersectionType.FrontFace) == IntersectionType.FrontFace && inFrontOfTopFace)
                        {
                            IntersectInfo topHitInfo = new IntersectInfo();
                            topHitInfo.hitPosition = topHitPosition;
                            topHitInfo.closestHitObject = this;
                            topHitInfo.hitType = IntersectionType.FrontFace;
                            topHitInfo.normalAtHit = topPlane.planeNormal;
                            topHitInfo.distanceToHit = testDistanceToHit;

                            yield return topHitInfo;
                        }

                        if ((ray.intersectionType & IntersectionType.BackFace) == IntersectionType.BackFace && !inFrontOfTopFace)
                        {
                            IntersectInfo topHitInfo = new IntersectInfo();
                            topHitInfo.hitPosition = topHitPosition;
                            topHitInfo.closestHitObject = this;
                            topHitInfo.hitType = IntersectionType.BackFace;
                            topHitInfo.normalAtHit = -topPlane.planeNormal;
                            topHitInfo.distanceToHit = testDistanceToHit;

                            yield return topHitInfo;
                        }
                    }
                }

                {
                    bool inFrontOfBottomFace;
                    double testDistanceToHit = bottomPlane.GetDistanceToIntersection(ray, out inFrontOfBottomFace);
                    Vector3 bottomHitPosition = ray.origin + ray.direction * testDistanceToHit;

                    if (bottomHitPosition.x * bottomHitPosition.x + bottomHitPosition.y * bottomHitPosition.y < radius * radius)
                    {
                        if ((ray.intersectionType & IntersectionType.FrontFace) == IntersectionType.FrontFace && inFrontOfBottomFace)
                        {
                            IntersectInfo bottomHitInfo = new IntersectInfo();
                            bottomHitInfo.hitPosition = bottomHitPosition;
                            bottomHitInfo.closestHitObject = this;
                            bottomHitInfo.hitType = IntersectionType.FrontFace;
                            bottomHitInfo.normalAtHit = bottomPlane.planeNormal;
                            bottomHitInfo.distanceToHit = testDistanceToHit;
//.........这里部分代码省略.........
开发者ID:jeske,项目名称:agg-sharp,代码行数:101,代码来源:Cylinder.cs

示例2: GetClosestIntersection

        public override IntersectInfo GetClosestIntersection(Ray ray)
        {
            double radiusSquared = radius * radius;

            Vector2 rayOrigin = new Vector2(ray.origin);
            Vector2 rayDirectionXY = new Vector2(ray.direction);
            Vector2 rayDirection = rayDirectionXY.GetNormal();
            Vector2 thisPosition = Vector2.Zero;
            Vector2 deltaFromShpereCenterToRayOrigin = rayOrigin - thisPosition;
            double distanceFromCircleCenterToRayOrigin = Vector2.Dot(deltaFromShpereCenterToRayOrigin, rayDirection); // negative means the Circle is in front of the ray.
            double lengthFromRayOrginToCircleCenterSquared = Vector2.Dot(deltaFromShpereCenterToRayOrigin, deltaFromShpereCenterToRayOrigin);
            double lengthFromRayOrigintoNearEdgeOfCircleSquared = lengthFromRayOrginToCircleCenterSquared - radiusSquared;
            double distanceFromCircleCenterToRaySquared = distanceFromCircleCenterToRayOrigin * distanceFromCircleCenterToRayOrigin;
            double amountCircleCenterToRayIsGreaterThanRayOriginToEdgeSquared = distanceFromCircleCenterToRaySquared - lengthFromRayOrigintoNearEdgeOfCircleSquared;

            if (amountCircleCenterToRayIsGreaterThanRayOriginToEdgeSquared > 0)
            {
                {
                    bool inFrontOfTop;
                    double testDistanceToHit = topPlane.GetDistanceToIntersection(ray, out inFrontOfTop);
                    bool wantFrontAndInFront = (ray.intersectionType & IntersectionType.FrontFace) == IntersectionType.FrontFace && inFrontOfTop;
                    bool wantBackAndInBack = (ray.intersectionType & IntersectionType.BackFace) == IntersectionType.BackFace && !inFrontOfTop;
                    if (wantFrontAndInFront || wantBackAndInBack)
                    {
                        Vector3 topHitPosition = ray.origin + ray.direction * testDistanceToHit;

                        if (topHitPosition.x * topHitPosition.x + topHitPosition.y * topHitPosition.y < topRadius * topRadius)
                        {
                            IntersectInfo topHitInfo = new IntersectInfo();
                            topHitInfo.hitPosition = topHitPosition;
                            topHitInfo.closestHitObject = this;
                            if (ray.intersectionType == IntersectionType.FrontFace)
                            {
                                topHitInfo.hitType = IntersectionType.FrontFace;
                                topHitInfo.normalAtHit = topPlane.planeNormal;
                            }
                            else
                            {
                                topHitInfo.hitType = IntersectionType.BackFace;
                                topHitInfo.normalAtHit = -topPlane.planeNormal;
                            }
                            topHitInfo.distanceToHit = testDistanceToHit;

                            return topHitInfo;
                        }
                    }
                }

                {
                    bool inFrontOfBottom;
                    double testDistanceToHit = bottomPlane.GetDistanceToIntersection(ray, out inFrontOfBottom);
                    if (ray.intersectionType == IntersectionType.FrontFace && inFrontOfBottom
                        || ray.intersectionType == IntersectionType.BackFace && !inFrontOfBottom)
                    {
                        Vector3 bottomHitPosition = ray.origin + ray.direction * testDistanceToHit;

                        if (bottomHitPosition.x * bottomHitPosition.x + bottomHitPosition.y * bottomHitPosition.y < radius * radius)
                        {
                            IntersectInfo bottomHitInfo = new IntersectInfo();
                            bottomHitInfo.hitPosition = bottomHitPosition;
                            bottomHitInfo.closestHitObject = this;
                            if (ray.intersectionType == IntersectionType.FrontFace)
                            {
                                bottomHitInfo.hitType = IntersectionType.FrontFace;
                                bottomHitInfo.normalAtHit = bottomPlane.planeNormal;
                            }
                            else
                            {
                                bottomHitInfo.hitType = IntersectionType.BackFace;
                                bottomHitInfo.normalAtHit = -bottomPlane.planeNormal;
                            }
                            bottomHitInfo.distanceToHit = testDistanceToHit;

                            return bottomHitInfo;
                        }
                    }
                }

                IntersectInfo info = new IntersectInfo();
                info.closestHitObject = this;
                info.hitType = IntersectionType.FrontFace;
                if (ray.isShadowRay)
                {
                    return info;
                }
                double distanceFromRayOriginToCircleCenter = -distanceFromCircleCenterToRayOrigin;

                double amountCircleCenterToRayIsGreaterThanRayOriginToEdge = Math.Sqrt(amountCircleCenterToRayIsGreaterThanRayOriginToEdgeSquared);

                double scaleRatio = ray.direction.Length / rayDirectionXY.Length;

                if (ray.intersectionType == IntersectionType.FrontFace)
                {
                    double distanceToFrontHit = (distanceFromRayOriginToCircleCenter - amountCircleCenterToRayIsGreaterThanRayOriginToEdge) * scaleRatio;
                    if (distanceToFrontHit > ray.maxDistanceToConsider || distanceToFrontHit < ray.minDistanceToConsider)
                    {
                        return null;
                    }
                    info.distanceToHit = distanceToFrontHit;
                    info.hitPosition = ray.origin + ray.direction * info.distanceToHit;
//.........这里部分代码省略.........
开发者ID:jeske,项目名称:agg-sharp,代码行数:101,代码来源:Cylinder.cs


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