Ejemplo n.º 1
0
        public static Trapezium LocalToWorld(int x, int y, Trapezium trap, Vector2 dx)
        {
            var p00 = LocalToWorld(x, y, trap.p00, dx);
            var p10 = LocalToWorld(x + 1, y, trap.p10, dx);
            var p01 = LocalToWorld(x, y + 1, trap.p01, dx);
            var p11 = LocalToWorld(x + 1, y + 1, trap.p11, dx);

            return(new Trapezium(p00, p10, p01, p11));
        }
        public Trapezium GenerateTrapezium()
        {
            var trapezium = new Trapezium()
            {
                CenterOfMass = GeneratePoint(),
                Height       = _random.Next(PositiveDiapason),
                LongBase     = _random.Next(PositiveDiapason),
                ShortBase    = _random.Next(PositiveDiapason)
            };

            return(trapezium);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var rectangle = new Rectangle(5, 6);
            var circle    = new Circle(3 / Math.Sqrt(Math.PI));
            var square    = new Square(2);
            var trapezium = new Trapezium(3, 7, 5, 4);
            var triangle  = new Triangle(17, 17, 30);
            var shapes    = new HashSet <IShape>()
            {
                circle, rectangle, square, trapezium, triangle
            };
            var shapeService = new ShapeService();

            Console.WriteLine(shapeService.CalculateSummaryArea(shapes));
            Console.WriteLine(shapeService.CalculateSummaryPerimeter(shapes));
        }
Ejemplo n.º 4
0
        public static void TestOperationsTrapezium()
        {
            var t1 = new Trapezium(2, 2, 3);
            var t2 = new Trapezium(3, 2, 2);

            Console.WriteLine($" t1 + t2 = {t1 * 2}");
            Console.WriteLine($" t1 / t2 = {t1 / 2}");
            Console.WriteLine($" t1 == t2 = {t1 == t2}");
            Console.WriteLine($" t1.Hashcode = {t1.GetHashCode()}");
            Console.WriteLine($" t2.Hashcode = {t2.GetHashCode()}");
            Console.WriteLine($" t1.square = {t1.square}");
            Console.WriteLine($" t1.perimeter = {t1.perimeter}");
            Console.WriteLine($" t1.gravityCenter = {t1.gravityCenter}");
            Console.WriteLine($" t2.square = {t2.square}");
            Console.WriteLine($" t2.perimeter = {t2.perimeter}");
            Console.WriteLine($" t2.gravityCenter = {t2.gravityCenter}");
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Point A = new Point(0, 0);
            Point B = new Point(0, 8);
            Point C = new Point(8, 0);
            Point D = new Point(8, 8);

            try
            {
                Ellipse ellips = new Ellipse(A, B, C);
                Console.WriteLine(ellips);

                Circle circle = new Circle(A, C);
                Console.WriteLine(circle);

                Trapezium trapezium = new Trapezium(A, B, C, D);
                Console.WriteLine(trapezium);

                Triangle trianle = new Triangle(A, B, C);
                Console.WriteLine(trianle);

                Parallelogram parallelogram = new Parallelogram(A, B, C, D);
                Console.WriteLine(parallelogram);

                Rectangle rectangle = new Rectangle(A, B, C, D);
                Console.WriteLine(rectangle);

                Rhombus rhombus = new Rhombus(A, B, C, D);
                Console.WriteLine(rhombus);

                Square square = new Square(A, B, C, D);
                Console.WriteLine(square);
            }
            catch (ApplicationException e)
            {
                Console.WriteLine(" {0} ", e.Message);
            }
            finally
            {
                Console.WriteLine("\nBy!\n");
            }

            Console.ReadKey();
        }
Ejemplo n.º 6
0
    public static Trapezium Parse(SqlString s)
    {
        Trapezium rv = new Trapezium();

        string[] tmp = s.Value.Split(",".ToCharArray());
        rv.upper_base = double.Parse(tmp[0]);
        rv.lower_base = double.Parse(tmp[1]);
        rv.height     = double.Parse(tmp[2]);
        rv.Field      = 0.5 * (rv.upper_base + rv.lower_base) * rv.height;

        if (rv.ValidateTrapezium() == false)
        {
            throw new ArgumentException("Invalid input values");
        }
        if (s.IsNull)
        {
            return(Null);
        }
        else
        {
            return(rv);
        }
    }
Ejemplo n.º 7
0
        public static void Trapezium()
        {
            Console.OutputEncoding = Encoding.UTF8;
            try
            {
                Console.WriteLine("Enter the value of points x1, y1: ");
                x1 = int.Parse(Console.ReadLine());
                y1 = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter the value of points x2,y2: ");
                x2 = int.Parse(Console.ReadLine());
                y2 = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter the value of points x3,y3: ");
                x3 = int.Parse(Console.ReadLine());
                y3 = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter the value of points x4,y4: ");
                x4 = int.Parse(Console.ReadLine());
                y4 = int.Parse(Console.ReadLine());

                Trapezium trapezium = new Trapezium(x1, x2, x3, x4, y1, y2, y3, y4);

                if (trapezium.FigureArea() is 0)
                {
                    ExeptionFilter.ExeptionOutputENG();
                    ChangeTrapezium();
                }
                else
                {
                    trapezium.DrawENG();
                    SelectENG.NextChangeENG();
                }
            }
            catch
            {
                ExeptionFilter.ExeptionOutputENG();
                Trapezium();
            }
        }
Ejemplo n.º 8
0
        public static void Trapezium()
        {
            Console.OutputEncoding = Encoding.UTF8;
            try
            {
                Console.WriteLine("Введите значение точек x1, y1: ");
                x1 = int.Parse(Console.ReadLine());
                y1 = int.Parse(Console.ReadLine());
                Console.WriteLine("Введите значение точек x2,y2: ");
                x2 = int.Parse(Console.ReadLine());
                y2 = int.Parse(Console.ReadLine());
                Console.WriteLine("Введите значение точек x3,y3: ");
                x3 = int.Parse(Console.ReadLine());
                y3 = int.Parse(Console.ReadLine());
                Console.WriteLine("Введите значение точек x4,y4: ");
                x4 = int.Parse(Console.ReadLine());
                y4 = int.Parse(Console.ReadLine());

                Trapezium trapezium = new Trapezium(x1, x2, x3, x4, y1, y2, y3, y4);

                if (trapezium.FigureArea() is 0)
                {
                    ExeptionFilter.ExeptionOutputENG();
                    ChangeTrapezium();
                }
                else
                {
                    trapezium.Draw();
                    SelectRUS.NextChange();
                }
            }
            catch
            {
                ExeptionFilter.ExeptionOutputENG();
                Trapezium();
            }
        }
Ejemplo n.º 9
0
        public void CalculateSummaryPerimeter()
        {
            var rectangle  = new Rectangle(6, 6);
            var circle     = new Circle(3 / Math.PI);
            var rectangle1 = new Rectangle(6, 6);
            var circle1    = new Circle(3 / Math.PI);
            var square     = new Square(2);
            var square1    = new Square(2);
            var trapezium  = new Trapezium(2, 2, 2, 2);
            var trapezium1 = new Trapezium(2, 2, 2, 2);
            var triangle   = new Triangle(17, 17, 30);
            var triangle1  = new Triangle(17, 17, 30);
            var shapes     = new HashSet <IShape>()
            {
                circle, circle1,
                rectangle, rectangle1,
                square, square1,
                trapezium, trapezium1,
                triangle, triangle1
            };

            //Expected result calculated by me
            Assert.Equal(60, _shapeService.CalculateSummaryPerimeter(shapes));
        }
 public double Accept(Trapezium trapezium) => trapezium.TrapeziumPerimeter;
Ejemplo n.º 11
0
 public double Accept(Trapezium trapezium) => trapezium.TrapeziumArea;
Ejemplo n.º 12
0
        public void ExecuteMethod(
            string nameMethod,
            double param1,
            double param2,
            double param3,
            double param4,
            string testFunction,
            int rangeArray,
            double[] LinSysMasA,
            double[,] LinSysMatrixB,
            double[] massX,
            double[] massF,
            double[] massW,
            double pointInterpolation,
            double[,]  MatrixAlgebraA,
            double pointPercentile,
            double pointGenerator)
        {
            TestFunction = testFunction;
            switch (nameMethod)
            {
            //*** Approximate decision of equalization f(x)=0 ***
            case "Bisection Method":
            {
                Bisection bisect = new Bisection(new FunctionOne(TestFunBisection), param1, param2, param3);
                result = "\n Result of the program: \n" +
                         "    x= " + string.Format("{0:f" + precision + "}", bisect.Result);
            }
            break;

            case "Chord Method":
            {
                Сhord chord = new Сhord(new FunctionOne(TestFunNewton), new FunctionOne(TestFunNewton2), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", chord.GetSolution());
            }
            break;

            case "Iteration Method":
            {
                IterationMethod itermet = new IterationMethod(new FunctionOne(TestFunIteration), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", itermet.GetSolution());
            }
            break;

            case "Newton Method":
            {
                Newton newton = new Newton(new FunctionOne(TestFunNewton), new FunctionOne(TestFunNewton2), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", newton.GetSolution());
            }
            break;

            //*** Differential Equations ***
            case "Euler Simple":
            {
                EulerSimple eulersimpl        = new EulerSimple(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var         result_eulersimpl = eulersimpl.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulersimpl[0, j])
                             + " : "
                             + string.Format("{0:f" + precision + "}", result_eulersimpl[1, j]) + "\n";
                }
            }
            break;

            case "Euler Modified":
            {
                EulerModified eulerModif        = new EulerModified(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var           result_eulerModif = eulerModif.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulerModif[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_eulerModif[1, j]) + "\n";
                    // result = result + "fun=\n";
                    // result = result +Convert.ToString( TestFunDifferEquations);
                }
            }
            break;

            case "Euler Corrected":
            {
                EulerCorrected eulerCorrect        = new EulerCorrected(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var            result_eulerCorrect = eulerCorrect.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulerCorrect[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_eulerCorrect[1, j]) + "\n";
                }
            }
            break;

            case "Runge-Kutta4":
            {
                RungeKutta4 rungeKutta4        = new RungeKutta4(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var         result_rungeKutta4 = rungeKutta4.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_rungeKutta4[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_rungeKutta4[1, j]) + "\n";
                }
            }
            break;

            //*** Integration ***
            case "Chebishev":
                Chebishev chebish        = new Chebishev(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var       result_chebish = chebish.GetSolution();

                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_chebish[1, j]) + "   \t   integral = "
                             + string.Format("{0:f" + precision + "}", result_chebish[0, j]);
                }
                break;

            case "Simpson":
                Simpson simps        = new Simpson(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var     result_simps = simps.GetSolution();
                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_simps[1, j]) +
                             " \t   integral = " + string.Format("{0:f" + precision + "}", result_simps[0, j]);
                }
                break;

            case "Simpson2":
                Simpson2 simps2        = new Simpson2(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var      result_simps2 = simps2.GetSolution();
                result = "\n\n  integral = " + string.Format("{0:f" + precision + "}", result_simps2);
                break;

            case "Trapezium":
                Trapezium trapez        = new Trapezium(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var       result_trapez = trapez.GetSolution();
                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_trapez[1, j])
                             + " \t   integral = " + string.Format("{0:f" + precision + "}", result_trapez[0, j]);
                }
                break;

            //*** Non Linear equalization ***
            case "Half Division":
                HalfDivision halfdiv = new HalfDivision(new FunctionOne(TestFunNonLinearEquations), param1, param2, param3);
                result = "\n    X = " + string.Format("{0:f" + precision + "}", halfdiv.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", halfdiv.GetSolution()[1, 0]);
                break;

            case "Hord Metod":
                HordMetod hormet = new HordMetod(new FunctionOne(TestFunNonLinearEquations), param1, param2, Convert.ToInt32(param3));
                result = "\n    X = " + string.Format("{0:f" + precision + "}", hormet.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", hormet.GetSolution()[1, 0]);
                break;

            case "Newton Metod":
                NewtonMethod newt = new NewtonMethod(new FunctionOne(TestFunNonLinearEquations), param1, param2);
                result = "\n     X = " + string.Format("{0:f" + precision + "}", newt.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", newt.GetSolution()[1, 0]);
                break;

            case "Secant Metod":
                Secant sec = new Secant(new FunctionOne(TestFunNonLinearEquations), param1, param2);
                result = "\n     X = " + string.Format("{0:f" + precision + "}", sec.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", sec.GetSolution()[1, 0]);
                break;

            default:
                result = "";
                break;

            //*** Linear Systems ***
            case "Gaus":
                double[,] LinSysMatrix;
                LinSysMatrix = new double[100, 100];
                for (int l = 0; l < rangeArray; l++)
                {
                    for (int j = 0; j < rangeArray; j++)
                    {
                        LinSysMatrix[l, j] = LinSysMatrixB[l, j];
                    }
                    LinSysMatrix[l, rangeArray] = LinSysMasA[l];
                }
                Gaus gaus        = new Gaus(4, LinSysMatrix);
                var  result_gaus = gaus.GetSolution();
                result = "";
                for (int j = 0; j < result_gaus.Length; j++)
                {
                    result = result + "\n         X" + j + "  =  "
                             + string.Format("{0:f" + precision + "}", result_gaus[j]) + ";";
                }
                break;

            case "Zeidel":
                Zeidel zeidel        = new Zeidel(4, LinSysMatrixB, LinSysMasA);
                var    result_zeidel = zeidel.GetSolution();
                result = "";
                for (int j = 0; j < result_zeidel.Length; j++)
                {
                    result = result + "\n         X" + j + "  =  "
                             + string.Format("{0:f" + precision + "}", result_zeidel[j]) + ";";
                }
                break;

            //*** Interpolation ***
            case "Lagrange Interpolator":

                LagrangeInterpolator lagran = new LagrangeInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", lagran.GetSolution());
                break;

            case "Newton Interpolator":

                NewtonInterpolator newinterpol = new NewtonInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", newinterpol.GetSolution());
                break;

            case "Neville Interpolator":

                NevilleInterpolator newill = new NevilleInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", newill.GetSolution());
                break;

            case "Spline Interpolator":
                SplineInterpolator spline = new SplineInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", spline.GetSolution());
                break;

            case "Barycentric Interpolator":

                BarycentricInterpolation barycen = new BarycentricInterpolation(massX, massF, massW, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", barycen.GetSolution());
                break;

            //*** Matrix Algebra ***
            case "Matrix Determinant":
                MatrixDeterminant matrdet = new MatrixDeterminant();
                result = " \n\n\n  Determinant =  " + string.Format("{0:f" + precision + "}", matrdet.MatrixDet(MatrixAlgebraA, rangeArray)) + ";\n";
                break;

            case "RMatrix LU":

                MatrixLU matrlu         = new MatrixLU(MatrixAlgebraA, rangeArray, rangeArray);
                var      result_matrlu  = matrlu.GetSolution();
                var      result_matrlu2 = matrlu.GetSolution2();
                result = "A:\n";
                for (int ii = 0; ii < rangeArray; ii++)
                {
                    for (int j = 0; j < rangeArray; j++)
                    {
                        result = result + "  \t " + string.Format("{0:f" + precision + "}", result_matrlu[ii, j]);
                    }
                    result = result + " \n";
                }
                result = result + "\nL:\n";
                for (int ii = 0; ii < rangeArray; ii++)
                {
                    result = result + "      " + string.Format("{0:f" + precision + "}", result_matrlu2[ii]);
                }
                result = result + " \n ";
                break;

            case "Matrix Inverse LU":
                RMatrixLuInverse matrluinv = new RMatrixLuInverse();

                MatrixLU matrlu2 = new MatrixLU(MatrixAlgebraA, rangeArray, rangeArray);
                if (matrluinv.rmatrixluinverse(MatrixAlgebraA, rangeArray, matrlu2.GetSolution2()) == true)
                {
                    result = "\n             An inverse matrix exists \n\n ";
                    var result_matrluinv = matrluinv.GetSolution();
                    for (int ii = 0; ii < rangeArray; ii++)
                    {
                        for (int j = 0; j < rangeArray; j++)
                        {
                            result = result + "    \t" + string.Format("{0:f" + precision + "}", result_matrluinv[ii, j]);
                        }
                        result = result + "\n\n";
                    }
                }
                else
                {
                    result = "\n             An inverse matrix does not exist";
                }

                break;

            //*** Optimizing***
            case "Brentopt":
                Brentopt brent = new Brentopt(new FunctionOne(TestFunOptimizing), param1, param2, param3);
                result = "\n    Point of the found minimum :";
                result = result + "\n\n    XMin = " + string.Format("{0:f" + precision + "}", brent.GetSolution());
                result = result + "\n\n     A value of function is in the found minimum :";
                result = result + "\n\n    F(XMin) = " + string.Format("{0:f" + precision + "}", brent.GetSolutionFunction());
                break;

            case "Golden Section":
                GoldenSection godsection = new GoldenSection(new FunctionOne(TestFunOptimizing), param1, param2, Convert.ToInt32(param3));
                result = "\n    Scopes   of segment  which a decision of task is on .";
                result = result + "\n\n    a = " + string.Format("{0:f" + precision + "}", godsection.GetSolutionA());
                result = result + "\n\n    b = " + string.Format("{0:f" + precision + "}", godsection.GetSolutionB());
                break;

            case "Pijavsky":
                Pijavsky pijavsky = new Pijavsky(new FunctionOne(TestFunOptimizing), param1, param2, param3, Convert.ToInt32(param4));
                result = "\n    Abscissa of the best point from found..";
                result = result + "\n\n    F = " + string.Format("{0:f" + precision + "}", pijavsky.GetSolution());
                break;

            //*** Statistics ***
            case "Correlation Pearson":


                CorrelationPearson corelperson = new CorrelationPearson(massX, massF, 6);
                result = "\n    Pearson product-moment correlation coefficient.";
                result = result + "\n\n    K = " + string.Format("{0:f" + precision + "}", corelperson.GetSolution());
                break;

            case "Correlation Spearmans Rank":
                CorrelationSpearmansRank corelspear = new CorrelationSpearmansRank(massX, massF, 6);
                result = "\n    Pearson product-moment correlation coefficient.";
                result = result + "\n\n    K = " + string.Format("{0:f" + precision + "}", corelspear.GetSolution());
                break;

            case "Descriptive Statistics Median":
                DescriptiveStatisticsADevMedian desceripM = new DescriptiveStatisticsADevMedian(massX, 6);
                result = "\n    Output parameters:";
                result = result + "\n\n    M = " + string.Format("{0:f" + precision + "}", desceripM.GetSolution());
                break;

            case "Descriptive Statistics Moments":
                DescriptiveStatisticsMoments desceripMo = new DescriptiveStatisticsMoments(massX, 6);
                result = "\n    Output parameters:";
                result = result + "\n\n    M = " + string.Format("{0:f" + precision + "}", desceripMo.GetSolution());
                result = result + "\n\n    Variance = " + string.Format("{0:f" + precision + "}", desceripMo.variance);
                result = result + "\n\n    Skewness = " + string.Format("{0:f" + precision + "}", desceripMo.skewness) + " (if variance<>0; zero otherwise)";
                result = result + "\n\n    Kurtosis = " + string.Format("{0:f" + precision + "}", desceripMo.kurtosis) + " (if variance<>0; zero otherwise)";
                break;

            case "Descriptive Statistics Percentile":
                DescriptiveStatisticsPercentile desceripP = new DescriptiveStatisticsPercentile(massX, 6, pointPercentile);
                result = "\n    Output parameters:";
                result = result + "\n\n    V = " + string.Format("{0:f" + precision + "}", desceripP.GetSolution());
                break;

            case "Generator 1":
                RandomGeneratorsMethod1 random1 = new RandomGeneratorsMethod1();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random1.GetSolution());
                break;

            case "Generator 2":
                RandomGeneratorsMethod2 random2 = new RandomGeneratorsMethod2(Convert.ToInt32(pointGenerator));
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random2.GetSolution());
                break;

            case "Generator 3":
                RandomGeneratorsMethod3 random3 = new RandomGeneratorsMethod3();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random3.GetSolution());
                break;

            case "Generator 4":
                RandomGeneratorsMethod4 random4 = new RandomGeneratorsMethod4();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random4.GetSolution());
                break;

            case "Generator 5":
                RandomGeneratorsMethod5 random5 = new RandomGeneratorsMethod5(pointGenerator);
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random5.GetSolution());
                break;
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Increasing by unit - trapezium
        /// </summary>
        /// <param name="bwImg"></param>
        /// <returns></returns>
        public static Image <Gray, byte> fitTrapeziumToMask1(Image <Gray, byte> bwImg)
        {
            Point     c = new Point((int)(bwImg.Width / 2.0), (int)(bwImg.Height - 1));
            Trapezium t = new Trapezium(c);

            var black = bwImg.CopyBlank();

            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            var aux = black.And(bwImg);

            while (CvInvoke.CountNonZero(aux) == 0)
            {
                t.BaseWidth++;
                t.Height++;
                t.LeftTriangle++;
                t.RightTriangle++;
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
                CvInvoke.BitwiseAnd(black, aux, aux);
            }
            t.BaseWidth--;
            t.Height--;
            t.LeftTriangle--;
            t.RightTriangle--;
            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            while (CvInvoke.CountNonZero(black.And(bwImg)) == 0 &&
                   (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0) &&
                   (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width))
            {
                t.BaseWidth++;
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            }
            t.BaseWidth--;
            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            while (CvInvoke.CountNonZero(black.And(bwImg)) == 0 && (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0))
            {
                t.LeftTriangle++;
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            }
            t.LeftTriangle--;
            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            while (CvInvoke.CountNonZero(black.And(bwImg)) == 0 && (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width))
            {
                t.RightTriangle++;
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            }
            t.RightTriangle--;
            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            while (CvInvoke.CountNonZero(black.And(bwImg)) == 0 && t.Height < black.Height)
            {
                t.Height++;
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            }
            t.Height--;
            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            return(black);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Increasing by dividing - trapezium
        /// </summary>
        /// <param name="bwImg"></param>
        /// <returns></returns>
        public static Image <Gray, byte> fitTrapeziumToMask(Image <Gray, byte> bwImg)
        {
            Point     c       = new Point((int)(bwImg.Width / 2.0), (int)(bwImg.Height - 1));
            Trapezium t       = new Trapezium(c);
            int       inistep = 16;
            int       step    = inistep;
            var       black   = bwImg.CopyBlank();

            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            var aux = black.And(bwImg);

            if (CvInvoke.CountNonZero(aux) != 0)
            {
                bwImg._Not();
                black.SetZero();
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
                aux = black.And(bwImg);
            }

            while (CvInvoke.CountNonZero(aux) == 0 &&
                   (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0) &&
                   (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width))
            {
                t.BaseWidth     += step;
                t.Height        += step;
                t.LeftTriangle  += step;
                t.RightTriangle += step;
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
                CvInvoke.BitwiseAnd(black, bwImg, aux);
            }
            t.BaseWidth     -= step;
            t.Height        -= step;
            t.LeftTriangle  -= step;
            t.RightTriangle -= step;

            t.BaseWidth     = Math.Max(t.BaseWidth, 1);
            t.Height        = Math.Max(t.BaseWidth, 1);
            t.LeftTriangle  = Math.Max(t.BaseWidth, 1);
            t.RightTriangle = Math.Max(t.BaseWidth, 1);

            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            while (step > 0)
            {
                if (CvInvoke.CountNonZero(aux) == 0 &&
                    (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0) &&
                    (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width))
                {
                    t.BaseWidth += step;
                }
                else
                {
                    t.BaseWidth -= step;
                    step        /= 2;
                }
                black.SetZero();
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
                CvInvoke.BitwiseAnd(black, bwImg, aux);
            }
            if (t.BaseWidth < 0)
            {
                black.SetZero();
                return(black);
            }
            while (CvInvoke.CountNonZero(aux) > 0)
            {
                t.BaseWidth--;
                black.SetZero();
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
                CvInvoke.BitwiseAnd(black, bwImg, aux);
            }
            step = inistep;

            while (step > 0)
            {
                if (CvInvoke.CountNonZero(aux) == 0 && (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0))
                {
                    t.LeftTriangle += step;
                }
                else
                {
                    t.LeftTriangle -= step;
                    step           /= 2;
                }
                black.SetZero();
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
                CvInvoke.BitwiseAnd(black, bwImg, aux);
            }
            t.LeftTriangle--;
            step = inistep;
            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            while (step > 0)
            {
                if (CvInvoke.CountNonZero(aux) == 0 && (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width))
                {
                    t.RightTriangle += step;
                }
                else
                {
                    t.RightTriangle -= step;
                    step            /= 2;
                }
                black.SetZero();
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
                CvInvoke.BitwiseAnd(black, bwImg, aux);
            }
            t.RightTriangle--;
            step = inistep;
            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            while (step > 0)
            {
                if (CvInvoke.CountNonZero(aux) == 0 && t.Height < black.Height)
                {
                    t.Height += step;
                }
                else
                {
                    t.Height -= step;
                    step     /= 2;
                }
                black.SetZero();
                black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
                CvInvoke.BitwiseAnd(black, bwImg, aux);
            }
            t.Height--;
            black.SetZero();
            black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255));
            return(black);
        }