引用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));
}
}
}
}