コード例 #1
0
ファイル: Algorithms.cs プロジェクト: ifalvarez/complices
        /**
         *      Returns a list containing lines connected to the given points. A line is a list of points.
         *      Only returns correct results for square or hex grids.
         *
         *      @param IsNeighborsConnected a functions that returns true or false, depending on whether
         *      two points can be considered connected when they are neighbors. For example, if you want
         *      rays of points that refer to cells of the same color, you can pass in a functions that
         *      compares the DefaultColors of cells.
         *
         *      @tparam TCell the type of cell of the grid that this algorithm takes.
         *      @tparam TPoint the type of point of the grid that this algorithm takes.
         *
         *      @code
         *      private bool IsSameColour(point1, point2)
         *      {
         *         return grid[point1].Color == grid[point2].Color;
         *      }
         *
         *      private SomeMethod()
         *      {
         *              ...
         *              var rays = GetConnectedRays<ColourCell, PointyHexPoint, PointyHexNeighborIndex>(
         *                      grid, point, IsSameColour);
         *              ...
         *      }
         *      @endcode
         *
         *      You can of course also use a lambda expression, like this:
         *
         *      @code
         *      //The following code returns all lines that radiate from the given point,
         *
         *      GetConnectedRays<ColourCell, PointyHexPoint, PointyHexNeighborIndex>(
         *              grid, point, (x, y) => grid[x].Color == grid[y].Color);
         *
         *      @endcode
         */
        public static IEnumerable <IEnumerable <TPoint> > GetConnectedRays <TCell, TPoint>(
            AbstractUniformGrid <TCell, TPoint> grid,
            TPoint point,
            Func <TPoint, TPoint, bool> isNeighborsConnected)

            where TPoint : IVectorPoint <TPoint>, IGridPoint <TPoint>
        {
            var lines = new List <IEnumerable <TPoint> >();

            foreach (var direction in grid.GetNeighborDirections())
            {
                var line = new PointList <TPoint>();

                var edge = point;

                while (grid.Contains(edge) && isNeighborsConnected(point, edge))
                {
                    line.Add(edge);
                    edge = edge.MoveBy(direction);
                }

                if (line.Count > 1)
                {
                    lines.Add(line);
                }
            }

            return(lines);
        }
コード例 #2
0
ファイル: Algorithms.cs プロジェクト: ifalvarez/complices
        /**
         *      Gets the longest of the rays connected to this cell.
         *
         *      @tparam TCell the type of cell of the grid that this algorithm takes.
         *      @tparam TPoint the type of point of the grid that this algorithm takes.
         *
         *      @see GetConnectedRays
         */
        public static IEnumerable <TPoint> GetLongestConnectedRay <TCell, TPoint>(
            AbstractUniformGrid <TCell, TPoint> grid,
            TPoint point,
            Func <TPoint, TPoint, bool> isNeighborsConnected)

            where TPoint : IVectorPoint <TPoint>, IGridPoint <TPoint>
        {
            var rays = GetConnectedRays(grid, point, isNeighborsConnected);

            return(GetBiggestShape(rays));
        }
コード例 #3
0
 public static IEnumerable <TPoint> GetNeighborDirections <TCell, TPoint>(this AbstractUniformGrid <TCell, TPoint> grid)
     where TPoint : IGridPoint <TPoint>, IVectorPoint <TPoint>
 {
     return(grid.NeighborDirections);
 }