Beispiel #1
0
        private void MenuShowChart_Clicked(object sender, RoutedEventArgs e)
        {
            try
            {
                Func <double, double> fx = this.Fx.CompileDynamicMethod();
                Func <double, double> gx = this.Gx.CompileDynamicMethod();
                if (this.chartData.YTo == null || this.chartData.YFrom == null) //chart data y-values were not initialized
                {
                    Parallel.Invoke(() =>
                    {
                        Extremum extremum    = DiagramWindow.FuncFindMinMax(new Func <double, double>[] { fx, gx }, this.chartData.XFrom, this.chartData.XTo);
                        this.chartData.YFrom = -Math.Abs(1.5 * Math.Max(Math.Abs(extremum.Min), Math.Abs(extremum.Max)));
                        this.chartData.YTo   = -chartData.YFrom;
                    });
                }

                DiagramWindow diagWnd = new DiagramWindow(chartData.XFrom, chartData.XTo, chartData.YFrom, chartData.YTo, chartData.Dx);

                diagWnd.DrawGraph(new Func <double, double>[] { fx, gx }, this.chartData.XFrom, this.chartData.XTo);


                diagWnd.Owner = this;
                diagWnd.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
                diagWnd.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Beispiel #2
0
        private void MenuChartConfig_Clicked(object sender, RoutedEventArgs e)
        {
            double xFrom             = this.chartData.XFrom;
            double xTo               = this.chartData.XTo;
            Func <double, double> fx = this.Fx.CompileDynamicMethod();
            Func <double, double> gx = this.Gx.CompileDynamicMethod();
            Extremum extremum        = DiagramWindow.FuncFindMinMax(new Func <double, double>[] { fx, gx }, xFrom, xTo);
            double   yFrom           = this.chartData.YFrom ?? -Math.Abs(1.5 * Math.Max(Math.Abs(extremum.Min), Math.Abs(extremum.Max)));
            double   yTo             = -yFrom;

            ChartConfigWindow configWnd = new ChartConfigWindow(xFrom, xTo, yFrom, yTo, this.chartData.Dx, this.BindYToFx, this.BindYToGx);

            configWnd.ChartConfigChanged += delegate(object o, ChartEventArgs eArgs)
            {
                this.chartData.XFrom = eArgs.From.X;
                this.chartData.XTo   = eArgs.To.X;
                this.chartData.YFrom = eArgs.From.Y;
                this.chartData.YTo   = eArgs.To.Y;
                this.chartData.Dx    = eArgs.Dx;
                this.BindYToFx       = eArgs.BindToFx;
                this.BindYToGx       = eArgs.BindToGx;

                if (this.BindYToFx && this.BindYToGx)
                {
                    extremum = DiagramWindow.FuncFindMinMax(new Func <double, double>[] { fx, gx }, xFrom, xTo);
                }
                else if (this.BindYToFx)
                {
                    extremum = DiagramWindow.FuncFindMinMax(new Func <double, double>[] { fx }, xFrom, xTo);
                }
                else if (this.BindYToGx)
                {
                    extremum = DiagramWindow.FuncFindMinMax(new Func <double, double>[] { gx }, xFrom, xTo);
                }

                this.chartData.YFrom = -Math.Abs(1.5 * Math.Max(Math.Abs(extremum.Min), Math.Abs(extremum.Max)));
                this.chartData.YTo   = -this.chartData.YFrom;
            };



            configWnd.Owner = this;
            configWnd.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            configWnd.Show();
        }
Beispiel #3
0
        private void MenuReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Func <double, double> fx = this.Fx.CompileDynamicMethod();
                Func <double, double> gx = this.Gx.CompileDynamicMethod();
                if (this.chartData.YTo == null || this.chartData.YFrom == null) //chart data y-values were not initialized
                {
                    Parallel.Invoke(() =>
                    {
                        Extremum extremum    = DiagramWindow.FuncFindMinMax(new Func <double, double>[] { fx, gx }, this.chartData.XFrom, this.chartData.XTo);
                        this.chartData.YFrom = -Math.Abs(1.5 * Math.Max(Math.Abs(extremum.Min), Math.Abs(extremum.Max)));
                        this.chartData.YTo   = -chartData.YFrom;
                    });
                }

                DiagramWindow diagWnd = new DiagramWindow(chartData.XFrom, chartData.XTo, chartData.YFrom, chartData.YTo, chartData.Dx);
                diagWnd.Owner = this;

                RenderTargetBitmap rtb = diagWnd.DrawGraphToImage(new Func <double, double>[] { fx, gx }, width: 800, height: 800);
                diagWnd.Close();

                PngBitmapEncoder png = new PngBitmapEncoder();
                png.Frames.Add(BitmapFrame.Create(rtb));
                using (Stream stm = File.Create("diagram.png"))
                {
                    png.Save(stm);
                }

                //generate pdf--------------------------------------------------------
                PdfDocument doc = new PdfDocument();
                doc.Info.Title = "RootFinder Report";

                PdfPage page1 = doc.AddPage();

                // Get an XGraphics object for drawing
                XGraphics gfx = XGraphics.FromPdfPage(page1);

                // Create a font
                XFont font = new XFont("Times New Roman", 14, XFontStyle.Regular);

                //generate a report---------------------------------------------------------------------------------------------------------------
                StringBuilder report = new StringBuilder();
                string        task   = "Finding roots of equations using dynamic expression tree generation and function interpolation\r\n" +
                                       "\tUsed algorithms: Shunting-yard algorithm and Lagrange interpolation.\r\n\r\n";
                report.Append(task);
                report.AppendFormat("Search interval: {0}\r\n", lblSrcInterval.Content);
                report.AppendFormat("Eps.: {0}\r\n", lblEps.Content);
                report.AppendFormat("Interpolation error: {0}\r\n", lblInterpError.Content);
                report.AppendFormat("Number of roots: {0}\r\n", lblRoots.Content);
                report.AppendFormat("Power of Lagrange polynomial: {0}\r\n", lblLagPower.Content);
                report.AppendFormat("Roots are shown on the page #2\r\n", lblInterpError.Content);

                //generate a report---------------------------------------------------------------------------------------------------------------


                XTextFormatter tf = new XTextFormatter(gfx);

                XRect rect = new XRect(10, 10, page1.Width - 10, 150);
                gfx.DrawRectangle(XBrushes.SeaShell, rect);
                //tf.Alignment = ParagraphAlignment.Left;
                tf.DrawString(report.ToString(), font, XBrushes.Black, rect, XStringFormats.TopLeft);

                XImage img = XImage.FromFile("diagram.png");

                gfx.DrawImage(img, (page1.Width - img.PixelWidth / 1.5) / 2.0, 250, img.PixelWidth / 1.5, img.PixelHeight / 1.5);

                //page#2 - roots
                const int stringsPerPage = 50;
                int       k = 0;
                for (; k < this.roots.Count; k += stringsPerPage)
                {
                    StringBuilder rootsStr = new StringBuilder();
                    if (k == 0)
                    {
                        rootsStr.AppendLine("Roots found:\r\n");
                    }

                    for (int i = 0; i < stringsPerPage && i + k < this.roots.Count; i++)
                    {
                        rootsStr.AppendLine(string.Format("{0}", this.roots[i + k]));
                    }
                    PdfPage page2 = doc.AddPage();
                    gfx = XGraphics.FromPdfPage(page2);
                    gfx.DrawRectangle(XBrushes.White, rect);
                    tf   = new XTextFormatter(gfx);
                    rect = new XRect(10, 10, page2.Width - 10, page2.Height - 10);
                    tf.DrawString(rootsStr.ToString(), font, XBrushes.Black, rect, XStringFormats.TopLeft);
                }

                using (FileStream fStream = new FileStream("report.pdf", FileMode.Create))
                {
                    doc.Save(fStream);
                }
                Process.Start("report.pdf");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }