Example #1
0
        public void GenerateMatrix(int rows, int cols, NCalc.Expression generator)
        {
            Matrix = new ObservableCollection <ObservableCollection <FilterItem> >();

            if (rows > 0 && cols > 0)
            {
                try
                {
                    for (int i = 0; i < rows; i++)
                    {
                        Matrix.Add(new ObservableCollection <FilterItem>());
                        for (int j = 0; j < cols; j++)
                        {
                            generator.Parameters["y"] = (float)i;
                            generator.Parameters["x"] = (float)j;
                            Matrix[i].Add(new FilterItem((float)generator.ComputeDouble()));
                        }
                    }

                    Matrix[rows / 2][cols / 2].IsAnchor = true;
                    OnPropertyChanged(nameof(Matrix));
                    OnPropertyChanged(nameof(Anchor));
                }
                catch (Exception)
                {
                    GenerateMatrix(rows, cols, new NCalc.Expression("0"));
                }
            }
        }
        private void ApplyFunction(object parameter)
        {
            var func = new NCalc.Expression(Function)
            {
                Parameters =
                {
                    ["anchX"] = (float)AnchorX,
                    ["anchY"] = (float)AnchorY,
                    ["rows"]  = (float)Rows,
                    ["cols"]  = (float)Cols
                }
            };

            try
            {
                for (int x = 0; x < CurrentFilter.Matrix.Count; x++)
                {
                    for (int y = 0; y < CurrentFilter.Matrix.FirstOrDefault()?.Count; y++)
                    {
                        func.Parameters["y"] = (float)x;
                        func.Parameters["x"] = (float)y;
                        float digit = (float)func.ComputeDouble();
                        CurrentFilter.Matrix[x][y].Coeficient = digit;
                    }
                }

                OnPropertyChanged(nameof(CurrentFilter));
            }
            catch (Exception exception)
            {
                MessageBox.Show($"{exception.Message}", "Function Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }