Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ConsoleLib.Console;
- using System.Collections.Generic;
- namespace Genkit
- {
- public static class GridTools
- {
- public static void DrawIntGrid( int[,] Grid, Rect2D R, bool bWait = true )
- {
- // TODO:change this to not draw a dependency on Qud's display library
- ScreenBuffer SB = XRL.UI.Popup.ScrapBuffer;
- for (int x = 0; x <= Grid.GetUpperBound(0); x++)
- for (int y = 0; y <= Grid.GetUpperBound(1); y++)
- {
- SB.Goto(x, y);
- if( x >= R.x1 && x <= R.x2 && y >= R.y1 && y <= R.y2 )
- {
- SB.Write("#");
- }
- else
- {
- string hexValue = Grid[x, y].ToString("X");
- SB.Write(hexValue);
- }
- }
- XRL.Core.XRLCore._Console.DrawBuffer(SB);
- if( bWait ) Keyboard.getch();
- }
- public static void DrawIntGrid(int[,] Grid,bool bWait = true)
- {
- // TODO:change this to not draw a dependency on Qud's display library
- ScreenBuffer SB = XRL.UI.Popup.ScrapBuffer;
- SB.Clear();
- for (int x = 0; x <= Grid.GetUpperBound(0); x++)
- for (int y = 0; y <= Grid.GetUpperBound(1); y++)
- {
- SB.Goto(x, y);
- int g = Grid[x, y];
- string hexValue = Grid[x, y].ToString("X");
- if (g > 15)
- {
- g -= 16;
- hexValue = "&G" + Grid[x, y].ToString("X");
- }
- if (g > 15)
- {
- g -= 16;
- hexValue = "&W"+Grid[x, y].ToString("X");
- }
- if (g > 15)
- {
- g -= 16;
- hexValue = "&R" + Grid[x, y].ToString("X");
- }
- SB.Write(hexValue);
- }
- XRL.Core.XRLCore._Console.DrawBuffer(SB);
- if (bWait) Keyboard.getch();
- }
- public static Rect2D MaxRectSize(int[,] Grid)
- {
- Rect2D MaxRect = Rect2D.zero;
- DrawIntGrid(Grid);
- int[,] Histogram = new int[Grid.GetUpperBound(0)+1,Grid.GetUpperBound(1)+1];
- // Generate sub-histograms per row
- for( int x=0;x<=Grid.GetUpperBound(0);x++)
- {
- int nCount = 0;
- for ( int y=0;y<=Grid.GetUpperBound(1);y++ )
- {
- if( Grid[x,y] == 0 )
- {
- nCount++;
- }
- else
- {
- nCount = 0;
- }
- Histogram[x, y] = nCount;
- }
- }
- DrawIntGrid(Histogram);
- // Solve each histogram's rects using stack method http://stackoverflow.com/questions/4311694/maximize-the-rectangular-area-under-histogram
- Stack<int> ColumnStack = new Stack<int>();
- int[,] Area = new int[Grid.GetUpperBound(0) + 1, Grid.GetUpperBound(1) + 1];
- int[,] Left = new int[Grid.GetUpperBound(0) + 1, Grid.GetUpperBound(1) + 1];
- int[,] Right = new int[Grid.GetUpperBound(0) + 1, Grid.GetUpperBound(1) + 1];
- for (int y = 0; y <= Grid.GetUpperBound(1); y++)
- {
- int i, t;
- ColumnStack.Clear();
- for( i = 0;i<=Grid.GetUpperBound(0);i++)
- {
- while( ColumnStack.Count > 0 )
- {
- if(Histogram[i,y] <= Histogram[ColumnStack.Peek(),y] )
- {
- ColumnStack.Pop();
- }
- else
- {
- break;
- }
- }
- if( ColumnStack.Count == 0 )
- {
- t = -1;
- }
- else
- {
- t = ColumnStack.Peek();
- }
- Area[i,y] = i - t - 1;
- Left[i, y] = i - t - 1;
- ColumnStack.Push(i);
- }
- ColumnStack.Clear();
- for( i = Grid.GetUpperBound(0);i>=0;i--)
- {
- while (ColumnStack.Count > 0)
- {
- if (Histogram[i, y] <= Histogram[ColumnStack.Peek(), y])
- {
- ColumnStack.Pop();
- }
- else
- {
- break;
- }
- }
- if (ColumnStack.Count == 0)
- {
- t = Grid.GetUpperBound(0)+1;
- }
- else
- {
- t = ColumnStack.Peek();
- }
- Area[i,y] += t - i - 1;
- Right[i, y] = t - i - 1;
- ColumnStack.Push(i);
- }
- }
- DrawIntGrid(Area);
- int MaxArea = 0;
- //Calculating Area[i] and find max Area
- for (int y = 0; y <= Grid.GetUpperBound(1); y++)
- for (int i = 0; i <= Grid.GetUpperBound(0); i++)
- {
- int Width = Area[i, y] + 1;
- Area[i, y] = Histogram[i, y] * (Area[i, y] + 1);
- if( Area[i,y] > MaxArea )
- {
- MaxRect.x1 = i - Left[i,y];
- MaxRect.x2 = i + Right[i, y];
- MaxRect.y2 = y;
- MaxRect.y1 = y - (Histogram[i, y]) + 1;
- MaxArea = Area[i, y];
- }
- }
- DrawIntGrid(Area);
- DrawIntGrid(Area,MaxRect);
- return MaxRect;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement