首页 > 其他 > 详细

c# 绘制三角函数图像

时间:2014-04-19 00:32:20      阅读:1675      评论:0      收藏:0      [点我收藏+]
1.先将c#form页面自带的原点坐标(原页面左上角)转移到自己定义的panel标签的中点,并建立直角坐标系,代码如下:
bubuko.com,布布扣
2.根据mousemove将页面的坐标显示出来。
bubuko.com,布布扣
3.根据x坐标获得y坐标,即获得所要函数的点的集合,然后循环输出。
4.从textbox输入一串字符串,优化成已经确定的格式,如(a+b*sin(c*x+d)),即字符串分析,当然有更好的方法。
程序的整体页面:bubuko.com,布布扣
运行实例:
bubuko.com,布布扣
 
bubuko.com,布布扣
 
bubuko.com,布布扣
源代码如下,不完善,有待改进:
bubuko.com,布布扣
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10  
 11 namespace 三角函数图像显示器
 12 {
 13    
 14     public partial class Form1 : Form
 15     {
 16         public string TestString;
 17         public Form1()
 18         {
 19             InitializeComponent();
 20             this.label1.Text = "请输入三角函数,格式为:\n a+b*sin(c*x+d):" ;
 21         }
 22         public int BetterString()
 23         {
 24             TestString = textBox1.Text;
 25             int type = 0;
 26             if (TestString[1] != + && TestString[1] != -)
 27             {
 28                 TestString = TestString.Insert(0, "0");
 29                 TestString = TestString.Insert(1, "+");
 30             }
 31             // Convert.ToString(TestString[4]);
 32             if (TestString[2] == c || TestString[2] == s || TestString[2] == t)
 33             {
 34                 TestString = TestString.Insert(2, "1");
 35                 TestString = TestString.Insert(3, "*");
 36             }
 37          
 38             if (TestString.Substring(4, 3) == "sin" )
 39                 type = 1;
 40             if (TestString.Substring(4, 3) == "cos" )
 41                 type = 2;
 42             if (TestString.Substring(4, 3) == "tan" )
 43                 type = 3;
 44             if (TestString.Substring(4, 3) == "cot" )
 45                 type = 4;
 46             if (TestString[8] == x)
 47             {
 48                 TestString = TestString.Insert(8, "1");
 49                 TestString = TestString.Insert(9, "*");
 50             }
 51             if (TestString.Length <= 12)
 52             {
 53                 TestString = TestString.Insert(11, "+");
 54                 TestString = TestString.Insert(12, "0");
 55             }
 56  
 57             return type;
 58         }
 59  
 60         public Double GetValueY( int x)
 61         {
 62             int type = BetterString();
 63             Double tempValue;
 64             if (TestString[11] == +)
 65                 tempValue = ( Convert.ToInt32(TestString[8]) - 48) * x + (20*( Convert.ToInt32(TestString[12]) - 48));
 66             else
 67                 tempValue = ( Convert.ToInt32(TestString[8]) - 48) * x - (20 * ( Convert.ToInt32(TestString[12]) - 48));
 68             switch (type)
 69             {
 70                 case 1: tempValue = Math.Sin(tempValue*2* Math.PI/(panel1.Width/2)); break;
 71                 case 2: tempValue = Math.Cos(tempValue * 2 * Math.PI / (panel1.Width / 2)); break;
 72                 case 3: tempValue = Math.Tan(tempValue * 2 * Math.PI / (panel1.Width / 2)); break;
 73                 case 4: tempValue = 1/( Math.Tan(tempValue * 2 * Math.PI / (panel1.Width / 2)+1)); break;
 74             }
 75             if (TestString[1] == +)
 76                 tempValue = tempValue * ( Convert.ToInt32(TestString[2]) - 48) + ( Convert.ToInt32(TestString[0]) - 48);
 77             else
 78                 tempValue = ( Convert.ToInt32(TestString[0]) - 48) - tempValue * ( Convert.ToInt32(TestString[2]) - 48);
 79  
 80             return tempValue;
 81         }
 82  
 83         public void GetXY( Graphics g)
 84         {
 85             Pen myPen = new Pen( Color.Black, 1);
 86             //Graphics g = this.CreateGraphics();
 87             int x1, x2;
 88             x2 = 0;
 89             double  y2;
 90             y2 = 0;
 91             for (x1 = -panel1.Width / 2; x1 < panel1.Width / 2; x1++)
 92             {
 93                 g.DrawLine(myPen, ( float)GetValueY(x1)*60 , x1, ( float)GetValueY(x1 + 1)*60, x1 + 1);
 94  
 95             }
 96         }
 97      
 98         private void panel1_MouseMove( object sender, MouseEventArgs e)
 99         {
100             double x1 = panel1.Width / 2, y1 = panel1.Height / 2;
101             double x2 = ((e.X - x1));
102             double y2 = -((e.Y - y1));
103             string str_X2 = x2.ToString();
104             string str_Y2 = y2.ToString();
105             if (str_X2.Length > 5)
106                 str_X2 = str_X2.Remove(5, str_X2.Length - 5);
107             if (str_Y2.Length > 5)
108                 str_Y2 = str_Y2.Remove(5, str_Y2.Length - 5);
109             this.label2.Text = "坐标显示:" + "\n" +"x=" + str_X2 + "\n" + "y=" + str_Y2;
110           
111         }
112  
113         private void button1_Click( object sender, EventArgs e)
114         {
115             Graphics g = panel1.CreateGraphics();
116             Pen myPen = new Pen( Color.Blue, 1);
117           
118             // Set world transform of graphics object to rotate.
119             //   e.Graphics.RotateTransform(45.0F);
120             // Then to translate, prepending to world transform.
121             g.TranslateTransform(panel1.Width / 2F, panel1.Height / 2F);
122             //坐标系顺时针旋转270℃
123            g.RotateTransform(270.0F);
124             g.DrawLine(myPen, 0, -panel1.Width / 2, 0, panel1.Width / 2);
125             g.DrawLine(myPen, -panel1.Height / 2, 0, panel1.Height / 2, 0);
126             BetterString();
127             GetXY(g);
128             // Draw translated, rotated ellipse to screen.
129            //g.DrawLine(new Pen(Color.Blue, 1), 10,10, -10, 10);
130           // DrawSin(g);
131  
132         }
133  
134         private void Clear_Click( object sender, EventArgs e)
135         {
136             Graphics g = panel1.CreateGraphics();
137           g.Clear( Color.White);
138           Pen myPen = new Pen( Color.Blue, 1);
139  
140           // Set world transform of graphics object to rotate.
141           //   e.Graphics.RotateTransform(45.0F);
142           // Then to translate, prepending to world transform.
143           g.TranslateTransform(panel1.Width / 2F, panel1.Height / 2F);
144           //坐标系顺时针旋转270℃
145           g.RotateTransform(270.0F);
146           g.DrawLine(myPen, 0, -panel1.Width / 2, 0, panel1.Width / 2);
147           g.DrawLine(myPen, -panel1.Height / 2, 0, panel1.Height / 2, 0);
148         }
149     }
150 }
View Code

 

c# 绘制三角函数图像,布布扣,bubuko.com

c# 绘制三角函数图像

原文:http://www.cnblogs.com/hopb/p/3673743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!