static PolygonSet CreateSetFromList(IList <Polygon> source) { // First we need to reorganize the polygons var root = new PolygonHierachy(source[0]); for (var i = 1; i < source.Count; ++i) { ProcessLevel(source[i], ref root); } // Generate the set from the hierachy var set = new PolygonSet(); ProcessSetLevel(set, root); return(set); }
static void ProcessSetLevel(PolygonSet set, PolygonHierachy current) { while (current != null) { var poly = current.Current; foreach (var child in current.Childs) { poly.AddHole(child.Current); foreach (var grandchild in child.Childs) { ProcessSetLevel(set, grandchild); } } set.Add(poly); current = current.Next; } }
static void ProcessLevel(Polygon poly, ref PolygonHierachy localRoot) { if (localRoot == null) { localRoot = new PolygonHierachy(poly); return; } // Check if source is the new root if (CheckIfInside(localRoot.Current.Points, poly.Points)) { var nroot = new PolygonHierachy(poly); var tmp = localRoot; while (tmp != null) { var cur = tmp; tmp = tmp.Next; cur.Next = null; nroot.Childs.Add(cur); } localRoot = nroot; return; } // Check if source is not in the local root if (!CheckIfInside(poly.Points, localRoot.Current.Points)) { ProcessLevel(poly, ref localRoot.Next); return; } // Now process the childs for (var i = 0; i < localRoot.Childs.Count; ++i) { if (!CheckIfInside(poly.Points, localRoot.Childs[i].Current.Points)) { continue; } // Process to the child level var childRoot = localRoot.Childs[i]; ProcessLevel(poly, ref childRoot); localRoot.Childs[i] = childRoot; return; } // Else -> new child var newChildList = new List <PolygonHierachy>(); var newPoly = new PolygonHierachy(poly); newChildList.Add(newPoly); for (var i = 0; i < localRoot.Childs.Count; ++i) { if (CheckIfInside(localRoot.Childs[i].Current.Points, poly.Points)) { newPoly.Childs.Add(localRoot.Childs[i]); } else { newChildList.Add(localRoot.Childs[i]); } } localRoot.Childs = newChildList; //.Childs.Add(new PolygonHierachy(poly)); }
public PolygonHierachy(Polygon current) { Current = current; Childs = new List <PolygonHierachy>(); Next = null; }