protected override void DrawCellBackground(Graphics graphics, Cell cell, Distances distances) { if ((graphics == null) || (cell == null)) { return; } if (cell is OverCell) { base.DrawCellBackground(graphics, cell, distances); return; } // undercell var x1 = Convert.ToSingle(cell.Column * this.CellSize); var y1 = Convert.ToSingle(cell.Row * this.CellSize); var x2 = Convert.ToSingle(x1 + this.CellSize); var y2 = Convert.ToSingle(y1 + this.CellSize); var cellOffset = Convert.ToSingle(this.Inlet * this.CellSize); var x3 = x1 + cellOffset; var x4 = x2 - cellOffset; var y3 = y1 + cellOffset; var y4 = y2 - cellOffset; Brush brush = this.GetCellBrush(distances, cell); if ((cell as UnderCell).IsVerticalPassage()) { graphics.FillRectangle( brush, x3, y1, Convert.ToSingle(this.CellSize - (2 * cellOffset)), cellOffset); graphics.FillRectangle( brush, x3, y4, Convert.ToSingle(this.CellSize - (2 * cellOffset)), cellOffset); } else { graphics.FillRectangle( brush, x1, y3, cellOffset, Convert.ToSingle(this.CellSize - (2 * cellOffset))); graphics.FillRectangle( brush, x4, y3, cellOffset, Convert.ToSingle(this.CellSize - (2 * cellOffset))); } }
public Distances PathTo(Cell goal) { var current = goal; var breadcrumbs = new Distances(_root) { [current] = this[current] }; while (current != _root) { foreach (var neighbour in current.Links()) { if (this[neighbour] < this[current]) { if (!breadcrumbs.ContainsKey(neighbour)) { breadcrumbs.Add(neighbour, this[neighbour]); } else { breadcrumbs[neighbour] = this[neighbour]; } current = neighbour; break; } } } return(breadcrumbs); }
public override Bitmap MakeImage(Distances distances) { if (this.Grid == null) { return(null); } Bitmap bmp = new Bitmap( this.CalculateImageWidth(), this.CalculateImageHeight()); this.FillBackground(bmp); Graphics g = Graphics.FromImage(bmp); foreach (Cell cell in this.Grid.GetCells()) { if (cell != null) { this.DrawCellBackground(g, cell, distances); } } foreach (Cell cell in this.Grid.GetCells()) { if (cell != null) { this.DrawCellContour(g, cell); } } return(bmp); }
public Distances PathTo(Cell goal) { Cell current = goal; Distances breadcrumbs = new Distances(root); breadcrumbs[current] = this[current]; while (current != root) { foreach (var neighbor in current.Links()) { if (this[neighbor] < this[current]) { if (!breadcrumbs.ContainsKey(neighbor)) { breadcrumbs.Add(neighbor, this[neighbor]); } else { breadcrumbs[neighbor] = this[neighbor]; } current = neighbor; break; } } } return(breadcrumbs); }
public Distances Distances() { var distances = new Distances(this); var frontier = new List <Cell> { this }; while (frontier.Any()) { var newFrontier = new List <Cell>(); foreach (var cell in frontier) { foreach (var linked in cell.Links()) { if (distances.ContainsKey(linked)) { continue; } distances.Add(linked, distances[cell] + 1); newFrontier.Add(linked); } } frontier = newFrontier; } return(distances); }
public static Distances PathTo(Cell start, Cell goal) { Cell current = goal; Distances fullGrid = Build(start); Distances breadcrumbs = new Distances(start); breadcrumbs.Add(current, fullGrid.GetDistance(current)); do { foreach (Cell neighbour in current.Links) { if (fullGrid.GetDistance(neighbour) < fullGrid.GetDistance(current)) { breadcrumbs.Add(neighbour, fullGrid.GetDistance(neighbour)); current = neighbour; break; } } }while (current != start); return(breadcrumbs); }
private static void Test(IMazeBuilder builder, int rows, int columns, int samples) { if (builder == null) { return; } Console.WriteLine("{0}x{1}: {2}", rows, columns, builder); int sumPathLength = 0; int sumDeadEnds = 0; for (int i = 0; i < samples; i++) { var grid = Grid.CreateGrid(rows, columns); builder.Build(grid); Distances longestPath = Distances.LongestPath(grid); sumPathLength += longestPath.MaxDistance().Value; Distances deadEnds = Distances.DeadEnds(grid); sumDeadEnds += deadEnds.Count(); } Console.WriteLine("Longest Path: {0}", (double)sumPathLength / samples); Console.WriteLine("Nr Dead Ends: {0}", (double)sumDeadEnds / samples); }
public static Distances Build(Cell start) { Distances fullGrid = new Distances(start); fullGrid.BuildDistances(); return(fullGrid); }
public override string ContentsOf(Cell cell) { if (Distances != null && Distances.ContainsKey(cell)) { return(Base36.Encode(Distances[cell])); } return(" "); }
private void DrawCellBackGround(Graphics g, Cell cell, Distances distances) { var points = this.GetPoints(cell); Brush brush = this.GetCellBrush(distances, cell); g.FillPolygon(brush, points); Pen pen = this.GetCellPen(distances, cell); g.DrawPolygon(pen, points); }
public static Distances LongestPath(Grid grid) { if (grid == null) { return(null); } Distances distance = Build(grid.RandomCell()); Distances distanceBack = Build(distance.MaxDistance().Key); return(PathTo(distance.MaxDistance().Key, distanceBack.MaxDistance().Key)); }
static void ColoredGrid(int size) { Stopwatch stopwatch = new Stopwatch(); Random random = new Random(); int seed = random.Next(int.MinValue, int.MaxValue); ColoredGrid grid = new ColoredGrid(size, size, seed) { color = Color.Coral, IsIntensityOff = true }; algorithm = (Algorithm)random.Next(Enum.GetNames(typeof(Algorithm)).Length); stopwatch.Start(); ColoredGrid maze = (ColoredGrid)RandomAlgorithm <Grid, Cell>(grid, algorithm); Cell start = maze.GetCell(0, 0); //longest path in maze Distances distances = start.Distances(); var newStart = distances.Maximum(); var newDistances = newStart.Key.Distances(); maze.Start = newStart.Key; var goal = newDistances.Maximum(); maze.End = goal.Key; maze.distances = newDistances.PathTo(goal.Key); Bitmap img = maze.ToPNG(50); string name = "Maze.png"; img.Save(name); Process process = new Process(); process.StartInfo.FileName = name; process.Start(); process.Close(); stopwatch.Stop(); Console.WriteLine(algorithm); Console.WriteLine(stopwatch.ElapsedMilliseconds / (float)1000 + " seconds"); }
private static void BuildPolar() { Console.WriteLine("Polar"); var grid = PolarGrid.CreatePolarGrid(40); var mazeBuilder = new RecursiveBacktracker(); mazeBuilder.Build(grid); var distances = Distances.Build(grid.GetCell(0, 0)); var gd = GridDisplayFactory.GetDisplayForGrid(grid); var bitmap = gd.MakeImage(distances); bitmap.Save("Polar.png", ImageFormat.Png); Console.WriteLine("Done"); }
protected virtual void DrawCellBackground(Graphics graphics, Cell cell, Distances distances) { if ((graphics == null) || (cell == null)) { return; } double x1 = cell.Column * this.CellSize; double y1 = cell.Row * this.CellSize; Brush brush = this.GetCellBrush(distances, cell); graphics.FillRectangle( brush, Convert.ToSingle(x1), Convert.ToSingle(y1), Convert.ToSingle(this.CellSize), Convert.ToSingle(this.CellSize)); }
public static string ToText(Grid grid, Distances distances) { if (grid == null) { return(string.Empty); } string output = "+"; for (int i = 0; i < grid.Columns; i++) { output += "---+"; } output += "\n"; for (int r = 0; r < grid.Rows; r++) { string top = "|"; string bottom = "+"; for (int i = 0; i < grid.Columns; i++) { Cell cell = grid.GetCell(r, i); if (cell == null) { cell = new Cell(-1, -1); } string cellContent = " "; if ((distances != null) && distances.KnowsCell(cell)) { cellContent = " " + (char)(distances.GetDistance(cell) + 65) + " "; } top += cellContent + (cell.IsLinked(cell.East) ? " " : "|"); bottom += (cell.IsLinked(cell.South) ? " " : "---") + "+"; } output += top + "\n" + bottom + "\n"; } return(output); }
public override Bitmap MakeImage(Distances distances) { if (this.Grid == null) { return(null); } Bitmap bmp = new Bitmap( this.CalculateImageWidth(), this.CalculateImageHeight()); this.FillBackground(bmp); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; foreach (Cell cell in this.Grid.GetCells()) { if (cell != null) { this.DrawCellBackGround(g, cell, distances); } } foreach (Cell cell in this.Grid.GetCells()) { if (cell == null) { continue; } if ((cell.Row + cell.Column) % 2 == 0) { this.DrawBigCellContour(g, cell); } else { this.DrawSmallCellContour(g, cell); } } return(bmp); }
protected Color GetCellColor(Distances distances, Cell cell) { if ((this.GridStepInfo != null) && (this.GridStepInfo.CurrentCell != null)) { if (cell == this.GridStepInfo.CurrentCell) { return(this.StepCurrentCellColor); } if (this.GridStepInfo.Path.Contains(cell)) { return(this.StepPathColor); } if (this.GridStepInfo.CurrentCell.IsLinked(cell)) { return(this.StepCurrentCellNeighborColor); } } if (distances == null) { return(Color.FromArgb(255, 208, 208, 208)); } if (!distances.KnowsCell(cell)) { return(Color.FromArgb(255, 208, 208, 208)); } int dist = distances.GetDistance(cell); int maxdist = distances.MaxDistance().Value; double intensity = 1.0 * (maxdist - dist) / maxdist; byte r = (byte)((this.DistanceFromColor.R * intensity) + (this.DistanceToColor.R * (1 - intensity))); byte g = (byte)((this.DistanceFromColor.G * intensity) + (this.DistanceToColor.G * (1 - intensity))); byte b = (byte)((this.DistanceFromColor.B * intensity) + (this.DistanceToColor.B * (1 - intensity))); return(Color.FromArgb(255, r, g, b)); }
public static Distances DeadEnds(Grid grid) { if (grid == null) { return(null); } Distances distance = new Distances(null); foreach (Cell cell in grid.GetCells()) { if (cell == null) { continue; } if (cell.Links.Count == 1) { distance.Add(cell, 1); } } return(distance); }
protected Pen GetCellPen(Distances distances, Cell cell) { return(new Pen(this.GetCellColor(distances, cell))); }
protected override void DrawCellBackground(Graphics graphics, Cell cell, Distances distances) { if ((graphics == null) || (cell == null)) { return; } var x1 = Convert.ToSingle(cell.Column * this.CellSize); var y1 = Convert.ToSingle(cell.Row * this.CellSize); var x2 = Convert.ToSingle(x1 + this.CellSize); var y2 = Convert.ToSingle(y1 + this.CellSize); var cellOffset = Convert.ToSingle(this.Inlet * this.CellSize); var x3 = x1 + cellOffset; var x4 = x2 - cellOffset; var y3 = y1 + cellOffset; var y4 = y2 - cellOffset; Brush brush = this.GetCellBrush(distances, cell); graphics.FillRectangle( brush, x3, y3, Convert.ToSingle(this.CellSize - (2 * cellOffset)), Convert.ToSingle(this.CellSize - (2 * cellOffset))); if (cell.IsLinked(cell.North)) { graphics.FillRectangle( brush, x3, y1, Convert.ToSingle(this.CellSize - (2 * cellOffset)), cellOffset); } if (cell.IsLinked(cell.South)) { graphics.FillRectangle( brush, x3, y4, Convert.ToSingle(this.CellSize - (2 * cellOffset)), cellOffset); } if (cell.IsLinked(cell.West)) { graphics.FillRectangle( brush, x1, y3, cellOffset, Convert.ToSingle(this.CellSize - (2 * cellOffset))); } if (cell.IsLinked(cell.East)) { graphics.FillRectangle( brush, x4, y3, cellOffset, Convert.ToSingle(this.CellSize - (2 * cellOffset))); } }
public override Bitmap MakeImage(Distances distances) { if (this.Grid == null) { return(null); } Bitmap bmp = new Bitmap( this.CalculateImageWidth(), this.CalculateImageHeight()); this.FillBackground(bmp); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; var center = this.CalculateImageWidth() / 2.0; foreach (Cell cell in this.Grid.GetCells()) { if (cell == null) { continue; } Brush brush = this.GetCellBrush(distances, cell); if (cell.Row == 0) { g.FillEllipse( brush, Convert.ToSingle(center - this.CellSize), Convert.ToSingle(center - this.CellSize), Convert.ToSingle(2 * this.CellSize), Convert.ToSingle(2 * this.CellSize)); continue; } var theta = 2 * Math.PI / this.Grid.ColumnSize(cell.Row); var innerRadius = cell.Row * this.CellSize; var outerRadius = innerRadius + this.CellSize; var thetaCounterClockWise = cell.Column * theta; var thetaClockWise = thetaCounterClockWise + theta; var ax = Convert.ToSingle(center + innerRadius * Math.Cos(thetaCounterClockWise)); var ay = Convert.ToSingle(center + innerRadius * Math.Sin(thetaCounterClockWise)); var bx = Convert.ToSingle(center + outerRadius * Math.Cos(thetaCounterClockWise)); var by = Convert.ToSingle(center + outerRadius * Math.Sin(thetaCounterClockWise)); var cx = Convert.ToSingle(center + innerRadius * Math.Cos(thetaClockWise)); var cy = Convert.ToSingle(center + innerRadius * Math.Sin(thetaClockWise)); var dx = Convert.ToSingle(center + outerRadius * Math.Cos(thetaClockWise)); var dy = Convert.ToSingle(center + outerRadius * Math.Sin(thetaClockWise)); var path = new GraphicsPath(); path.StartFigure(); // Start the first figure. path.AddArc( Convert.ToSingle(center - innerRadius), Convert.ToSingle(center - innerRadius), Convert.ToSingle(2 * innerRadius), Convert.ToSingle(2 * innerRadius), Convert.ToSingle(thetaCounterClockWise * 180 / Math.PI), Convert.ToSingle(theta * 180 / Math.PI)); path.AddLine(cx, cy, dx, dy); path.AddArc( Convert.ToSingle(center - outerRadius), Convert.ToSingle(center - outerRadius), Convert.ToSingle(2 * outerRadius), Convert.ToSingle(2 * outerRadius), Convert.ToSingle(thetaClockWise * 180 / Math.PI), -Convert.ToSingle(theta * 180 / Math.PI)); path.AddLine(bx, by, ax, ay); path.CloseFigure(); g.FillPath(brush, path); Pen pen = this.GetCellPen(distances, cell); g.DrawPath(pen, path); } Pen wallPen = new Pen(this.WallColor); foreach (Cell cell in this.Grid.GetCells()) { if (cell == null) { continue; } if (cell.Row == 0) { continue; } var theta = 2 * Math.PI / this.Grid.ColumnSize(cell.Row); var innerRadius = cell.Row * this.CellSize; var outerRadius = innerRadius + this.CellSize; var thetaCounterClockWise = cell.Column * theta; var thetaClockWise = thetaCounterClockWise + theta; var cx = Convert.ToSingle(center + innerRadius * Math.Cos(thetaClockWise)); var cy = Convert.ToSingle(center + innerRadius * Math.Sin(thetaClockWise)); var dx = Convert.ToSingle(center + outerRadius * Math.Cos(thetaClockWise)); var dy = Convert.ToSingle(center + outerRadius * Math.Sin(thetaClockWise)); if (!cell.IsLinked((cell as PolarCell).Inward)) { g.DrawArc( wallPen, Convert.ToSingle(center - innerRadius), Convert.ToSingle(center - innerRadius), Convert.ToSingle(2 * innerRadius), Convert.ToSingle(2 * innerRadius), Convert.ToSingle(thetaCounterClockWise * 180 / Math.PI), Convert.ToSingle(theta * 180 / Math.PI)); } if (!cell.IsLinked((cell as PolarCell).Clockwise)) { g.DrawLine(wallPen, cx, cy, dx, dy); } } g.DrawArc(wallPen, 1, 1, this.CalculateImageWidth() - 1, this.CalculateImageWidth() - 1, 0, 360); return(bmp); }
public override Bitmap MakeImage(Distances distances) { if (this.Grid == null) { return(null); } var height = 0.5 * this.CellSize * Math.Sqrt(3.0); var width = this.CellSize; Bitmap bmp = new Bitmap( this.CalculateImageWidth(), this.CalculateImageHeight()); this.FillBackground(bmp); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; foreach (Cell cell in this.Grid.GetCells()) { if (cell == null) { continue; } var cx = 0.5 * width * (cell.Column + 1); var cy = 0.5 * height + cell.Row * height; var westX = cx - 0.5 * width; var midX = cx; var eastX = cx + 0.5 * width; var apexY = cy + 0.5 * height; var baseY = cy - 0.5 * height; if ((cell as TriangleCell).Upright()) { apexY = cy - 0.5 * height; baseY = cy + 0.5 * height; } Brush brush = this.GetCellBrush(distances, cell); PointF[] points = new PointF[] { new PointF { X = Convert.ToSingle(westX), Y = Convert.ToSingle(baseY) }, new PointF { X = Convert.ToSingle(midX), Y = Convert.ToSingle(apexY) }, new PointF { X = Convert.ToSingle(eastX), Y = Convert.ToSingle(baseY) } }; g.FillPolygon(brush, points); Pen pen = this.GetCellPen(distances, cell); g.DrawPolygon(pen, points); } foreach (Cell cell in this.Grid.GetCells()) { if (cell == null) { continue; } var cx = 0.5 * width * (cell.Column + 1); var cy = 0.5 * height + cell.Row * height; var westX = cx - 0.5 * width; var midX = cx; var eastX = cx + 0.5 * width; var apexY = cy + 0.5 * height; var baseY = cy - 0.5 * height; if ((cell as TriangleCell).Upright()) { apexY = cy - 0.5 * height; baseY = cy + 0.5 * height; } Pen wallPen = new Pen(this.WallColor); if (cell.West == null) { g.DrawLine( wallPen, Convert.ToSingle(westX), Convert.ToSingle(baseY), Convert.ToSingle(midX), Convert.ToSingle(apexY)); } if (!cell.IsLinked(cell.East)) { g.DrawLine( wallPen, Convert.ToSingle(eastX), Convert.ToSingle(baseY), Convert.ToSingle(midX), Convert.ToSingle(apexY)); } var noSouth = (cell as TriangleCell).Upright() && (cell.South == null); var notLinked = !(cell as TriangleCell).Upright() && !cell.IsLinked(cell.North); if (noSouth || notLinked) { g.DrawLine( wallPen, Convert.ToSingle(eastX), Convert.ToSingle(baseY), Convert.ToSingle(westX), Convert.ToSingle(baseY)); } } return(bmp); }
protected Brush GetCellBrush(Distances distances, Cell cell) { return(new SolidBrush(this.GetCellColor(distances, cell))); }
public override Bitmap MakeImage(Distances distances) { if (this.Grid == null) { return(null); } double aSize = this.CellSize / 2.0; double bSize = this.CellSize * Math.Sqrt(3) / 2.0; Bitmap bmp = new Bitmap( this.CalculateImageWidth(), this.CalculateImageHeight()); this.FillBackground(bmp); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; foreach (Cell cell in this.Grid.GetCells()) { if (cell == null) { continue; } var cx = this.CellSize + 3 * aSize * cell.Column; var cy = bSize + 2 * bSize * cell.Row; if (cell.Column % 2 != 0) { cy += bSize; } var x_fw = Convert.ToInt32(cx - this.CellSize); var x_nw = Convert.ToInt32(cx - aSize); var x_ne = Convert.ToInt32(cx + aSize); var x_fe = Convert.ToInt32(cx + this.CellSize); var y_n = Convert.ToInt32(cy - bSize); var y_m = Convert.ToInt32(cy); var y_s = Convert.ToInt32(cy + bSize); Brush brush = this.GetCellBrush(distances, cell); Point[] points = new Point[] { new Point { X = x_fw, Y = y_m }, new Point { X = x_nw, Y = y_n }, new Point { X = x_ne, Y = y_n }, new Point { X = x_fe, Y = y_m }, new Point { X = x_ne, Y = y_s }, new Point { X = x_nw, Y = y_s }, }; g.FillPolygon(brush, points); Pen pen = this.GetCellPen(distances, cell); g.DrawPolygon(pen, points); } foreach (Cell cell in this.Grid.GetCells()) { if (cell == null) { continue; } var cx = this.CellSize + 3 * aSize * cell.Column; var cy = bSize + 2 * bSize * cell.Row; if (cell.Column % 2 != 0) { cy += bSize; } var x_fw = Convert.ToInt32(cx - this.CellSize); var x_nw = Convert.ToInt32(cx - aSize); var x_ne = Convert.ToInt32(cx + aSize); var x_fe = Convert.ToInt32(cx + this.CellSize); var y_n = Convert.ToInt32(cy - bSize); var y_m = Convert.ToInt32(cy); var y_s = Convert.ToInt32(cy + bSize); var hexCell = cell as HexCell; Pen wallPen = new Pen(this.WallColor); if (hexCell.Southwest == null) { g.DrawLine(wallPen, x_fw, y_m, x_nw, y_s); } if (hexCell.Northwest == null) { g.DrawLine(wallPen, x_fw, y_m, x_nw, y_n); } if (hexCell.North == null) { g.DrawLine(wallPen, x_nw, y_n, x_ne, y_n); } if (!hexCell.IsLinked(hexCell.Northeast)) { g.DrawLine(wallPen, x_ne, y_n, x_fe, y_m); } if (!hexCell.IsLinked(hexCell.Southeast)) { g.DrawLine(wallPen, x_fe, y_m, x_ne, y_s); } if (!hexCell.IsLinked(hexCell.South)) { g.DrawLine(wallPen, x_ne, y_s, x_nw, y_s); } } return(bmp); }
public virtual Bitmap MakeImage(Distances distances) { return(null); }