在.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();