C#窗体应用程序运用二维数组计算 明氏距离 切氏距离 绝对值距离 欧式距离 欧几里得距离

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Point pstart = new Point(0, 0);//定义线起点和终点
        private Point pend = new Point(0, 0);
        Graphics g;//鼠标指针
        int clicknum = 0;
        int a = 0;
        int[,] intArray = new int[1000000, 2];
        public Form1()
        {
            InitializeComponent();
            clicknum = 0;
            a = 0;
            g = pictureBox1.CreateGraphics();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.pictureBox1.Refresh();
            Graphics g = pictureBox1.CreateGraphics();
            a = 1;
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
          //  e.Graphics.DrawLine(System.Drawing.Pens.Blue, pstart, pend);
        }
        private double det(double x1, double y1) //这都是定义的方法,计算方法
        {
            return Math.Pow(x1 - y1, 2);
        }
        private double abc(double x1, double y1)//这都是定义的方法,计算方法
        {
            return Math.Abs(x1-y1); 
        }
        private double bcd(double x1, double y1)//这都是定义的方法,计算方法
        {
            return Math.Pow(x1 - y1, clicknum);
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        { if (a >0)
            {
                if (clicknum > 0)
                {
                    Pen pen = new Pen(Color.MediumSpringGreen, 5);//画出来的线的颜色
                    pend.X = e.X;
                    pend.Y = e.Y;
                    g.DrawLine(pen, pstart, pend);
                    pstart.X = pend.X;
                    pstart.Y = pend.Y;
                }
                else
                {
                    pstart.X = e.X;
                    pstart.Y = e.Y;
                }
                intArray[clicknum, 0] = e.X;//记录点的坐标,记录到数组里面
                intArray[clicknum, 1] = e.Y;
                clicknum = clicknum + 1;
                }
        }
                //后面几个都是计算距离
        private void button2_Click(object sender, EventArgs e)//点击,计算距离
        {
            int i;
            double m;
            double s = 0;
            for (i = 0; i < clicknum - 1; i++)
            {
                s += det(intArray[i, 0], intArray[i, 1]);
            }
            m = Math.Pow(s, 0.5);
            String b = m.ToString();//强制转换double类型的d为String类型
            // return m;
            textBox1.Text = b;//让b的值显示在文本框中
        }
        private void button3_Click(object sender, EventArgs e)
        {
            int i;
            double s = 0;
            for (i = 0; i < clicknum - 1; i++)
            {
                s += abc(intArray[i,0], intArray[i,1]);
            }
            String c = s.ToString();
            textBox2.Text = c;
            }
                private void button4_Click(object sender, EventArgs e)
        {
            int i;
            double M;
            double s = 0;
            for (i = 0; i < clicknum - 1; i++)
            {
                s += bcd(intArray[i,0], intArray[i,1]);
            }
            M = Math.Pow(s,1/clicknum);
            string d = M.ToString();
            textBox3.Text = d;
        }
            private void button5_Click(object sender, EventArgs e)
        {
//            double Qlength()
//{
            int i;
            double a, b = 0;
            for (i = 0; i < clicknum - 1; i++)
            {
                a = abc(intArray[i,0], intArray[i,1]);
                b =Math.Max(a, b);
            }
            string h = b.ToString();
            textBox4.Text = h;
        }
           }
}

button的click事件每一个都要绑定,一定不要忘了,还有picture的mouse事件都根据代码,有的都要绑定。

下面是设计的窗口可以根据自己的情况更改窗口和按钮大小

在这里插入图片描述
下面是结果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值