本文主要是介绍利用C#生成一个简单的TIN三角网,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
###输入TXT数据
private void inputbutton_Click(object sender, EventArgs e){OpenFileDialog opg = new OpenFileDialog();opg.Title = "选择文本数据";opg.Filter = "Txt数据(*.txt)|*.txt";opg.ShowDialog();strName = opg.FileName;if (strName == "") return;inputBox.Text = strName;FileStream fs = new FileStream(strName, FileMode.Open);StreamReader sr = new StreamReader(fs, Encoding.Default);string strLine = sr.ReadLine();while (strLine != "" && strLine != null){Lines.Add(strLine);Console.WriteLine(strLine);strLine = sr.ReadLine();}Point point = new Point();for (int i = 1; i < Lines.Count; i++){string[] strArray = Lines[i].Split(charArray);//point. = Convert.ToInt32(strArray[0]);point.X = Convert.ToInt32(strArray[1]);point.Y = Convert.ToInt32(strArray[2]);pointList.Add(point);}}
###生成TIN三角网
private void probutton_Click(object sender, EventArgs e){g = this.CreateGraphics();double ang;this.Refresh();ArrayList tinlines = new ArrayList();//定义与第一点最近的点double mindis = Distance((Point)pointList[0], (Point)pointList[1]);double dis;int count = 1;TinLine tl = new TinLine();for(int i = 1;i<pointList.Count ;i++){dis = Distance((Point)pointList[0], (Point)pointList[i]);if (dis < mindis){mindis = dis;count = i;}}//将第一条边反向已进行三角形扩展tl.Begin = (Point)pointList[0];tl.End = (Point)pointList[count];tinlines.Add(tl);TinLine line = new TinLine();Point a = ((TinLine)tinlines[0]).Begin;Point b = ((TinLine)tinlines[0]).End;line.Begin = b;line.End = a;tinlines.Add(line);//对每一条边进行扩展for (int j = 0; j < tinlines.Count; j++){double minang = 0;bool OK;OK = false;TinLine tling1 = new TinLine();TinLine tling2 = new TinLine();for (int i = 0; i < pointList.Count; i++){int youbian;//判断第三点与前两点的位置关系youbian = ZuoYou((Point)pointList[i], ((TinLine)tinlines[j]).Begin, ((TinLine)tinlines[j]).End);if (youbian == 1){//获取角度最大点ang = Angle((Point)pointList[i], ((TinLine)tinlines[j]).Begin, ((TinLine)tinlines[j]).End);if (ang > minang){minang = ang;count = i;}OK = true;}}if (OK == true){//将新生成两条边添加入集合中int t1 = 0;int t2 = 0;tling1.Begin = ((TinLine)tinlines[j]).Begin;tling1.End = (Point)pointList[count];tling2.Begin = (Point)pointList[count];tling2.End = ((TinLine)tinlines[j]).End;tinlines.Add(tling1);tinlines.Add(tling2);for (int i = 0; i < tinlines.Count - 2;i++ ){//判断新生成的两边是否与已生成的边重合if ((tling2.Begin == ((TinLine)tinlines[i]).Begin && tling2.End == ((TinLine)tinlines[i]).End)|| (tling2.Begin == ((TinLine)tinlines[i]).End && tling2.End == ((TinLine)tinlines[i]).Begin)){t2 = 1;}if ((tling1.Begin == ((TinLine)tinlines[i]).Begin && tling1.End == ((TinLine)tinlines[i]).End) || (tling1.Begin == ((TinLine)tinlines[i]).End && tling1.End == ((TinLine)tinlines[i]).Begin)){t1 = 1;}}//两条边都重合if (t2 == 1 && t1 == 1){for (int i = 0; i < 2;i++ ){tinlines.Remove(tinlines[tinlines.Count - 1]);}}//第二条边重合else if (t2 == 1){tinlines.Remove(tinlines[tinlines.Count - 1]);}//第一条边重合else if (t1 == 1){tinlines.Remove(tinlines[tinlines.Count - 2]);} }}tinlines.Remove(tinlines[0]);//将集合中的第一条边删除Pen pen = new Pen(Color.Red, 1);for (int i = 0; i <= tinlines.Count - 1; i++){g.DrawLine(pen, ((TinLine)tinlines[i]).Begin, ((TinLine)tinlines[i]).End);}g.Dispose();}
###程序中用到的自定义方法
public double Distance(Point a, Point b){double d;d = Math.Sqrt((b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y));return d;}
public int ZuoYou(Point c, Point a, Point b){int youbian;double S;S = (a.X - c.X) * (b.Y - c.Y) - (a.Y - c.Y) * (b.X - c.X);if (S > 0){youbian = 1;}else if (S < 0){youbian = -1;}else{youbian = 0;}return youbian;}
public double Angle(Point c, Point a, Point b){double ang;double l1 = Math.Sqrt((b.X - c.X) * (b.X - c.X) + (b.Y - c.Y) * (b.Y - c.Y));double l2 = Math.Sqrt((a.X - c.X) * (a.X - c.X) + (a.Y - c.Y) * (a.Y - c.Y));double l3 = Math.Sqrt((b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y));ang = Math.Acos((l1 * l1 + l2 * l2 - l3 * l3) / (2 * l1 * l2));return ang;}
这篇关于利用C#生成一个简单的TIN三角网的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!