public static void Main(string[] args) { var width = 45; var height = 30; var grid = new GridFactory().Create(width, height); var manipulator = new GridManipulator(grid, new Random()); // manipulator.CutLine(new Line2D(new Point2D(0, 0), new Point2D(20, 40))); Renderer renderer = null;//new Renderer(grid);//.RenderGrid(grid); var spiral = new SpiralParametricFunction(1, 9, 3, width / 2.0f, height / 2.0f, 0); manipulator.CutParametric(spiral.TInitial, spiral.TFinal, 20f, spiral.PointAt, renderer); var lastSpiralPoint = spiral.PointAt(spiral.TFinal - 30); var v = new Vector2D(lastSpiralPoint, new Point2D(width / 2.0f, height / 2.0f)); v = v.ToUnitVector(); var cutDestination = lastSpiralPoint + v * 3; manipulator.CutLine(new Line2D(lastSpiralPoint, cutDestination), renderer); Application.DoEvents(); var entranceCell = grid.Cells[(height / 2) * grid.Width + (width / 2)]; var cells = manipulator.FillRegion(entranceCell, renderer); renderer = new Renderer(grid);//.RenderGrid(grid); manipulator.PlaceLocksAndKeys(entranceCell, cells, new[] { Color.Red, Color.Blue, Color.DarkGreen, Color.DeepPink, Color.Magenta, Color.Cyan, Color.Yellow, Color.Gold, Color.Black, Color.DarkOrange, Color.Gray, Color.Aquamarine, Color.AliceBlue, Color.Azure, Color.DodgerBlue, Color.Indigo, Color.DeepSkyBlue, Color.PaleGreen, Color.PaleGoldenrod, Color.PaleTurquoise}); Console.WriteLine("Cell Count: " + cells.Count); entranceCell.Type = CellType.Entrance; while (true) { renderer.RenderGrid(grid); Application.DoEvents(); } }
public void CutParametric(float tInitial, float tFinal, float tStep, Func<double, Point2D> pointAt, Renderer renderer = null) { var steps = (int)Math.Ceiling((tFinal - tInitial) / tStep); var points = Util.Generate(steps, step => pointAt(tInitial + step * tStep)); for (var i = 0; i < points.Length - 1; i++) { CutLine(new Line2D(points[i], points[i + 1]), renderer); } }
public List<Cell> FillRegion(Cell initialSeed, Renderer renderer = null) { var seeds = new List<Cell>(); seeds.Add(initialSeed); var cells = new List<Cell>(); // includes all seeds - any room which has a clearly defined entrance cells.Add(initialSeed); while (seeds.Any()) { var index = (int)(seeds.Count * Math.Pow(random.NextDouble(), 0.25)); var seed = seeds.ElementAt(index); //GetRandomIntegerWeightedTowardEnd(seeds.Count, random)); var candidates = seed.Connectors.Where((c) => c.State == ConnectorState.Unlinked && !cells.Contains(c.Other(seed))).ToArray(); if (!candidates.Any()) { seeds.Remove(seed); continue; } CellConnector connector; if (candidates.Count() == 1) { seeds.Remove(seed); connector = candidates.First(); } else { connector = candidates.SelectRandom(); } connector.Connect(); var cell = connector.Other(seed); seeds.Add(cell); cells.Add(cell); renderer?.RenderGrid(grid); } return cells; }
public void CutLine(Line2D line, Renderer renderer = null) { foreach (var connector in grid.Cells.SelectMany(x => x.Connectors).Distinct()) { var intersection = connector.Segment.FindLineIntersection(line); if (intersection == line) { // wrong connector.connectorState = ConnectorState.Banned; } else if (intersection is Point2D) { var t1 = connector.Segment.NearestT((Point2D)intersection); var t2 = line.NearestT((Point2D)intersection); if (t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1) { connector.connectorState = ConnectorState.Banned; } } } renderer?.RenderGrid(grid); }