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事件都根据代码,有的都要绑定。
下面是设计的窗口可以根据自己的情况更改窗口和按钮大小
下面是结果图