画函数图形的C#程序
来源:互联网 发布:电脑机器人编程软件 编辑:程序博客网 时间:2024/05/22 02:56
可以看到,不但要画的函数的表达式可以由用户动态地输入,而且函数自变量的范围也可以是常量表达式。 下面就是源程序:
// plot.cs: 画函数图形, 编译方法: csc /t:winexe plot.cs Expression.cs
using System;
using System.Drawing;
using System.Windows.Forms;
using Skyiv.Util;
namespace Skyiv.Ben.Plot
{
sealed class PlotForm : Form
{
const int yBase = 24; // 屏幕保留区域的高度
TextBox tbxX0, tbxX1; // 函数自变量的取值范围
TextBox tbxExpression; // 函数的表达式
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);
tbxX0 = new TextBox();
tbxX0.Text = "-Math.PI";
tbxX0.Location = new Point(55, 3);
tbxX0.Size = new Size(100, 20);
tbxX1 = new TextBox();
tbxX1.Text = "Math.PI";
tbxX1.Location = new Point(160, 3);
tbxX1.Size = new Size(100, 20);
tbxExpression = new TextBox();
tbxExpression.Text = "Math.Sin(x)";
tbxExpression.Location = new Point(265, 3);
tbxExpression.Size = new Size(335, 20);
tbxExpression.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
Controls.AddRange(new Control[]{btnSubmit, tbxX0, tbxX1, tbxExpression});
Text = "Plot";
BackColor = Color.White;
ClientSize = new Size(600, 600 + yBase);
// WindowState = FormWindowState.Maximized;
ResumeLayout(false);
}
// 点击“刷新”按钮时重绘程序主窗口
void BtnSubmit_Click(object sender, EventArgs e)
{
Invalidate();
}
/**//*
// 因为本程序使用 C# 的反射功能动态生成数学表达式并计算其值
// 所以重画时有点慢,如果你的计算机的速度不是非常快的,
// 就不要在窗口改变大小时强制重绘,而是通过点击发“刷新”按钮重绘。
protected override void OnSizeChanged(EventArgs e)
{
Invalidate();
base.OnSizeChanged(e);
}
*/
protected override void OnPaint(PaintEventArgs e)
{
Graphics gc = e.Graphics;
try
{
double x0 = new Expression(tbxX0.Text).Compute(0);
double x1 = new Expression(tbxX1.Text).Compute(0);
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, y1;
Expression fx = new Expression(tbxExpression.Text);
GetFunctionValueRange(fx, 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, "f(x): " + tbxExpression.Text);
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 = 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)
{
Out(gc, 0, ex.Message);
}
base.OnPaint(e);
}
// 函数值的取值范围
void 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;
}
}
// 在指定的位置写字符串
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));
}
static void Main()
{
Application.Run(new PlotForm());
}
}
}
其中的“Expression.cs”程序请参看我的另一篇随笔:“动态地生成用户输入的函数表达式(C#)”。using System;
using System.Drawing;
using System.Windows.Forms;
using Skyiv.Util;
namespace Skyiv.Ben.Plot
{
sealed class PlotForm : Form
{
const int yBase = 24; // 屏幕保留区域的高度
TextBox tbxX0, tbxX1; // 函数自变量的取值范围
TextBox tbxExpression; // 函数的表达式
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);
tbxX0 = new TextBox();
tbxX0.Text = "-Math.PI";
tbxX0.Location = new Point(55, 3);
tbxX0.Size = new Size(100, 20);
tbxX1 = new TextBox();
tbxX1.Text = "Math.PI";
tbxX1.Location = new Point(160, 3);
tbxX1.Size = new Size(100, 20);
tbxExpression = new TextBox();
tbxExpression.Text = "Math.Sin(x)";
tbxExpression.Location = new Point(265, 3);
tbxExpression.Size = new Size(335, 20);
tbxExpression.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
Controls.AddRange(new Control[]{btnSubmit, tbxX0, tbxX1, tbxExpression});
Text = "Plot";
BackColor = Color.White;
ClientSize = new Size(600, 600 + yBase);
// WindowState = FormWindowState.Maximized;
ResumeLayout(false);
}
// 点击“刷新”按钮时重绘程序主窗口
void BtnSubmit_Click(object sender, EventArgs e)
{
Invalidate();
}
/**//*
// 因为本程序使用 C# 的反射功能动态生成数学表达式并计算其值
// 所以重画时有点慢,如果你的计算机的速度不是非常快的,
// 就不要在窗口改变大小时强制重绘,而是通过点击发“刷新”按钮重绘。
protected override void OnSizeChanged(EventArgs e)
{
Invalidate();
base.OnSizeChanged(e);
}
*/
protected override void OnPaint(PaintEventArgs e)
{
Graphics gc = e.Graphics;
try
{
double x0 = new Expression(tbxX0.Text).Compute(0);
double x1 = new Expression(tbxX1.Text).Compute(0);
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, y1;
Expression fx = new Expression(tbxExpression.Text);
GetFunctionValueRange(fx, 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, "f(x): " + tbxExpression.Text);
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 = 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)
{
Out(gc, 0, ex.Message);
}
base.OnPaint(e);
}
// 函数值的取值范围
void 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;
}
}
// 在指定的位置写字符串
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));
}
static void Main()
{
Application.Run(new PlotForm());
}
}
}
这里的表达式是使用C#语法。如需要使用 VisualBasic 语法,请参阅该随笔的评论。
- 画函数图形的C#程序
- 画函数图形的C#程序,兼论一个病态函数
- 画WAV的图形函数
- c#的图形描画
- C#的chart图形
- 用程序画一些空心的图形
- C#画散射图形
- Android中画各种图形的函数
- 好玩的图形函数
- GDI图形图形的一些函数
- C#的图形绘制基础知识
- C#打印简单的图形
- 图形程序运行的条件
- 高效的WP7程序--图形
- C# GDI+ 画简单的图形:直线、矩形、扇形等
- Matlab函数产生的Figure图形框如何嵌入C# Winform中的一个Panel中?
- C# ZedGraph画曲线图形
- 两个简单的画验证码图形程序
- UML中的用例图
- 决定走出第一步
- SVN 服务器搭建
- 安装visualSVN server出现Service 'VisualSVN Server' failed to start
- 杂记之UNIX之ID概念
- 画函数图形的C#程序
- (转)JAVA存储过程
- PKU 1083 Moving Tables
- ios开发学习-网络(Networking) 效果源码分享--系列教程
- setsockopt设置socket状态
- struts2中使用serclet的session,application等内置对象
- zoj 1002 Fire Net
- 自旋锁(spinlock) 解释得经典,透彻
- <context:annotation-config />