抛物线绘制(visual studio C# winform)

项目2说明:
绘制坐标系,并实现曲线y = aX2+bX+c (a、b、c为输入参数0<=x<=100,曲线的形状a,b,c,的输入值决定,可自行给出测试数据)的绘制,并实现图形随窗体的四向(上、下、左、右)拉伸,可实现绘制信息的存储和打开。可在窗体上添加系数a、b、c、的输入框。
在这里插入图片描述

1、新建一个Win Form窗体应用程序,工程名称drawCurveAccordingFunction,窗体名称Form2_gsq.cs;

2、在窗体上依次拖拽控件,顺序和属性设置如下表所示:

序号 控件类型 控件名称 属性 值
1 tableLayoutPanel tableLayoutPanel1_gsq Dock Fill
2 tableLayoutPanel tableLayoutPanel2_gsq Dock Fill
3 label label1_gsq Text Y=
4 label Label2_gsq Text x^2+
5 label Label3_gsq Text x+
6 textBox textBox1_gsq Text 空字符串
7 textBox textBox2_gsq Text 空字符串
8 textBox textBox3_gsq Text 空字符串
9 button button1_gsq Text 绘制
10 button Button2_gsq Text 保存
11 button Button3_gsq Text 打开
12 pictureBox pictureBox1_gsq Dock Fill
13 Form Form2_gsq Text 抛物线绘制
14 pictureBox pictureBox1_gsq Backcolor 128, 128, 255
15 Form Form2_gsq FormBoderStyle Sizable

3、定义公共变量
int PBwidth ;
int PBheight;
Bitmap myImage;
private double a,b,c;
4、给按钮button1_gsq添加click事件:

private void button1_Click(object sender, EventArgs e)
{
PBwidth = pictureBox1_gsq.Width;
PBheight = pictureBox1_gsq.Height;
a = double.Parse(textBox1_gsq.Text);
b = double.Parse(textBox2_gsq.Text);
c = double.Parse(textBox3_gsq.Text);

        PaintCuv2();
        pictureBox1_gsq.Image = myImage;
    }

5、给按钮button2_gsq添加click事件:
private void button2_Click(object sender, EventArgs e)
{
string filePath = “”;
SaveFileDialog s1 = new SaveFileDialog();
s1.Title = “保存CFG文件”;
s1.Filter = “cfg文件(.cfg)|.cfg”;
s1.FilterIndex = 1;
s1.FileName = “曲线参数”;
if (s1.ShowDialog() == DialogResult.OK) filePath = s1.FileName;
else return;
FileStream file = new FileStream(@filePath, FileMode.Create);
byte[] data = System.Text.Encoding.Default.GetBytes(textBox1_gsq.Text+“\n”);
file.Write(data, 0, data.Length);
file.Flush();

        data = System.Text.Encoding.Default.GetBytes(textBox2_gsq.Text + "\n");
        file.Write(data, 0, data.Length);
        file.Flush();

        data = System.Text.Encoding.Default.GetBytes(textBox3_gsq.Text + "\n");
        file.Write(data, 0, data.Length);
        file.Flush();

        file.Close();
        file.Dispose();
    }

6、给按钮button3_gsq添加click事件:
private void button3_Click(object sender, EventArgs e)
{
string resultFile = “”;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = “D:\Patch”;
openFileDialog1.Filter = “All files (.)|.|cfg files (.cfg)|.cfg”;
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
resultFile = openFileDialog1.FileName;
string CFGStr = ReadTxtFileLineByLine(resultFile);
a =double.Parse(CFGStr.Split(’ ‘)[0]);
b = double.Parse(CFGStr.Split(’ ‘)[1]);
c = double.Parse(CFGStr.Split(’ ')[2]);
PaintCuv2();
}

    }

7、定义绘制函数图片的函数PaintCuv2
private void PaintCuv2()
{

        Debug.Print("2----" + PBwidth.ToString() + "," + PBheight.ToString());
        int LeftW = 30;
        int ButH = 30;
        myImage = new Bitmap(PBwidth, PBheight);
        Graphics g = Graphics.FromImage(myImage);
        g.Clear(Color.White);
        g.TranslateTransform(0, PBheight);
        g.ScaleTransform(1, -1);//反转坐标
        g.TranslateTransform(LeftW, ButH);
        Debug.Print("3----" + PBwidth.ToString() + "," + PBheight.ToString());
        Pen pGrid = new Pen(Color.Blue, 2f);
        g.DrawLine(pGrid, 0, 0, 0, PBheight- 2*ButH);
        pGrid = new Pen(Color.Red, 2f);
        g.DrawLine(pGrid, 0, 0, PBwidth-2* LeftW, 0);
        pGrid = new Pen(Color.Green, 2f);
        g.DrawLine(pGrid, PBwidth - 2 * LeftW, 0, PBwidth - 2 * LeftW, PBheight - 2 * ButH);
        pGrid = new Pen(Color.Black, 1f);
        Font DrawFont = new Font("宋体", 12);
        Brush DrawBrush = new SolidBrush(Color.FromArgb(0, 0, 0));
        for (int i=0;i<=10;i++)
        {
            g.DrawLine(pGrid, (PBwidth - 2 * LeftW) *i/10, 0, (PBwidth - 2 * LeftW) * i / 10, PBheight - 2 * ButH);
            g.DrawLine(pGrid, 0, (PBheight - 2 * ButH)*i/10, PBwidth - 2 * LeftW, (PBheight - 2 * ButH) * i / 10);
            
        }
        g.ScaleTransform(1, -1);//反转坐标
        for (int i=0;i<=10;i++)
        {
            g.DrawString((i * 10).ToString(), DrawFont, DrawBrush, new Point((PBwidth - 2 * LeftW) * i / 10-10, 5));
        }
        g.ScaleTransform(1, -1);//反转坐标

        pGrid = new Pen(Color.Black, 2f);

        float stepLength = 1f;                   //画线时x标的间隔值
        float ii = 0.0f;
        while (ii <= (PBwidth - 2 * LeftW))
        {
            float fleft_x= ii * 100 / (PBwidth - 2 * LeftW);
            float fright_x= (ii+ stepLength) * 100 / (PBwidth - 2 * LeftW);
            float fleft_y = f_y(fleft_x);
            float fright_y = f_y(fright_x);

            fleft_y = (PBheight - 2 * ButH)* fleft_y / 100;
            fright_y = (PBheight - 2 * ButH) * fright_y / 100;

            fleft_x = ii;
            fright_x = ii + stepLength;
            g.DrawLine(pGrid, fleft_x, fleft_y, fright_x, fright_y);
            //Debug.Print(ii.ToString()+"_"+fleft_x.ToString() + "," + fleft_y.ToString() + "    " + fright_x.ToString() + "," + fright_y.ToString());
            ii = ii + stepLength;


        }
                    
    }

8、定义写保存文件函数ReadTxtFileLineByLine
private string ReadTxtFileLineByLine(string FilePath)
{
string Res = “”;
StreamReader sr = new StreamReader(FilePath, Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
Res += line + " ";
}
return Res.Trim();
}
9、添加窗体载入事件Load
private void Form2_Load(object sender, EventArgs e)
{
textBox1_gsq.Text = “-0.0002”;
textBox2_gsq.Text = “0.206”;
textBox3_gsq.Text = “-2.48”;
}

10、定义函数计算过程
private float f_y(float x)
{
return (float)(c + b * x + a * x * x); }

11、给图片控件增加Paint事件
private void pictureBox1_gsq_Paint(object sender, PaintEventArgs e)
{
PBwidth = pictureBox1_gsq.Width;
PBheight = pictureBox1_gsq.Height;
Debug.Print(“1----” + PBwidth.ToString() + “,” + PBheight.ToString());
PaintCuv2();
pictureBox1_gsq.Image = myImage;
}

  • 11
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

次郎不小

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值