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


C# Vec2.Normalize方法代码示例

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


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

示例1: OkuboBoundary

        protected PointSet<Point> OkuboBoundary(int time)
        {
            Vector3 center3;
            _cores[_selectedCore].DistanceToPointInZ(Vector3.UnitZ * time, out center3);
            Vec2 center = ((Vec3)center3).ToVec2();

            // Compute Okubo Weiss field for goal slice.
            if (_okubo == null)
            {
                if (time > _velocity.TimeOrigin || time < _velocity.TimeOrigin + _velocity.Size.T)
                    LoadField(time, MemberMain, 1);
                _okubo = new VectorField(_velocity.GetTimeSlice(time), FieldAnalysis.OkuboWeiss, 1, true)[0] as ScalarField;
                if (_compareSlice != null && _compareSlice.NumTextures < 3)
                    _compareSlice.AddScalar(_okubo);
                float fill, mean;
                _okubo.ComputeStatistics(out fill, out mean, out _standardDerivation);
            }
            float threshold = _standardDerivation * (-0.2f);
            Debug.Assert(_okubo.Sample(center) < threshold);

            // Run into each direction and cross Okubo.
            Point[] circleOW = new Point[LineX];
            float angleDiff = 2 * (float)(Math.PI / LineX);
            for (int dir = 0; dir < LineX; ++dir)
            {
                float x = (float)(Math.Sin(angleDiff * dir + Math.PI / 2));
                float y = (float)(Math.Cos(angleDiff * dir + Math.PI / 2));

                Vec2 ray = new Vec2(x, y);
                ray.Normalize();
                ray *= StepSize;
                int step = 0;

                while (true) // Hehe!
                {
                    Vec2 pos = center + ray * step++;
                    if (!_okubo.IsValid(pos) || _okubo.Sample(pos) >= threshold)
                    {
                        pos = center + ray * (step - 2);
                        circleOW[dir] = new Point(new Vector3(pos.X, pos.Y, time)) { Color = new Vector3(0.1f, 0.1f, 0.9f) };
                        break;
                    }
                }
            }

            return new PointSet<Point>(circleOW);
        }
开发者ID:AnkeAnke,项目名称:FlowSharp,代码行数:47,代码来源:CoreDistanceMapper.cs

示例2: TriangulatePolygon

            /**
             * Triangulates a polygon using simple ear-clipping algorithm. Returns
             * size of Triangle array unless the polygon can't be triangulated.
             * This should only happen if the polygon self-intersects,
             * though it will not _always_ return null for a bad polygon - it is the
             * caller's responsibility to check for self-intersection, and if it
             * doesn't, it should at least check that the return value is non-null
             * before using. You're warned!
                 *
                 * Triangles may be degenerate, especially if you have identical points
                 * in the input to the algorithm.  Check this before you use them.
             *
             * This is totally unoptimized, so for large polygons it should not be part
             * of the simulation loop.
             *
             * Returns:
             * -1 if algorithm fails (self-intersection most likely)
             * 0 if there are not enough vertices to triangulate anything.
             * Number of triangles if triangulation was successful.
             *
             * results will be filled with results - ear clipping always creates vNum - 2
             * or fewer (due to pinch point polygon snipping), so allocate an array of
                 * this size.
             */
            public static int TriangulatePolygon(List<float> xv, List<float> yv, List<Triangle> results)
            {
                if (xv.Count < 3)
                    return 0;

                //Recurse and split on pinch points
                Polygon pA = new Polygon(), pB = new Polygon();
                Polygon pin = new Polygon(xv, yv);
                if (ResolvePinchPoint(pin, ref pA, ref pB))
                {
                    List<Triangle> mergeA = new List<Triangle>();
                    List<Triangle> mergeB = new List<Triangle>();
                    int nA = TriangulatePolygon(pA.x, pA.y, mergeA);
                    int nB = TriangulatePolygon(pB.x, pB.y, mergeB);
                    if (nA==-1 || nB==-1)
                        return -1;
                    for (int i=0; i<nA; ++i)
                        results.Add(mergeA[i]);
                    for (int i=0; i<nB; ++i)
                        results.Add(mergeB[i]);
                    return (nA+nB);
                }

                int vNum = xv.Count;
                List<Triangle> buffer = new List<Triangle>();
                float[] xrem = new float[vNum];
                float[] yrem = new float[vNum];
                for (int i = 0; i < vNum; ++i)
                {
                    xrem[i] = xv[i];
                    yrem[i] = yv[i];
                }

                int xremLength = vNum;

                while (vNum > 3)
                {
                    // Find an ear
                    int earIndex = -1;
                    //float earVolume = -1.0f;
                    float earMaxMinCross = -10.0f;
                    for (int i = 0; i < vNum; ++i)
                    {
                        if (IsEar(i, xrem, yrem))
                        {
                            int lower = remainder(i-1, vNum);
                            int upper = remainder(i+1, vNum);
                            Vec2 d1 = new Vec2(xrem[upper]-xrem[i], yrem[upper]-yrem[i]);
                            Vec2 d2 = new Vec2(xrem[i]-xrem[lower], yrem[i]-yrem[lower]);
                            Vec2 d3 = new Vec2(xrem[lower]-xrem[upper], yrem[lower]-yrem[upper]);

                            d1.Normalize();
                            d2.Normalize();
                            d3.Normalize();
                            float cross12 = Math.Abs(d1.Cross(d2));
                            float cross23 = Math.Abs(d2.Cross(d3));
                            float cross31 = Math.Abs(d3.Cross(d1));
                            //Find the maximum minimum angle
                            float minCross = Math.Min(cross12, Math.Min(cross23, cross31));
                            if (minCross > earMaxMinCross)
                            {
                                earIndex = i;
                                earMaxMinCross = minCross;
                            }

                            /*//This bit chooses the ear with greatest volume first
                            float testVol = b2Abs( d1.X*d2.Y-d2.X*d1.Y );
                            if (testVol > earVolume){
                                    earIndex = i;
                                    earVolume = testVol;
                            }*/
                        }
                    }

                    // If we still haven't found an ear, we're screwed.
                    // Note: sometimes this is happening because the
//.........这里部分代码省略.........
开发者ID:RubisetCie,项目名称:box2c,代码行数:101,代码来源:ConvexDecomposition.cs

示例3: Initialize

            public void Initialize(SimplexCache cache,
                DistanceProxy proxyA, Transform transformA,
                DistanceProxy proxyB, Transform transformB)
            {
                _proxyA = proxyA;
                _proxyB = proxyB;
                int count = cache.Count;
                Box2DXDebug.Assert(0 < count && count < 3);

                if (count == 1)
                {
                    _type = Type.Points;
                    Vec2 localPointA = _proxyA.GetVertex(cache.IndexA[0]);
                    Vec2 localPointB = _proxyB.GetVertex(cache.IndexB[0]);
                    Vec2 pointA = Math.Mul(transformA, localPointA);
                    Vec2 pointB = Math.Mul(transformB, localPointB);
                    _axis = pointB - pointA;
                    _axis.Normalize();
                }
                else if (cache.IndexB[0] == cache.IndexB[1])
                {
                    // Two points on A and one on B
                    _type = Type.FaceA;
                    Vec2 localPointA1 = _proxyA.GetVertex(cache.IndexA[0]);
                    Vec2 localPointA2 = _proxyA.GetVertex(cache.IndexA[1]);
                    Vec2 localPointB = _proxyB.GetVertex(cache.IndexB[0]);
                    _localPoint = 0.5f*(localPointA1 + localPointA2);
                    _axis = Vec2.Cross(localPointA2 - localPointA1, 1.0f);
                    _axis.Normalize();

                    Vec2 normal = Math.Mul(transformA.R, _axis);
                    Vec2 pointA = Math.Mul(transformA, _localPoint);
                    Vec2 pointB = Math.Mul(transformB, localPointB);

                    float s = Vec2.Dot(pointB - pointA, normal);
                    if (s < 0.0f)
                    {
                        _axis = -_axis;
                    }
                }
                else if (cache.IndexA[0] == cache.IndexA[1])
                {
                    // Two points on B and one on A.
                    _type = Type.FaceB;
                    Vec2 localPointA = proxyA.GetVertex(cache.IndexA[0]);
                    Vec2 localPointB1 = proxyB.GetVertex(cache.IndexB[0]);
                    Vec2 localPointB2 = proxyB.GetVertex(cache.IndexB[1]);
                    _localPoint = 0.5f*(localPointB1 + localPointB2);
                    _axis = Vec2.Cross(localPointB2 - localPointB1, 1.0f);
                    _axis.Normalize();

                    Vec2 normal = Math.Mul(transformB.R, _axis);
                    Vec2 pointB = Math.Mul(transformB, _localPoint);
                    Vec2 pointA = Math.Mul(transformA, localPointA);

                    float s = Vec2.Dot(pointA - pointB, normal);
                    if (s < 0.0f)
                    {
                        _axis = -_axis;
                    }
                }
                else
                {
                    // Two points on B and two points on A.
                    // The faces are parallel.
                    Vec2 localPointA1 = _proxyA.GetVertex(cache.IndexA[0]);
                    Vec2 localPointA2 = _proxyA.GetVertex(cache.IndexA[1]);
                    Vec2 localPointB1 = _proxyB.GetVertex(cache.IndexB[0]);
                    Vec2 localPointB2 = _proxyB.GetVertex(cache.IndexB[1]);

                    Vec2 pA = Math.Mul(transformA, localPointA1);
                    Vec2 dA = Math.Mul(transformA.R, localPointA2 - localPointA1);
                    Vec2 pB = Math.Mul(transformB, localPointB1);
                    Vec2 dB = Math.Mul(transformB.R, localPointB2 - localPointB1);

                    float a = Vec2.Dot(dA, dA);
                    float e = Vec2.Dot(dB, dB);
                    Vec2 r = pA - pB;
                    float c = Vec2.Dot(dA, r);
                    float f = Vec2.Dot(dB, r);

                    float b = Vec2.Dot(dA, dB);
                    float denom = a*e - b*b;

                    float s = 0.0f;
                    if (denom != 0.0f)
                    {
                        s = Math.Clamp((b*f - c*e)/denom, 0.0f, 1.0f);
                    }

                    float t = (b*s + f)/e;

                    if (t < 0.0f)
                    {
                        t = 0.0f;
                        s = Math.Clamp(-c/a, 0.0f, 1.0f);
                    }
                    else if (t > 1.0f)
                    {
                        t = 1.0f;
//.........这里部分代码省略.........
开发者ID:rbrother,项目名称:seikkailulaakso,代码行数:101,代码来源:Collision.TimeOfImpact.cs


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