AnimationDeveloperSamples.AnimationUtils.SimplifyPath2D C# (CSharp) Method

SimplifyPath2D() private static method

private static SimplifyPath2D ( IGeometry path, bool bReverse, double simpFactor ) : IGeometry
path IGeometry
bReverse bool
simpFactor double
return IGeometry
        private static IGeometry SimplifyPath2D(IGeometry path, bool bReverse, double simpFactor)
        {
            IGeometry oldPath = path;
            IPointCollection oldPointCollection = oldPath as IPointCollection;
            IPolyline newPath = new PolylineClass();
            IPointCollection newPointCollection = newPath as IPointCollection;
            ISpatialReference sr = oldPath.SpatialReference;

            int pointCount;
            pointCount = oldPointCollection.PointCount;
            double[] lastCoord = new double[2];

            IPoint beginningPoint = new PointClass();
            oldPointCollection.QueryPoint(bReverse ? (pointCount - 1) : 0, beginningPoint);
            beginningPoint.QueryCoords(out lastCoord[0], out lastCoord[1]);

            bool bKeep = true;

            IPolyline oldLine = oldPath as IPolyline;
            double length = oldLine.Length;

            object Missing = Type.Missing;
            newPointCollection.AddPoint(beginningPoint, ref Missing, ref Missing);

            for (int i = 1; i < pointCount - 1; i++) //simplify 2D path
            {
                double[] coord = new double[2];
                IPoint currentPoint = new PointClass();
                oldPointCollection.QueryPoint(bReverse ? (pointCount - i - 1) : i, currentPoint);
                currentPoint.QueryCoords(out coord[0], out coord[1]);

                double[] d = new double[2];
                d[0] = coord[0] - lastCoord[0];
                d[1] = coord[1] - lastCoord[1];

                double distance;
                distance = Math.Sqrt(d[0] * d[0] + d[1] * d[1]);

                if (distance < (0.25 * simpFactor * length))
                {
                    bKeep = false;
                }
                else
                {
                    bKeep = true;
                }

                if (bKeep)
                {
                    newPointCollection.AddPoint(currentPoint, ref Missing, ref Missing);
                    lastCoord[0] = coord[0];
                    lastCoord[1] = coord[1];
                }
            }

            IPoint finalPoint = new PointClass();
            oldPointCollection.QueryPoint(bReverse ? 0 : (pointCount - 1), finalPoint);
            newPointCollection.AddPoint(finalPoint, ref Missing, ref Missing);

            newPath.SpatialReference = sr;
            return (IGeometry)newPath;
        }
    }