1.
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;
using System.IO;
namespace 打开点文件进行操作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<Class1.POINT> LP = new List<Class1.POINT>();
List<float> LX = new List<float>();
List<float> LY = new List<float>();
openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr=new StreamReader(openFileDialog1.FileName);
string PATH = openFileDialog1.FileName;
string str = sr.ReadLine();
str = sr.ReadLine();
while (str != null)
{
string[] s = str.Split('\t');
Class1.POINT PT = new Class1.POINT();
PT.ID = s[0];
PT.Name = s[1];
PT.Point = new PointF((float)Convert.ToDouble(s[2]), (float)Convert.ToDouble(s[3]));
PT.Color = Color.Black;
LP.Add(PT);
LX.Add((float)Convert.ToDouble(s[2]));
LY.Add((float)Convert.ToDouble(s[3]));
str = sr.ReadLine();
}
sr.Close();
Form2 fm2 = new Form2(LX,LY,LP,PATH);
fm2.Show();
}
}
}
}
2.
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;
using System.IO;
namespace 打开点文件进行操作
{
public partial class Form2 : Form
{
List<Class1.LINE> LL = new List<Class1.LINE>();
List<Class1.POINT> LP = new List<Class1.POINT>();
List<float> LX = new List<float>();
List<float> LY = new List<float>();
string PATH;
Graphics gra;
int M = 0;
List<int> I = new List<int>();
float Xmin, Xmax, Ymin, Ymax, KX, KY, KK, fm2Height, fm2Width;
public Form2( List<float> lx, List<float> ly,List<Class1.POINT> lp,string pat)
{
LX = lx;
LY = ly;
LP = lp;
PATH = pat;
InitializeComponent();
}
private void Form2_Shown(object sender, EventArgs e)
{
for (int i = 0; i < LP.Count; i++)
{
DrawPoint(LP[i]);
}
}
private void DrawLine(Class1.LINE L)
{
Pen mypen = new Pen(L.Color);
Class1.POINT p1=new Class1.POINT();
Class1.POINT p2 = new Class1.POINT();
p1.Point = L.P1;
p2.Point = L.P2;
gra.DrawLine(mypen,p1.Point,p2.Point);
}
private void DrawPoint(Class1.POINT P)
{
Brush mybs = new SolidBrush(P.Color);
gra.FillEllipse(mybs,change1(P).Point.X-4,change1(P).Point.Y-4,8,8);
}
private Class1.POINT change1(Class1.POINT P)
{
Class1.POINT p = new Class1.POINT();
p.Point = new PointF((P.Point.X - Xmin) / KK, (P.Point.Y - Ymin) / KK);
return p;
}
private void Form2_Load(object sender, EventArgs e)
{
gra = this.panel1.CreateGraphics();
LX.Sort();
LY.Sort();
Xmin = LX[0];
Xmax = LX[LX.Count - 1];
Ymin = LY[0];
Ymax = LY[LY.Count - 1];
fm2Height = this.panel1.Height;
fm2Width = this.panel1.Width;
KX = (Xmax - Xmin) / fm2Width;
KY = (Ymax - Ymin) / fm2Height;
KK = Math.Max(KX,KY);
}
int K = 0;
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
if (K == 1)
{
for (int i = 0; i < LP.Count; i++)
{
if (Math.Sqrt(Math.Pow(e.X - change1(LP[i]).Point.X, 2) + Math.Pow(e.Y - change1(LP[i]).Point.Y, 2)) < 4)
{
MessageBox.Show("ID"+'\t'+"NAME"+'\t'+"X"+'\t'+"Y"+'\n'+LP[i].ID+'\t'+LP[i].Name+'\t'+LP[i].Point.X+'\t'+LP[i].Point.Y);
}
}
}
if (K == 2)
{
for(int i=0;i<LP.Count;i++)
{
if (Math.Sqrt(Math.Pow(e.X - change1(LP[i]).Point.X, 2) + Math.Pow(e.Y - change1(LP[i]).Point.Y, 2)) < 4)
{
Class1.POINT p = new Class1.POINT();
p.Color = Color.Yellow;
p.Point = LP[i].Point;
DrawPoint(p);
}
}
}
if (K == 3)
{
for (int i = 0; i < LP.Count; i++)
{
if (Math.Sqrt(Math.Pow(e.X - change1(LP[i]).Point.X, 2) + Math.Pow(e.Y - change1(LP[i]).Point.Y, 2)) < 4)
{
M++;
I.Add(i);
}
}
if (M != 0 && M % 3 == 0)
{
for (int i = 0; i < 3; i++)
{
Class1.LINE l = new Class1.LINE();
l.Color = Color.Black;
l.P1 =change1(LP[I[M - (i + 1) % 3-1]]).Point;
l.P2 =change1(LP[I[M - i % 3 - 1]]).Point;
LL.Add(l);
DrawLine(l);
}
}
}
if (K == 4)
{
for (int i = 0; i < LL.Count; i++)
{
PointF mp = new PointF(e.X, e.Y);
PointF cp = new PointF((LL[i].P1.X + LL[i].P2.X) / 2, (LL[i].P1.Y + LL[i].P2.Y) / 2);
if (PtPDIS(mp, cp) < PtPDIS(LL[i].P1, LL[i].P2) / 2)
{
if (PtLDIS(LL[i], mp) < 2)
{
Class1.LINE l = new Class1.LINE();
l = LL[i];
l.Color = Color.Yellow;
DrawLine(l);
}
}
}
}
}
private float PtLDIS(Class1.LINE l, PointF p)
{
float A, B, C, D;
A = (l.P1.Y - l.P2.Y) / (l.P1.X - l.P2.X);
B = -1;
C = l.P2.Y - A * l.P2.X;
D = (float)(Math.Abs(A * p.X + B * p.Y + C) / Math.Sqrt(A*A+B*B));
return D;
}
private float PtPDIS(PointF p1, PointF p2)
{
return ((float)(Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y- p2.Y, 2))));
}
private void 点击某点显示该点信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
K = 1;
}
private void 点击某点该点变黄ToolStripMenuItem_Click(object sender, EventArgs e)
{
K = 2;
}
private void 点击三点连线ToolStripMenuItem_Click(object sender, EventArgs e)
{
K = 3;
}
private void 点击某线该线变黄ToolStripMenuItem_Click(object sender, EventArgs e)
{
K = 4;
}
private void 拖拉点改变该点位置ToolStripMenuItem_Click(object sender, EventArgs e)
{
K = 5;
}
Class1.POINT PPP = new Class1.POINT();
int Z = 0;
int I1;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (K == 5)
{
for (int i = 0; i < LP.Count; i++)
{
if (Math.Sqrt(Math.Pow(e.X - change1(LP[i]).Point.X, 2) + Math.Pow(e.Y - change1(LP[i]).Point.Y, 2)) < 4)
{
Z = 1;
I1 = i;
PPP.Color = this.BackColor;
PPP.Point = LP[i].Point;
DrawPoint(PPP);
PPP = LP[i];
}
}
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (K == 5)
{
if (Z == 1)
{
PPP.Color = this.BackColor;
DrawPoint(PPP);
PPP.Point = change2(e.X, e.Y);
PPP.Color = Color.Black;
DrawPoint(PPP);
}
for (int i = 0; i < LP.Count; i++)
{
if (i != I1)
{
DrawPoint(LP[i]);
}
}
}
}
private PointF change2(int x, int y)
{
PointF p = new PointF(x * KK + Xmin, y * KK + Ymin);
return(p);
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (K == 5)
{
if (Z == 1)
{
// Class1.POINT p = new Class1.POINT();
PPP.Point = change2(e.X,e.Y);
// p.ID = LP[I1].ID;
// p.Name = LP[I1].Name;
PPP.Color = Color.Black;
LP[I1] = PPP;
DrawPoint(PPP);
Z = 2;
}
save();
}
}
private void save()
{
StreamWriter sw = new StreamWriter(PATH,false);
sw.WriteLine("ID"+'\t'+"NAME"+'\t'+"X"+'\t'+"Y");
for (int i = 0; i < LP.Count; i++)
{
sw.WriteLine(LP[i].ID+'\t'+LP[i].Name+'\t'+LP[i].Point.X+'\t'+LP[i].Point.Y);
}
sw.Close();
}
}
}
3.建立一个公共类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace 打开点文件进行操作
{
public class Class1
{
public struct POINT
{
private PointF point;
public PointF Point
{
get { return point; }
set { point = value; }
}
private string id;
public string ID
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private Color color;
public Color Color
{
get { return color; }
set { color = value; }
}
}
public struct LINE
{
private PointF p1;
public PointF P1
{
get { return p1; }
set { p1 = value; }
}
private PointF p2;
public PointF P2
{
get { return p2; }
set { p2 = value; }
}
private Color color;
public Color Color
{
get { return color; }
set { color = value; }
}
}
}
}