public bool lineXpoly(Poly poly) // то же для произвольного полигона { if (poly.getSides().Any(side => intersects(side))) { return(true); } else { return(false); } }
// Рекурсивное разбиение public static Poly[] reduce(Poly poly) { if (poly.vert.Length > Constants.MAXV) { Poly[] reduced = new Poly[2]; int v0 = 0; int v1 = Constants.MAXV - 2; while (true) { do { if (poly.clearPath(v0, v1)) { goto found; } v0 = poly.next(v0); v1 = poly.next(v1); }while (v0 != 0); v1 = poly.prev(v1); } throw new Exception("Impossible to reduce a polygon"); found: int t0 = v0; int t1 = v1; v0 = Math.Min(t0, t1); v1 = Math.Max(t0, t1); reduced[0] = new Poly(); reduced[0].vert = poly.vert.Skip(v0).Take(v1 - v0 + 1).ToArray(); reduced[1] = new Poly(); reduced[1].vert = poly.vert.Take(v0 + 1).Concat(poly.vert.Skip(v1 - 1)).ToArray(); return(reduce(reduced[0]).Concat(reduce(reduced[1])).ToArray()); } else { return new[] { poly } }; }
public static void Work(string[] files) { foreach (string file in files) { // Загрузка карты Console.WriteLine(file); XElement root = XElement.Load(file); // Парсинг файла FeatureMember[] map = (from el in root.Elements(Constants.gml + "featureMember") select new FeatureMember(el.Descendants().First())).ToArray(); // Обработка //try //{ for (int MS = 0; MS < map.Length; MS++) { List <Poly> newMap = new List <Poly>(); foreach (OuterPoly OP in map[MS].pols) { OP.unite(); newMap.AddRange(Poly.reduce(OP)); } map[MS].pols = new List <Poly>(newMap); } // Проверка for (int MS = 0; MS < map.Length; MS++) { foreach (Poly poly in map[MS].pols) { LineD[] sides = poly.getSides(); for (int i = 0; i < sides.Length; i++) { for (int j = 0; j < sides.Length; j++) { if (Math.Abs(i - j) > 1 && sides[i].a != sides[j].a && sides[i].b != sides[j].b && sides[i].a != sides[j].b && sides[i].b != sides[j].a && sides[i].intersects(sides[j])) { throw new Exception("Intersection detected near " + sides[i].a.X.ToString() + " " + sides[i].a.Y.ToString() + " FID=" + MS.ToString()); } } } } } Console.WriteLine("Calculation successful. Saving result..."); //} //catch (Exception e) //{ // Console.WriteLine("An error occured: " + e.Message); //} // Вывод в XML root.Descendants(Constants.gml + "featureMember").Remove(); foreach (FeatureMember MS in map) { root.Add(new XElement(Constants.gml + "featureMember", MS.getXML())); } root.Save(file); } }