public static void CalculateBresenhamProductsToRectangle(int fromX, int
                                                                 fromY, int[][] map, MapRectangle rectangle, UpdateProductForSquare
                                                                 update, int startingProduct, bool includeFirstSquare, bool
                                                                 includeLastSquare, int[][] outputMap)
        {
            // top and bottom edges
            for (int x = rectangle.x; x <= rectangle.x2; x++)
            {
                outputMap[fromX][fromY] = startingProduct;
                CalculateBresenhamProductSquareToSquare(fromX, fromY,
                                                        x, rectangle.y, map,
                                                        (previous, mapval, xs, ys) => {
                    outputMap[xs][ys] = update(previous, mapval);
                    return(outputMap[xs][ys]);
                }, startingProduct, includeFirstSquare, includeLastSquare);
                outputMap[fromX][fromY] = startingProduct;
                CalculateBresenhamProductSquareToSquare(fromX, fromY,
                                                        x, rectangle.y2, map,
                                                        (previous, mapval, xs, ys) => {
                    outputMap[xs][ys] = update(previous, mapval);
                    return(outputMap[xs][ys]);
                }, startingProduct, includeFirstSquare, includeLastSquare);
            }

            // right and left edges
            for (int y = rectangle.y + 1; y <= rectangle.y2 - 1; y++)
            {
                outputMap[fromX][fromY] = startingProduct;
                CalculateBresenhamProductSquareToSquare(fromX, fromY,
                                                        rectangle.x, y, map,
                                                        (previous, mapval, xs, ys) => {
                    outputMap[xs][ys] = update(previous, mapval);
                    return(outputMap[xs][ys]);
                }, startingProduct, includeFirstSquare, includeLastSquare);
                outputMap[fromX][fromY] = startingProduct;
                CalculateBresenhamProductSquareToSquare(fromX, fromY,
                                                        rectangle.x2, y, map,
                                                        (previous, mapval, xs, ys) => {
                    outputMap[xs][ys] = update(previous, mapval);
                    return(outputMap[xs][ys]);
                }, startingProduct, includeFirstSquare, includeLastSquare);
            }
        }
        public static int CalculateBresenhamProductSquareToSquare(int x1,
                                                                  int y1, int x2, int y2, int[][] map, UpdateProductForSquare update,
                                                                  int startingProduct, bool includeFirstSquare, bool includeLastSquare)
        {
            x1 = MathUtils.Limit(x1, 0, map.Length - 1);
            y1 = MathUtils.Limit(y1, 0, map[0].Length - 1);
            x2 = MathUtils.Limit(x2, 0, map.Length - 1);
            y2 = MathUtils.Limit(y2, 0, map[0].Length - 1);
            Path path = GetBresenhamPath(x1, y1, x2, y2, null);
            int  product = startingProduct;
            int  x = x1, y = y1;

            for (int i = 0; i < path.NSteps; i++)
            {
                if (i != 0 || includeFirstSquare)
                {
                    product = update(product, map[x][y]);
                }
                x += StepDX[(int)path.Steps[i]];
                y += StepDY[(int)path.Steps[i]];
            }
            if (includeLastSquare)
            {
                product = update(product, map[x][y]);
            }
            return(product);
        }
 public static void CalculateBresenhamProductsToRectangle(int fromX, int
                                                          fromY, int[][] map, MapRectangle rectangle, UpdateProductForSquare
                                                          update, int startingProduct, int[][] outputMap)
 {
     CalculateBresenhamProductsToRectangle(fromX, fromY, map,
                                           rectangle, update, startingProduct, false, true, outputMap);
 }
 public static int CalculateBresenhamProductSquareToSquare(int x1,
                                                           int y1, int x2, int y2, int[][] map, UpdateProductForSquare update,
                                                           int startingProduct)
 {
     return(CalculateBresenhamProductSquareToSquare(x1, y1, x2, y2, map,
                                                    update, startingProduct, false, true));
 }
        public static void CalculateBresenhamProductsToRectangle(int fromX, int
           fromY, int[][] map, MapRectangle rectangle, UpdateProductForSquare
           update, int startingProduct, bool includeFirstSquare, bool
           includeLastSquare, int[][] outputMap)
        {
            // top and bottom edges
            for (int x = rectangle.x; x <= rectangle.x2; x++)
            {
                outputMap[fromX][fromY] = startingProduct;
                CalculateBresenhamProductSquareToSquare(fromX, fromY,
                   x, rectangle.y, map,
                   (previous, mapval, xs, ys) => {
                      outputMap[xs][ys] = update(previous, mapval);
                      return outputMap[xs][ys];
                   }, startingProduct, includeFirstSquare, includeLastSquare);
                outputMap[fromX][fromY] = startingProduct;
                CalculateBresenhamProductSquareToSquare(fromX, fromY,
                   x, rectangle.y2, map,
                   (previous, mapval, xs, ys) => {
                      outputMap[xs][ys] = update(previous, mapval);
                      return outputMap[xs][ys];
                   }, startingProduct, includeFirstSquare, includeLastSquare);
            }

            // right and left edges
            for (int y = rectangle.y + 1; y <= rectangle.y2 - 1; y++)
            {
                outputMap[fromX][fromY] = startingProduct;
                CalculateBresenhamProductSquareToSquare(fromX, fromY,
                   rectangle.x, y, map,
                   (previous, mapval, xs, ys) => {
                      outputMap[xs][ys] = update(previous, mapval);
                      return outputMap[xs][ys];
                   }, startingProduct, includeFirstSquare, includeLastSquare);
                outputMap[fromX][fromY] = startingProduct;
                CalculateBresenhamProductSquareToSquare(fromX, fromY,
                   rectangle.x2, y, map,
                   (previous, mapval, xs, ys) => {
                      outputMap[xs][ys] = update(previous, mapval);
                      return outputMap[xs][ys];
                   }, startingProduct, includeFirstSquare, includeLastSquare);
            }
        }
 public static void CalculateBresenhamProductsToRectangle(int fromX, int
    fromY, int[][] map, MapRectangle rectangle, UpdateProductForSquare
    update, int startingProduct, int[][] outputMap)
 {
     CalculateBresenhamProductsToRectangle(fromX, fromY, map,
        rectangle, update, startingProduct, false, true, outputMap);
 }
 public static int CalculateBresenhamProductSquareToSquare(int x1,
    int y1, int x2, int y2, int[][] map, UpdateProductForSquare update,
    int startingProduct, bool includeFirstSquare, bool includeLastSquare)
 {
     x1 = MathUtils.Limit(x1, 0, map.Length - 1);
     y1 = MathUtils.Limit(y1, 0, map[0].Length - 1);
     x2 = MathUtils.Limit(x2, 0, map.Length - 1);
     y2 = MathUtils.Limit(y2, 0, map[0].Length - 1);
     Path path = GetBresenhamPath(x1, y1, x2, y2, null);
     int product = startingProduct;
     int x = x1, y = y1;
     for (int i = 0; i < path.NSteps; i++)
     {
         if (i != 0 || includeFirstSquare)
         {
             product = update(product, map[x][y]);
         }
         x += StepDX[(int)path.Steps[i]];
         y += StepDY[(int)path.Steps[i]];
     }
     if (includeLastSquare)
     {
         product = update(product, map[x][y]);
     }
     return product;
 }
 public static int CalculateBresenhamProductSquareToSquare(int x1,
    int y1, int x2, int y2, int[][] map, UpdateProductForSquare update,
    int startingProduct)
 {
     return CalculateBresenhamProductSquareToSquare(x1, y1, x2, y2, map,
        update, startingProduct, false, true);
 }