예제 #1
0
        //
        // Finds the top-most shapes and recursively constructs the contained relationships.
        //
        private void ShapeHierarchyHelper(Figure outer, List<Figure> inner)
        {
            //
            // Base Case: Have we already processed this 'outer' figure?
            //
            if (outer.HierarchyEstablished()) return;

            //
            // Base case: no contained shapes; this is a bottom-most shape (leaf-shape in hierarchy)
            //
            if (!inner.Any())
            {
                outer.MakeLeaf();
                return;
            }

            //
            // Find the current set of top-most shapes.
            //
            List<Figure> topShapes = new List<Figure>();
            for (int f1 = 0; f1 < inner.Count; f1++)
            {
                bool contained = false;
                for (int f2 = 0; f2 < inner.Count; f2++)
                {
                    if (f1 != f2)
                    {
                        if (inner[f2].Contains(inner[f1]))
                        {
                            contained = true;
                            break;
                        }
                    }
                }

                if (!contained) topShapes.Add(inner[f1]);
            }

            //
            // Construct the child hierarchies:
            //    for each top shape, find the contained shapes, recur.
            //
            foreach (Figure topShape in topShapes)
            {
                // Find contained shapes.
                List<Figure> containedShapes = new List<Figure>();
                foreach (Figure innerShape in inner)
                {
                    if (!topShape.StructurallyEquals(innerShape))
                    {
                        if (topShape.Contains(innerShape))
                        {
                            containedShapes.Add(innerShape);
                        }
                    }
                }

                // Recur
                ShapeHierarchyHelper(topShape, containedShapes);
            }

            outer.SetChildren(topShapes);
        }