c#画拟合线(代码不好,一种实现思路)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace imath
{
    public partial class Form1 : Form
    {  
        public Form1()
        {
            InitializeComponent();
        }

        private ArrayList datas=new ArrayList();
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbX.Text) || string.IsNullOrEmpty(tbY.Text))
            {
                MessageBox.Show("输入有误");
                tbX.Text = "";
                tbY.Text = "";
                return;

            }


            list.Items.Add(string.Format("({0},{1})", tbX.Text, tbY.Text));
            double[] xy = { double.Parse(tbX.Text), double.Parse(tbY.Text) };
            datas.Add(xy);

        }

        private void compute() {
            int n = 6;
            
            double ex = 0, ey = 0, ex2 = 0, exy = 0;
            double k=0, b=0;


            foreach (double[] xy in datas)
            {
               ex+=xy[0];
               ey += xy[1];
               ex2 += Math.Pow(xy[0], 2); ;
               exy += xy[0] * xy[1];
               
            }

            MessageBox.Show(string.Format("{0},{1},{2},{3}", ex, ey, ex2, exy));

            k = (n * exy - ex * ey) / (n * ex2 - Math.Pow(ex, 2));
            b = (ex2 * ey - ex * exy) / (n * ex2 - Math.Pow(ex, 2));

            MessageBox.Show(String.Format("Y={0}X+{1} {2}", k, b,datas.Count));

            
        
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            double[][] ds ={
                         new double[]{0.9,1.1},
                          new double[]{2.5,1.6},
                          new double[]{3.3,2.6},
                         new double[] {4.5,3.2},
                          new double[]{5.7,4.0},
                          new double[]{6.7,5.0}
                         };
            foreach (double[] d in ds)
            {
                datas.Add(d);
            }

           // StringBuilder sb = new StringBuilder();
           // foreach( double[] xy in datas){
          //      sb.Append(String.Format("{0},{1}\n", xy[0], xy[1]));
           // }
            //MessageBox.Show(sb.ToString());
          //  compute();  
            draw();
           
        }

        private void draw() {
            double[][] dots ={
                           new double[]{0.9,1.1},
                          new double[]{2.5,1.6},
                          new double[]{3.3,2.6},
                         new double[] {4.5,3.2},
                          new double[]{5.7,4.0},
                          new double[]{6.7,5.0}  
                            };


            double Xmax = 0, Ymax = 0,Xmin=0,Ymin=0;
            for (int i = 0; i < dots.Length; i++) {
                if (dots[i][0] > Xmax) Xmax = dots[i][0];
                if (dots[i][1] > Ymax) Ymax = dots[i][1];
                if (dots[i][0] < Xmin) Xmin = dots[i][0];
                if (dots[i][1] < Ymin) Ymin = dots[i][1];             
            
            }

            double rangeX = (Xmax - Xmin)+10;
            double rangeY = (Ymax - Ymin);
            MessageBox.Show("" + rangeX + "," + rangeY + "\n");

           // Xmin = Xmin<0?Math.Abs(Xmin):0;
           // Ymin =Ymin<0?Math.Abs(Ymin):0;

            int[,] pix=new int[dots.Length,2];


            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dots.Length; i++)
            {
                pix[i,0] = (int)((dots[i][0]-Xmin)/rangeX*200);
                pix[i,1] =220-(int)((dots[i][1]-Ymin)/rangeY*200);
                sb.Append("" + pix[i, 0] + "," + pix[i, 1] + "\n");



            }

            MessageBox.Show(sb.ToString());
           


            Bitmap bmap = new Bitmap(500, 500);
            Graphics gph = Graphics.FromImage(bmap);
            gph.Clear(Color.White);

            gph.DrawLine(Pens.Black, new Point(0,20), new Point(0, 220));
            gph.DrawLine(Pens.Black, new Point(0, 220), new Point(200, 220));

            for (int i = 0; i < dots.Length; i++)
            {
                //int tx = (int)((dots[i][0] + Xmin) / rangeX * 200);
               // int ty=(int)((dots[i][1]+Ymin)*20);
                int tx = (int)dots[i][0];
                int ty = (int)dots[i][1];
                tx = pix[i, 0];
               ty = pix[i, 1];
               gph.FillEllipse(Brushes.Blue, new Rectangle(tx, ty, 3, 3));
               gph.DrawString(string.Format("({0},{1})",dots[i][0],dots[i][1]),new Font("宋体",7),Brushes.Blue,new Point(tx-3,ty-3));
               
            }



            for (int i = 1; i <dots.Length; i++) {
                gph.DrawLine(Pens.Blue,new Point(pix[i-1,0],pix[i-1,1]),new Point(pix[i,0],pix[i,1]));            
            }



           
           pictureBox1.Image = bmap;
        
        }




        
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值