//函数——判断P是否在外接圆的内部 public bool JudgeInTriangle(point P, triangle T) { bool Inside = true; point A = new point(); point B = new point(); point C = new point(); A = T.Tp[0]; B = T.Tp[1]; C = T.Tp[2]; double x1, y1, x2, y2, x3, y3; x1 = A.x; y1 = A.y; x2 = B.x; y2 = B.y; x3 = C.x; y3 = C.y; double x0, y0, r, d; x0 = ((B.y - A.y) * (C.y * C.y - A.y * A.y + C.x * C.x - A.x * A.x) - (C.y - A.y) * (B.y * B.y - A.y * A.y + B.x * B.x - A.x * A.x)) / (2 * (C.x - A.x) * (B.y - A.y) - 2 * (B.x - A.x) * (C.y - A.y)); y0 = ((B.x - A.x) * (C.x * C.x - A.x * A.x + C.y * C.y - A.y * A.y) - (C.x - A.x) * (B.x * B.x - A.x * A.x + B.y * B.y - A.y * A.y)) / (2 * (C.y - A.y) * (B.x - A.x) - 2 * (B.y - A.y) * (C.x - A.x)); r = Math.Round(Math.Sqrt((x0 - A.x) * (x0 - A.x) + (y0 - A.y) * (y0 - A.y)), 4); d = Math.Round(Math.Sqrt((x0 - P.x) * (x0 - P.x) + (y0 - P.y) * (y0 - P.y)), 4); if (d <= r) { Inside = true; } else { Inside = false; } return(Inside); }
//构建初始三角网 public void BuildInitialTin() { int a = CP.Count; for (int i = 0; i < PO.Count - 1; i++) { side l1 = new side(); side l2 = new side(); side l3 = new side(); l1.Sp = new List <point>(); l2.Sp = new List <point>(); l3.Sp = new List <point>(); triangle t1 = new triangle(); t1.Ts = new List <side>(); t1.Tp = new List <point>(); t1.Tp.Add(PO[i]); t1.Tp.Add(PO[i + 1]); t1.Tp.Add(CP[0]); l1.Sp.Add(PO[i]); l1.Sp.Add(PO[i + 1]); l2.Sp.Add(PO[i + 1]); l2.Sp.Add(CP[0]); l3.Sp.Add(PO[i]); l3.Sp.Add(CP[0]); t1.Ts.Add(l1); t1.Ts.Add(l2); t1.Ts.Add(l3); T1.Add(t1); } }
//判断点是否位于三角形内 public bool PinT(point p, triangle t) { bool In = true; double s1, s2, s3, s4; s1 = Area(t.Tp[0], t.Tp[1], t.Tp[2]); s2 = Area(t.Tp[0], t.Tp[1], p); s3 = Area(t.Tp[1], t.Tp[2], p); s4 = Area(t.Tp[0], t.Tp[2], p); if (Math.Round(s1, 4) == Math.Round((s2 + s3 + s4), 4)) { In = true; } else { In = false; } return(In); }
//生成平面三角网 public void BuildPlaneTIN() { //循环点 for (int i = 1; i < CP.Count; i++) { int T1count = T1.Count; List <triangle> T3 = new List <triangle>(); //找出所有外接圆包含P的三角形 for (int j = 0; j < T1count; j++) { if (JudgeInTriangle(CP[i], T1[j])) { T3.Add(T1[j]); } } //定位 List <triangle> FT = new List <triangle>(); for (int k = 0; k < T3.Count; k++) { if (PinT(CP[i], T3[k])) { FT.Add(T3[k]); T3.Remove(T3[k]); break; } } if (FT.Count > 0) { if (T3.Count > 0) { int[] Tnote = new int[T3.Count]; //设立标志数组 for (int t = 0; t < T3.Count; t++) { Tnote[t] = 0; } int v = 0; int num = 0; bool F = true; while (F) { if (Tnote[v] == 0) { for (int vv = 0; vv < FT.Count; vv++) { if (JudgeSame(T3[v], FT[vv])) { FT.Add(T3[v]); Tnote[v] = 1; v = -1; break; } else { num = vv; } } if (v == (T3.Count - 1) && num == (FT.Count - 1)) { F = false; } } v += 1; if (v == T3.Count) { F = false; } } } //将三角形加入到T2中 T2.AddRange(FT); //移除T1中三角形; for (int y = 0; y < T2.Count; y++) { T1.Remove(T2[y]); } //寻找公共边 for (int t2 = 0; t2 < T2.Count - 1; t2++) //第一个三角形 { int ii = t2 + 1; //其余三角形 while (ii <= T2.Count - 1) { if (JudgeSame(T2[t2], T2[ii])) { //移除公共边 T2[t2].Ts.Remove(SameSide); T2[ii].Ts.Remove(SameSide1); } ii += 1; } } //将剩下的边添加到S中 S.Clear(); for (int a1 = 0; a1 < T2.Count; a1++) { for (int a2 = 0; a2 < T2[a1].Ts.Count; a2++) { S.Add(T2[a1].Ts[a2]); } } //清空T2 T2.Clear(); //生成新的三角形并添加到T1中 for (int a3 = 0; a3 < S.Count; a3++) { side s0 = new side(); side s1 = new side(); side s2 = new side(); triangle t = new triangle(); //实例化 s0.Sp = new List <point>(); s1.Sp = new List <point>(); s2.Sp = new List <point>(); t.Tp = new List <point>(); t.Ts = new List <side>(); //三个顶点 t.Tp.Add(S[a3].Sp[0]); t.Tp.Add(S[a3].Sp[1]); t.Tp.Add(CP[i]); //三条边 s0 = S[a3]; s1.Sp.Add(S[a3].Sp[1]); s1.Sp.Add(CP[i]); s2.Sp.Add(S[a3].Sp[0]); s2.Sp.Add(CP[i]); t.Ts.Add(s0); t.Ts.Add(s1); t.Ts.Add(s2); T1.Add(t); } } } }