关于使用C#画函数图形的问题

引用http://www.cnblogs.com/zhangjun1130/archive/2010/01/04/1638874.html

画函数图形的C#程序,兼论一个病态函数(ZZ)

文中提到不能使用自定义公式的问题,我用引用Com控件的方法,但是速度比较慢

需要添加Microsoft.ScriptControl控件,公式只能使用JavaScript格式来写


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

namespace PlotFunctionForm
{
static class Program
{


sealed class PlotForm : Form
{

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new PlotForm());
}

// 要画的函数,如果能在 TextBox 中输入函数的表达式就更好了
double Function(double x)
{



//使用Microsoft ScriptControl Com控件
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
sc.Language = "JavaScript";
string express = TextBox1.Text;
express = express.Replace("x", x.ToString());
try
{
  double y = double.Parse(sc.Eval(express).ToString());
   return y;
}
catch(Exception ex)
{ MessageBox.Show(ex.Message); return -1; }



//double z = Math.Sin(x);
//return z;






// return Math.Sin(x);
//double u = Math.PI - x;
//double pi2 = Math.PI * Math.PI;
//return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;
}

// 仅仅用来显示在屏幕上
string FunctionString()
{
return TextBox1.Text;

//return "f(x) = 3 * x^2 + pi^-4 * ln[(pi-x)^2] + 1";
}

const int yBase = 24; // 屏幕保留区域的高度

TextBox tbxX0, tbxX1; // 函数自变量的取值范围

TextBox TextBox1;

Label label1=new Label();
Label label2=new Label();

PlotForm()
{
SuspendLayout();

Button btnSubmit = new Button();
btnSubmit.Text = "刷新";
btnSubmit.Location = new Point(0, 0);
btnSubmit.Size = new Size(48, 24);
btnSubmit.Click += new EventHandler(BtnSubmit_Click);


label1.AutoSize = true;
label1.Location = new System.Drawing.Point(55, 7);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(40, 12);
label1.TabIndex = 0;
label1.Text = "取值范围";


tbxX0 = new TextBox();
tbxX0.Text = "-3";
tbxX0.Location = new Point(110, 3);
tbxX0.Size = new Size(50, 20);

tbxX1 = new TextBox();
tbxX1.Text = "3";
tbxX1.Location = new Point(165, 3);
tbxX1.Size = new Size(50, 20);

label2.Text = "公式";
label2.Location = new Point(220, 7);
label2.Size = new Size(30, 20);

TextBox1 = new TextBox();
TextBox1.Text = "Math.sin(x)";
TextBox1.Location = new Point(255, 3);
TextBox1.Size = new Size(200, 20);


Controls.AddRange(new Control[] { btnSubmit, tbxX0, tbxX1, TextBox1,label1,label2 });
Text = "Plot";
BackColor = Color.White;
ClientSize = new Size(600, 600 + yBase);
// WindowState = FormWindowState.Maximized;

ResumeLayout(false);
}

void BtnSubmit_Click(object sender, EventArgs e)
{
Invalidate();
}

protected override void OnSizeChanged(EventArgs e)
{
Invalidate();
base.OnSizeChanged(e);
}

protected override void OnPaint(PaintEventArgs e)
{
double x0 = double.Parse(tbxX0.Text);
double x1 = double.Parse(tbxX1.Text);
Graphics gc = e.Graphics;
Size size = ClientSize;
int i0 = 0;
int i1 = size.Width - 1;
int j0 = yBase;
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=0, y1=1;
GetFunctionValueRange(x0, rx, i0, i1, out y0, out y1);
double ry = (y1 - y0) / (j1 - j0);
Out(gc, 0, "ClientSize: {0}x{1}", i1 - i0 + 1, j1 - j0 + 1);
Out(gc, 1, FunctionString());
Out(gc, 2, "x:[{0}, {1}] range:{2}", x0, x1, x1 - x0);
Out(gc, 3, "y:[{0}, {1}] range:{2}", y0, y1, y1 - y0);
Out(gc, 4, "rx:{0}", 1 / rx); // 函数自变量每单位值用多少个象素表示
Out(gc, 5, "ry:{0}", 1 / ry); // 函数的值每单位值用多少个象素表示
Out(gc, 6, "r :{0}", rx / ry); // 该值如果小于1表示图形纵向被压扁,反之则被拉伸
pen.Color = Color.Green;
int j = j1 + (int)(y0 / ry);
if (j >= j0 && j <= j1) gc.DrawLine(pen, i0, j, i1, j); // x坐标轴
int i = i0 - (int)(x0 / rx);
if (i >= i0 && i <= i1) 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 = Function(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); // 画函数的图形
}
base.OnPaint(e);
}

// 函数值的取值范围
void GetFunctionValueRange(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 = Function(x);
if (double.IsInfinity(y) || double.IsNaN(y)) continue;
if (y0 > y) y0 = y;
if (y1 < y) y1 = y;
}
}

// 在指定的位置写字符串
void Out(Graphics gc, int line, string fmt, params object[] args)
{
gc.DrawString(string.Format(fmt, args), new Font("Courier New", 10), Brushes.Blue, new PointF(5, yBase + 15 * line));
}


}
}
}

分享到: 更多
原文地址:https://www.cnblogs.com/tiasys/p/1693638.html