Exemple #1
0
        public void Visualize(IEnumerable <RateAtDate> data, int numPoints)
        {
            const string LinearFile = "Lin.csv";
            const string CosineFile = "Cos.csv";
            const string CubicFile  = "Cub.csv";

            DateTime start = data.First().Date;

            LinearInterpolation lin = new LinearInterpolation(data);
            CosineInterpolation cos = new CosineInterpolation(data);
            CubicInterpolation  cub = new CubicInterpolation(data);

            using (StreamWriter linsw = new StreamWriter(fileRoot + LinearFile))
                using (StreamWriter cossw = new StreamWriter(fileRoot + CosineFile))
                    using (StreamWriter cubsw = new StreamWriter(fileRoot + CubicFile))
                    {
                        double increment = ((double)(data.Last().Date - data.First().Date).TotalMilliseconds) / (double)numPoints;
                        for (int i = 0; i < numPoints; i++)
                        {
                            DateTime writeDate = start + TimeSpan.FromMilliseconds((i * increment));
                            linsw.WriteLine(lin.GetRate(writeDate));
                            cossw.WriteLine(cos.GetRate(writeDate));
                            cubsw.WriteLine(cub.GetRate(writeDate));
                        }
                    }

            ShowData(fileRoot + LinearFile);
            ShowData(fileRoot + CosineFile);
            ShowData(fileRoot + CubicFile);
        }
Exemple #2
0
class main { static void Main()
             {
// To test the cubic interpolation i will compare x and y values of the function sin(x) with the splined results (as shown in plot A)
//Generation of tabulated test values
                 int    n = 100; // N data points (Arbitrary precision)
                 vector x = new vector((int)n); vector y = new vector((int)n);

                 using (StreamWriter outputFile = new StreamWriter("ManualValues.txt")){
                     for (int i = 0; i < n; i = i + 1)
                     {
                         x[i] = i * (2 * PI / n); y[i] = Sin(x[i]);
                         outputFile.WriteLine($"{x[i]} {y[i]} {1-Cos(x[i])} {Cos(x[i])}");
                     }
                 }

//Cubic interpolation
                 var CubicInterp = new CubicInterpolation(x, y); // Constructor call

                 using (StreamWriter CubicSplineOut = new StreamWriter("CubicSpline.txt")){
                     for (double z = 0; z < x[x.size - 1]; z = z + (x[x.size - 1]) / 120)
                     {
                         CubicSplineOut.WriteLine($"{z} {CubicInterp.CubicSpline(z)} {CubicInterp.CubicSplineIntegrate(z)} {CubicInterp.CubicSplineDerivative(z)}");
                     }
                 }
             }
        /// <summary>
        /// Возвращает дважды-гладкую последовательность кривых проходящих через заданные точки представленных набором структур <see cref="Point3D"/>.
        /// </summary>
        /// <param name="points">Набор структур <see cref="Point3D"/>.</param>
        /// <returns>Массив кривых.</returns>
        public static BezierCurve[] FromPoints(IList <Point3D> points)
        {
            if (points.Count < 2)
            {
                throw new ArgumentException("Количество точек должно быть больше 1.", nameof(points));
            }

            var pointsX = new PointD[points.Count];
            var pointsY = new PointD[points.Count];
            var pointsZ = new PointD[points.Count];

            for (var i = 0; i < points.Count; i++)
            {
                var point = points[i];
                pointsX[i] = new PointD(i, point.X);
                pointsY[i] = new PointD(i, point.Y);
                pointsZ[i] = new PointD(i, point.Z);
            }

            //Строем кубические сплайны X(t), Y(t), Z(t)
            var funcX = new CubicInterpolation(pointsX);
            var funcY = new CubicInterpolation(pointsY);
            var funcZ = new CubicInterpolation(pointsZ);

            var result = new BezierCurve[points.Count - 1];

            for (var i = 0; i < result.Length; i++)
            {
                var polynomX = funcX.GetPolynom(i);
                var polynomY = funcY.GetPolynom(i);
                var polynomZ = funcZ.GetPolynom(i);

                //Замена переменной
                var sub = new Polynomial(i, 1);

                var resolveX = GetBezierPoints(polynomX.Substitution(sub));
                var resolveY = GetBezierPoints(polynomY.Substitution(sub));
                var resolveZ = GetBezierPoints(polynomZ.Substitution(sub));

                result[i] = new BezierCurve(
                    new Point3D(resolveX[0], resolveY[0], resolveZ[0]),
                    new Point3D(resolveX[1], resolveY[1], resolveZ[1]),
                    new Point3D(resolveX[2], resolveY[2], resolveZ[2]),
                    new Point3D(resolveX[3], resolveY[3], resolveZ[3]));
            }

            return(result);
        }
Exemple #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x1">Interpolated Data OX (new abscissa)</param>
        /// <param name="y1">Interpolated Data OY (new ordinate)</param>
        static void ExampleWithoutArrays(out double[] x1, out double[] y1)
        {
            Console.WriteLine("Example Without Arrays : Start.\n");

            x1 = null;
            y1 = null;

            int    N  = 40;
            double Fs = 20.0;

            double[] x = new double[N];
            double[] y = new double[N];


            for (int i = 0; i < N; i++)
            {
                x[i] = i / Fs;
                y[i] = Math.Sin(2.0 * Math.PI * x[i]);
            }

            CubicInterpolation cubic = new CubicInterpolation();

            cubic.CalcCoefficients(x, y);

            x1 = new double[2 * N - 1];
            y1 = new double[2 * N - 1];
            for (int i = 0; i < 2 * N - 1; i++)
            {
                x1[i] = i / (2.0 * Fs);
                y1[i] = cubic.Interpolate(x1[i]);
            }

            Console.WriteLine("WARNING!");
            Console.WriteLine("\tYou will do this at your own risk.");
            double newX = -2.0;                                                                     /* Never do it */
            double newY = cubic.Interpolate(newX);                                                  /* Never do it */

            Console.WriteLine("\tNew (X < sourceX[0], Y) = ({0,3:F2} ; {1,3:F2})", newX, newY);

            newX = 4 * N / Fs;                                                                      /* Never do it */
            newY = cubic.Interpolate(newX);                                                         /* Never do it */
            Console.WriteLine("\tNew (X > sourceX[end], Y) = ({0,3:F2} ; {1,3:F2})", newX, newY);

            Console.WriteLine("Example Without Arrays : Enjoy.\n");
        }
Exemple #5
0
        public void Initialise(List <CamNode> points, int loops, CamTarget target)
        {
            if (points.Count == 1)
            {
                throw new CamStudioException("At least two points are required");
            }

            var iterations = loops == 0 ? 1 : loops == 1 ? 2 : 3;

            SizeOfIteration = 1D / iterations;

            var size = points.Count * iterations;

            if (iterations > 1)
            {
                size++;
            }

            var xPoints = new double[size];
            var yPoints = new double[size];
            var zPoints = new double[size];

            var yawPoints   = new double[size];
            var pitchPoints = new double[size];
            var rollPoints  = new double[size];

            var fovPoints        = new double[size];
            var saturationPoints = new double[size];
            var sepiaPoints      = new double[size];

            for (var j = 0; j < iterations; j++)
            {
                for (var i = 0; i < points.Count; i++)
                {
                    xPoints[i + j * points.Count] = points[i].X;
                    yPoints[i + j * points.Count] = points[i].Y;
                    zPoints[i + j * points.Count] = points[i].Z;

                    yawPoints[i + j * points.Count]   = points[i].Yaw;
                    pitchPoints[i + j * points.Count] = points[i].Pitch;
                    rollPoints[i + j * points.Count]  = points[i].Roll;

                    fovPoints[i + j * points.Count]        = points[i].FieldOfView;
                    saturationPoints[i + j * points.Count] = points[i].Saturation;
                    sepiaPoints[i + j * points.Count]      = points[i].Sepia;
                }
            }

            if (iterations > 1)
            {
                xPoints[points.Count * iterations] = points[0].X;
                yPoints[points.Count * iterations] = points[0].Y;
                zPoints[points.Count * iterations] = points[0].Z;

                yawPoints[points.Count * iterations]   = points[0].Yaw;
                pitchPoints[points.Count * iterations] = points[0].Pitch;
                rollPoints[points.Count * iterations]  = points[0].Roll;

                fovPoints[points.Count * iterations]        = points[0].FieldOfView;
                saturationPoints[points.Count * iterations] = points[0].Saturation;
                sepiaPoints[points.Count * iterations]      = points[0].Sepia;
            }

            _xSpline = new CubicInterpolation(xPoints);
            _ySpline = new CubicInterpolation(yPoints);
            _zSpline = new CubicInterpolation(zPoints);

            _yawSpline   = new CubicInterpolation(yawPoints);
            _pitchSpline = new CubicInterpolation(pitchPoints);
            _rollSpline  = new CubicInterpolation(rollPoints);

            _fovSpline        = new CubicInterpolation(fovPoints);
            _saturationSpline = new CubicInterpolation(saturationPoints);
            _sepiaSpline      = new CubicInterpolation(sepiaPoints);
        }