/// <summary>
        /// Converts PosXY to a a 1 based position value to a 
        /// e.g PosX=1, PosY=1 -> 1
        ///     PosX=2, PosY=1 -> 2
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="layoutDimensions"></param>
        /// <returns></returns>
        public static int ConvertPosXYToPosition(PosXY posXY, LayoutDimensions layoutDimensions)
        {
            Debug.Assert(posXY.X > 0);
            Debug.Assert(posXY.X <= layoutDimensions.Width);
            Debug.Assert(posXY.Y > 0);
            Debug.Assert(posXY.Y <= layoutDimensions.Height);

            int result = ((posXY.Y - 1) * layoutDimensions.Width) + posXY.X;

            Debug.Assert(result > 0);
            Debug.Assert(result <= layoutDimensions.NumPositions);

            return result;
        }
 private static void FillGridRow(FillSettings fillSettings, LayoutDimensions layoutDimensions, List<int> list, PosXY topLeft, int gridWidth, int gridHeight)
 {
     for (int y = 0; y < gridHeight; y++)
     {
         for (int x = 0; x < gridWidth; x++)
         {
             AddPositionToList(layoutDimensions, list, topLeft, x, y);
         }
         if (fillSettings.IsSnake && y < (gridHeight - 1))
         {
             // In snake mode do the next line in reverse
             y++;
             for (int x = gridWidth - 1; x >= 0 && x != int.MaxValue; x--)
             {
                 AddPositionToList(layoutDimensions, list, topLeft, x, y);
             }
         }
     }
 }
        private static int GetPreviousPosition(int currentPos, FillSettings fillSettings, LayoutDimensions layoutDimensions, out bool endOfLine)
        {
            Debug.Assert(fillSettings.IsGrid == false);
            Debug.Assert(currentPos >= 1);
            Debug.Assert(currentPos <= layoutDimensions.NumPositions);

            PosXY currentPosXY = ConvertPositionToPosXY(currentPos, layoutDimensions);
            PosXY nextPosXY = new PosXY();

            endOfLine = false;

            if (fillSettings.GroupDirection == Direction.Row)
            {
                if (currentPosXY.X > 1)
                {
                    nextPosXY.X = currentPosXY.X - 1;
                    nextPosXY.Y = currentPosXY.Y;
                }
                else
                {
                    nextPosXY.X = layoutDimensions.Width;
                    nextPosXY.Y = currentPosXY.Y - 1;
                    endOfLine = true;
                }
            }
            else
            {
                Debug.Assert(fillSettings.GroupDirection == Direction.Col);
                if (currentPosXY.Y > 1)
                {
                    nextPosXY.X = currentPosXY.X;
                    nextPosXY.Y = currentPosXY.Y - 1;
                }
                else
                {
                    nextPosXY.X = currentPosXY.X - 1;
                    nextPosXY.Y = layoutDimensions.Height;
                    endOfLine = true;
                }
            }

            int nextPos = ConvertPosXYToPosition(nextPosXY, layoutDimensions);
            Debug.Assert(nextPos != currentPos);
            Debug.Assert(nextPos >= 1);
            Debug.Assert(nextPos <= layoutDimensions.NumPositions);

            return nextPos;
        }
 private static void AddPositionToList(LayoutDimensions layoutDimensions, List<int> list, PosXY topLeft, int x, int y)
 {
     list.Add(ConvertPosXYToPosition(new PosXY(topLeft.X + x, topLeft.Y + y), layoutDimensions));
 }