在.NET环境下绘制模糊数学中隶属函数分布图的整个源代码如下:其中:unit代表图形放大倍数,数值越大图形放大倍数越大。interval代表步进刻度,值越小越精确(必须大小0),但速度也越慢。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Collections; using System.Windows.Forms; using System.Drawing.Imaging; using System.Drawing.Drawing2D; namespace ImageFuzzy ...{ public partial class Form1 : Form ...{ private int type1; private int type2; private string item1; private string item2; private float a; private float b; private float c; private float d1; private float k; private float l; private float tempx; private float tempy; public void InitArray() ...{ type1 = 0; type2 = 0; k = 2; item1 = this.comboBox1.Text.ToString(); item2 = this.comboBox2.Text.ToString(); this.groupBox1.Text = item1 + item2 + "图形"; a = float.Parse(this.tbA.Text.Trim().ToString()); b = float.Parse(this.tbB.Text.Trim().ToString()); c = float.Parse(this.tbC.Text.Trim().ToString()); d1 = float.Parse(this.tbD.Text.Trim().ToString()); k = float.Parse(this.tbK.Text.Trim().ToString()); l = float.Parse(this.tbL.Text.Trim().ToString()); type1 = this.comboBox1.SelectedIndex+1; type2 = this.comboBox2.SelectedIndex+1; } private void delete() ...{ a = 0; b = 0; c = 0; d1 = 0; k = 0; l = 0; } private void set1() ...{ this.tbA.Text = "1"; this.tbB.Text = "2"; this.tbC.Text = "3"; this.tbD.Text = "4"; this.tbK.Text = "1"; this.tbL.Text = "2"; } private void set2() ...{ this.tbA.Text = "1"; this.tbB.Text = "1.5"; this.tbC.Text = "2"; this.tbD.Text = "2.5"; this.tbK.Text = "1"; this.tbL.Text = "2"; } private void set3() ...{ this.tbA.Text = "0.5"; this.tbB.Text = "1.5"; this.tbC.Text = "2"; this.tbD.Text = "3"; this.tbK.Text = "3"; this.tbL.Text = "2"; } private void set4() ...{ this.tbA.Text = "1.5"; this.tbB.Text = "1.5"; this.tbC.Text = "2"; this.tbD.Text = "2.5"; this.tbK.Text = "0.5"; this.tbL.Text = "2"; } private void set5() ...{ this.tbA.Text = "2"; this.tbB.Text = "2.5"; this.tbC.Text = "3"; this.tbD.Text = "3.5"; this.tbK.Text = "4"; this.tbL.Text = "6"; } private void set6() ...{ this.tbA.Text = "0.5"; this.tbB.Text = "1.5"; this.tbC.Text = "2"; this.tbD.Text = "2.5"; this.tbK.Text = "2"; this.tbL.Text = "4"; } public Form1() ...{ InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) ...{ //set1(); } private void Form1_Paint(object sender, PaintEventArgs e) ...{ } private void pictureBox1_Paint(object sender, PaintEventArgs e) ...{ float d; float x1; float x2; float y1; float y2; PointF p1; PointF p2; int unit = 40;//放大倍数 Font font = new Font("MS UI Gothic", 12); SolidBrush brush = new SolidBrush(Color.Black); float interval = 0.001F; //步进刻度,值越小越精确(必须大小0),但速度也越慢 PointF o = new PointF(this.pictureBox1.Width / 2, this.pictureBox1.Height / 2); e.Graphics.DrawLine(Pens.Red, 0, this.pictureBox1.Height / 2, this.pictureBox1.Width, this.pictureBox1.Height / 2); e.Graphics.DrawLine(Pens.Red, this.pictureBox1.Width / 2, 0, this.pictureBox1.Width / 2, this.pictureBox1.Height); e.Graphics.DrawString("O", font, brush, o); if (type1 == 0) ...{ for (d = -6.28F; d < 6.28F; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(unit * System.Math.Sin(d)); y2 = o.Y - (float)(unit * System.Math.Sin(d + interval)); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Black, p1, p2); } } else if (type1 == 1) ...{ //set1(); PointF o1 = new PointF(this.pictureBox1.Width/2, this.pictureBox1.Height / 4); e.Graphics.DrawString("1", font, brush, o1); if (type2 == 1) ...{ for (d = 0; d < a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(this.pictureBox1.Height / 4); y2 = o.Y - (float)(this.pictureBox1.Height / 4); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Black, p1, p2); tempx = x2; tempy = this.pictureBox1.Height/2; } PointF o2 = new PointF(tempx, tempy); e.Graphics.DrawString(a.ToString(), font, brush, o2); } else if (type2 == 2) ...{ for (d = a; d < 2*a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(this.pictureBox1.Height / 4); y2 = o.Y - (float)(this.pictureBox1.Height / 4); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } PointF o2 = new PointF(tempx, tempy); e.Graphics.DrawString(a.ToString(), font, brush, o2); } else if (type2 == 3) ...{ for (d = a; d <b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(this.pictureBox1.Height / 4); y2 = o.Y - (float)(this.pictureBox1.Height / 4); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } PointF o2 = new PointF(tempx, tempy); e.Graphics.DrawString(a.ToString(), font, brush, o2); } } else if (type1 == 2) ...{ //set2(); PointF o1 = new PointF(this.pictureBox1.Width/2, this.pictureBox1.Height / 4); e.Graphics.DrawString("1", font, brush, o1); if (type2 == 1) ...{ for (d = 0; d <=a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1*unit); y2 = o.Y - (float)(1*unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1=o.Y-(float)(unit*((b-d)/(b-a))); y2=o.Y-(float)(unit*((b-d-interval)/(b-a))); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if(type2==2) ...{ for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1=o.Y-(float)(unit*((d-a)/(b-a))); y2=o.Y-(float)(unit*((d-a-interval)/(b-a))); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = b; d <= (b+a); d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 3) ...{ for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(unit * ((d - a) / (b - a))); y2 = o.Y - (float)(unit * ((d - a - interval) / (b - a))); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = b; d <= c; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = c; d < d1; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(unit * ((d1 - d) / (d1 - c))); y2 = o.Y - (float)(unit * ((d1 - d - interval) / (d1 - c))); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } } else if (type1 == 3) ...{ //set3(); PointF o1 = new PointF(this.pictureBox1.Width / 2, this.pictureBox1.Height / 4); e.Graphics.DrawString("1", font, brush, o1); if (type2 == 1) ...{ for (d = 0; d <= a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(unit * (System.Math.Pow((b-d)/(b-a),k))); y2 = o.Y - (float)(unit * (System.Math.Pow((b - d - interval) / (b - a),k))); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 2) ...{ for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(unit * (System.Math.Pow((d - a) / (b - a), k))); y2 = o.Y - (float)(unit * (System.Math.Pow((d - a - interval) / (b - a), k))); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = b; d <= (a+b); d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 3) ...{ for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(unit * (System.Math.Pow((d - a) / (b - a), k))); y2 = o.Y - (float)(unit * (System.Math.Pow((d - a - interval) / (b - a), k))); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = b; d <= c; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = c; d < d1; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(unit * (System.Math.Pow((d1 - d) / (d1- c), k))); y2 = o.Y - (float)(unit * (System.Math.Pow((d1 - d - interval) / (d1 - c), k))); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } } else if (type1 == 4) ...{ //set4(); PointF o1 = new PointF(this.pictureBox1.Width / 2, this.pictureBox1.Height / 4); e.Graphics.DrawString("1", font, brush, o1); if (type2 == 3) ...{ for (d = 0; d <= 2*a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(System.Math.Exp(-((d-a)/k)*((d-a)/k)) * unit); y2 = o.Y - (float)(System.Math.Exp(-((d-interval - a) / k) * ((d-interval - a) / k)) * unit ); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 1) ...{ for (d = 0; d <= a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = a; d <= 2 * a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(System.Math.Exp(-((d - a) / k) * ((d - a) / k)) * unit); y2 = o.Y - (float)(System.Math.Exp(-((d - interval - a) / k) * ((d - interval - a) / k)) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 2) ...{ for (d = a; d <= 2 * a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(System.Math.Exp(-((d - a) / k) * ((d - a) / k)) * unit); y2 = o.Y - (float)(1-System.Math.Exp(-((d - interval - a) / k) * ((d - interval - a) / k)) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } } else if (type1 == 5) ...{ //set5(); PointF o1 = new PointF(this.pictureBox1.Width / 2, this.pictureBox1.Height / 4); e.Graphics.DrawString("1", font, brush, o1); if (type2 == 3) ...{ for (d = 0; d <= 2 * a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)((1.0/(1+k*System.Math.Pow(d-a,l))) * unit); y2 = o.Y - (float)((1.0 / (1 + k * System.Math.Pow(d-interval - a, l))) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 1) ...{ for (d = 0; d <= a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = a; d <= 2 * a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)((1.0 / (1 + k * System.Math.Pow(d - a, l))) * unit); y2 = o.Y - (float)((1.0 / (1 + k * System.Math.Pow(d - interval - a, l))) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 2) ...{ for (d = a; d <= 2 * a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)((1.0 / (1 + k * System.Math.Pow(d - a, -l))) * unit); y2 = o.Y - (float)((1.0 / (1 + k * System.Math.Pow(d - interval - a, -l))) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = 2*a; d <= 3*a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } } else if (type1 == 6) ...{ //set6(); PointF o1 = new PointF(this.pictureBox1.Width / 2, this.pictureBox1.Height / 4); e.Graphics.DrawString("1", font, brush, o1); if (type2 == 3) ...{ for (d =-b; d < -a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)((0.5 + 0.5 * System.Math.Sin((d-(a+b)/2)*(System.Math.PI/(b-a)))) * unit); y2 = o.Y - (float)((0.5 + 0.5 * System.Math.Sin((d-interval - (a + b) / 2) * (System.Math.PI / (b - a)))) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = -a; d < a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1* unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)((0.5 - 0.5 * System.Math.Sin((d - (a + b) / 2) * (System.Math.PI / (b - a)))) * unit); y2 = o.Y - (float)((0.5 - 0.5 * System.Math.Sin((d - interval - (a + b) / 2) * (System.Math.PI / (b - a)))) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 1) ...{ for (d = 0; d < a; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)((0.5 - 0.5 * System.Math.Sin((d - (a + b) / 2) * (System.Math.PI / (b - a)))) * unit); y2 = o.Y - (float)((0.5 - 0.5 * System.Math.Sin((d - interval - (a + b) / 2) * (System.Math.PI / (b - a)))) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } else if (type2 == 2) ...{ for (d = a; d < b; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)((0.5 + 0.5 * System.Math.Sin((d - (a + b) / 2) * (System.Math.PI / (b - a)))) * unit); y2 = o.Y - (float)((0.5 + 0.5 * System.Math.Sin((d - interval - (a + b) / 2) * (System.Math.PI / (b - a)))) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } for (d = b; d < c; d += interval) ...{ x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(1 * unit); y2 = o.Y - (float)(1 * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } } } } private void button1_Click(object sender, EventArgs e) ...{ InitArray(); Graphics g = Graphics.FromHwnd(this.pictureBox1.Handle); PaintEventArgs e1 = new PaintEventArgs(g, this.pictureBox1.ClientRectangle); this.pictureBox1_Paint(this.pictureBox1, e1); g.Dispose(); } } } |
首先:重载 pictureBox1_Paint 函数
进行画图时思想很简单,确定起始位置,结束位置,本程序中我用System.Drawing.PointF对象存储点坐标。使用 e.Graphics.DrawLine(Pens.Black, 坐标1, 坐标2)进行线条的绘制。下面我将拿绘制正态分布图形介绍下:程序段如下:
for (d = a; d <= 2 * a; d += interval) { x1 = o.X + d * unit; x2 = o.X + (d + interval) * unit; y1 = o.Y - (float)(System.Math.Exp(-((d - a) / k) * ((d - a) / k)) * unit); y2 = o.Y - (float)(1-System.Math.Exp(-((d - interval - a) / k) * ((d - interval - a) / k)) * unit); p1 = new PointF(x1, y1); p2 = new PointF(x2, y2); e.Graphics.DrawLine(Pens.Blue, p1, p2); } |
先确定起始坐标(x1,y1),再结合正态分布在增加一个步进刻度的情况下确定(x2,y2),接下来调用e.Graphics.DrawLine进行画图。
最后还有一点,由于每次重新画图的时候都要调用private void pictureBox1_Paint(object sender,PaintEventArgs e),其不是用户定义方法,所以用户句柄重新获取PictureBox_Paint方法,重新绘制图形,代码段如下:
Graphics g = Graphics.FromHwnd(this.pictureBox1.Handle); PaintEventArgs e1 = new PaintEventArgs(g, this.pictureBox1.ClientRectangle); this.pictureBox1_Paint(this.pictureBox1, e1); g.Dispose(); |