private void fungraph_GetFunctionValueRange(Expression fx, double x0, double rx, int i0, int i1, out double y0, out double y1) { // 函数值的取值范围 y0 = double.MaxValue; y1 = double.MinValue; for (int i = i0; i <= i1; i++) { double x = x0 + (i - i0) * rx; double y = fx.Compute(x); if (double.IsInfinity(y) || double.IsNaN(y)) { continue; } if (y0 > y) { y0 = y; } if (y1 < y) { y1 = y; } } }
private bool draw_fungraph(string fuuuus) { //绘制函数 if (fuuuus.StartsWith("f(x)=")) { fuuuus = fuuuus.Remove(0, 5); } if (fungraph_needrsdaw || lastEnterGy != fuuuus) { string fun = fuuuus; if (lastEnterGy != fuuuus || lastEnterGy == "") { lastEnterGy = fuuuus; fungraph_dawing = true; if (fun.Contains("sin(") && !fun.Contains("asin(")) { fun = fun.Replace("sin(", "Math.Sin("); } if (fun.Contains("cos(") && !fun.Contains("acos(")) { fun = fun.Replace("cos(", "Math.Cos("); } if (fun.Contains("tan(") && !fun.Contains("atan(")) { fun = fun.Replace("tan(", "Math.Tan("); } if (fun.Contains("asin(")) { fun = fun.Replace("asin(", "Math.Asin("); } if (fun.Contains("acos(")) { fun = fun.Replace("acos(", "Math.Acos("); } if (fun.Contains("atan(")) { fun = fun.Replace("atan(", "Math.Atan("); } if (fun.Contains("pow(")) { fun = fun.Replace("pow(", "Math.Pow("); } if (fun.Contains("lg(")) { fun = fun.Replace("lg(", "Math.Log10("); } if (fun.Contains("log(")) { fun = fun.Replace("log(", "Math.Log("); } if (fun.Contains("ln(")) { fun = fun.Replace("ln(", "Math.Log("); } if (fun.Contains("sqrt(")) { fun = fun.Replace("sqrt(", "Math.Sqrt("); } if (fun.Contains("ln(")) { fun = fun.Replace("ln(", "Math.Log("); } if (fun.Contains("π")) { fun = fun.Replace("π", "Math.PI"); } if (fun.Contains("pi")) { fun = fun.Replace("pi", "Math.PI"); } if (fun.Contains("e")) { fun = fun.Replace("e", "Math.E"); } lock (fungraph) { Graphics gc = Graphics.FromImage(fungraph); gc.Clear(Color.White); try { if (fun == "") { throw new Exception("请输入需要绘制的函数!"); } else if (!fun.Contains("x")) { throw new Exception("未找到自变量x"); } double x0 = new Expression(fungraph_str_to_xy(tbxX0.Text)).Compute(0); double x1 = new Expression(fungraph_str_to_xy(tbxX1.Text)).Compute(0); if (x1 <= x0) { throw new Exception("区间设置有误。请保证区间 结束>开始 。"); } Size size = panel_fungraph.Size; int i0 = 0; int i1 = size.Width - 1; int j0 = fungraph_title_height;// 屏幕保留区域的高度 int j1 = size.Height - 1; Pen pen = new Pen(Color.Black, 1); //gc.DrawLine(pen, i0, j0, i1, j0); // 画图区和保留区的分界线 double rx = (x1 - x0) / (i1 - i0); double y0, y1; Expression fx = new Expression(fun); fungraph_GetFunctionValueRange(fx, x0, rx, i0, i1, out y0, out y1); double ry = (y1 - y0) / (j1 - j0); if (check_fungr_data.Checked) { fungraph_Out(gc, 1, "f(x): " + fun); fungraph_Out(gc, 3, "x:[{0}, {1}] ({2})", fungraph_xy_to_str(x0), fungraph_xy_to_str(x1), fungraph_xy_to_str(x1 - x0)); fungraph_Out(gc, 4, "y:[{0}, {1}] ({2})", fungraph_xy_to_str(y0), fungraph_xy_to_str(y1), fungraph_xy_to_str(y1 - y0)); fungraph_Out(gc, 6, "rx:{0}", (1 / rx).ToString("0.0000")); // 函数自变量每单位值用多少个象素表示 fungraph_Out(gc, 7, "ry:{0}", (1 / ry).ToString("0.0000")); // 函数的值每单位值用多少个象素表示 fungraph_Out(gc, 8, "r :{0}", (rx / ry).ToString("0.0000")); // 该值如果小于1表示图形纵向被压扁,反之则被拉伸 } pen.Color = Color.Green; int j = j1 + (int)(y0 / ry); if (j >= j0 && j <= j1) { if (check_fg_drawxy.Checked) { gc.DrawLine(pen, i0, j, i1, j); // x坐标轴 gc.DrawString(fungraph_xy_to_str(x0), new Font("Courier New", 10), Brushes.Black, 0, j - 15); gc.DrawString("0", new Font("Courier New", 10), Brushes.Blue, size.Width / 2, j - 15); gc.DrawString(fungraph_xy_to_str(x1), new Font("Courier New", 10), Brushes.Black, new Rectangle(size.Width - 30, j - 15, 30, 15), new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Far }); } else { pen.Color = Color.Silver; gc.DrawLine(pen, i0, j, i1, j); // x坐标轴 } } int i = i0 - (int)(x0 / rx); if (i >= i0 && i <= i1) { if (check_fg_drawxy.Checked) { gc.DrawLine(pen, i, j0, i, j1); // y坐标轴 gc.DrawString(fungraph_xy_to_str(y1), new Font("Courier New", 10), Brushes.Black, i + 1, fungraph_title_height); gc.DrawString(fungraph_xy_to_str(y0), new Font("Courier New", 10), Brushes.Black, i + 1, j1 - 15); } else { pen.Color = Color.Silver; gc.DrawLine(pen, i, j0, i, j1); // y坐标轴 } } pen.Color = Color.Red; for (i = i0; i <= i1; i++) { double x = x0 + (i - i0) * rx; double y = fx.Compute(x); if (double.IsInfinity(y) || double.IsNaN(y)) { continue; } j = j1 - (int)((y - y0) / ry); if (j > j1 || j < j0) { continue; } gc.DrawLine(pen, i, j, i + 1, j); // 画函数的图形 } } catch (Exception ex) { StringFormat f = new StringFormat(); f.LineAlignment = StringAlignment.Center; f.Alignment = StringAlignment.Center; gc.DrawString(ex.Message, new Font("Courier New", 10), Brushes.Blue, new Rectangle(5, fungraph_title_height, panel_fungraph.Width, panel_fungraph.Height), f); fungraph_needrsdaw = false; button31.Text = "绘制函数图像"; fungraph_dawing = false; panel_fungraph.Invalidate(); return(false); } } } fungraph_needrsdaw = false; } button31.Text = "绘制函数图像"; fungraph_dawing = false; panel_fungraph.Invalidate(); return(true); }