//
        // Append subtraction to this current problem.
        //
        public static FigSynthProblem AppendAddition(FigSynthProblem that, FigSynthProblem toAppend)
        {
            BinarySynthOperation binaryAppend = toAppend as BinarySynthOperation;

            if (binaryAppend == null)
            {
                return(null);
            }

            if (that is AdditionSynth)
            {
                // Verify that the external form of that matches with the LHS of toAppend.
                Polygon testPoly = Polygon.MakePolygon(that.GetExteriorSegments());
                if (!testPoly.StructurallyEquals((binaryAppend.leftProblem as UnarySynth).figure))
                {
                    throw new ArgumentException("Exterior polygons do not match: " + (binaryAppend.leftProblem as UnarySynth).figure);
                }
            }
            // Create the new synth object
            AdditionSynth newSum = new AdditionSynth(that.Copy(), (binaryAppend.rightProblem as UnarySynth).figure);

            // The open regions that may be modified consist of a union of regions.
            List <AtomicRegion> newOpenRegions = new List <AtomicRegion>(that.openRegions);

            newOpenRegions.AddRange(toAppend.openRegions);

            newSum.SetOpenRegions(newOpenRegions);

            return(newSum);
        }
        //
        // Append subtraction to this current problem; the subtraction occurs within the inner figure (shape).
        //
        public static FigSynthProblem AppendFigureSubtraction(FigSynthProblem that, FigSynthProblem toAppend)
        {
            BinarySynthOperation binaryAppend = toAppend as BinarySynthOperation;

            if (binaryAppend == null)
            {
                return(null);
            }

            if (that is SubtractionSynth)
            {
                // Verify that the outer part of toAppend is a figure in this problem.
                Figure          theShape         = (binaryAppend.leftProblem as UnarySynth).figure;
                FigSynthProblem leftShapeProblem = that.GetSynthByShape(theShape);

                if (leftShapeProblem == null)
                {
                    throw new ArgumentException("Shape is not in the given problem: " + theShape);
                }

                //
                // Create the new subtraction node and insert it into the copy.
                //
                // Since the 'left' expression was a shape, the 'right' is the actual shape we are appending.
                SubtractionSynth newSub = new SubtractionSynth(leftShapeProblem, binaryAppend.rightProblem);

                return(that.Copy().ReplaceUnary(theShape, newSub));
            }
            else if (that is AdditionSynth)
            {
                // Verify that the external form of that matches with the LHS of toAppend.
                Polygon outerPoly = Polygon.MakePolygon(that.GetExteriorSegments());
                if (!outerPoly.StructurallyEquals((binaryAppend.leftProblem as UnarySynth).figure))
                {
                    throw new ArgumentException("Exterior polygons do not match: " + (binaryAppend.leftProblem as UnarySynth).figure);
                }

                // Make a copy of that.
                return(new SubtractionSynth(that.Copy(), (binaryAppend.rightProblem as UnarySynth).figure));
            }
            else
            {
                throw new ArgumentException("Expected Addition or Subtraction; acquired neither.");
            }
        }