Exemplo n.º 1
0
    // ***************************************************************************
    //  PRIVATE METHODS
    // ***************************************************************************
    private void assembleBorders()
    {
        borders = new List <Border>();
        // trigger to help build continuous borders
        bool valid = false;

        // create inline method to simplify the different scan direction below
        void scan(IEnumerable <int> xLocations, IEnumerable <int> yLocations, DIRECTION direction)
        {
            var border = new Border(this, direction);

            foreach (var x in xLocations)
            {
                foreach (var y in yLocations)
                {
                    if (g[x, y] < 1)
                    {
                        border.AddLocation(new Location(x, y) + corner);
                        valid = true;
                    }
                    else
                    {
                        // sharp break in discomfort, unpassable location, close off this border
                        if (valid)
                        {
                            borders.Add(border);
                            border = new Border(this, direction);
                        }
                        valid = false;
                    }
                }
            }
            // after the edge was scanned, see if we add this final border up to the corner
            if (valid)
            {
                Borders.Add(border);
            }
        }

        // scan the bottom (y=0, SOUTH)
        scan(Enumerable.Range(0, g.GetLength(0)), Enumerable.Range(0, 1), DIRECTION.SOUTH);
        // scan the top (y=length, NORTH)
        scan(Enumerable.Range(0, g.GetLength(0)), Enumerable.Range(g.GetLength(1) - 1, 1), DIRECTION.NORTH);
        // scan the left (x=0, WEST)
        scan(Enumerable.Range(0, 1), Enumerable.Range(0, g.GetLength(1)), DIRECTION.WEST);
        // scan the right (x=length, EAST)
        scan(Enumerable.Range(g.GetLength(0) - 1, 1), Enumerable.Range(0, g.GetLength(1)), DIRECTION.EAST);
    }