private void Button_Click(object sender, RoutedEventArgs e) { MainCanvas.Children.Clear(); var cells = new Dictionary <FortuneVoronoi.Common.Point, VoronoiCell>(); const int internalSitesCnt = 1600; const int horBorderSitesCnt = 50; const int vertBorderSitesCnt = 50; const int resolution = 16; const double realWidth = 1000.0; const double realHeight = 700.0; var dx = realWidth / horBorderSitesCnt / resolution; var dy = realHeight / vertBorderSitesCnt / resolution; var borderSites = SitesGridGenerator.GenerateTileBorder(horBorderSitesCnt, vertBorderSitesCnt, resolution); var internalSites = SitesGridGenerator.GenerateInternalSites(horBorderSitesCnt, vertBorderSitesCnt, resolution, internalSitesCnt); foreach (var site in borderSites.Concat(internalSites)) { var x = site.X * dx; var y = site.Y * dy; var v = new FortuneVoronoi.Common.Point(x, y); if (cells.ContainsKey(v)) { continue; } cells.Add(v, new VoronoiCell { IsVisible = !site.IsBorder, Site = new FortuneVoronoi.Common.Point(x, y) }); } var graph = Fortune.ComputeVoronoiGraph(cells); foreach (var cell in cells.Values.Where(c => c.IsVisible && c.IsClosed)) { var rgbFill = new byte[3]; _rnd.NextBytes(rgbFill); var fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(rgbFill[0], rgbFill[1], rgbFill[2])); var triangles = cell.CreateTriangles(); foreach (var triangle in triangles) { triangle.Fill = fill; Canvas.SetLeft(triangle, MainCanvas.ActualWidth * 0.5); Canvas.SetTop(triangle, MainCanvas.ActualHeight * 0.5); MainCanvas.Children.Add(triangle); } } }
private void Button_Click(object sender, RoutedEventArgs e) { MainCanvas.Children.Clear(); var cells = new Dictionary<FortuneVoronoi.Common.Point, VoronoiCell>(); const int internalSitesCnt = 1600; const int horBorderSitesCnt = 50; const int vertBorderSitesCnt = 50; const int resolution = 16; const double realWidth = 1000.0; const double realHeight = 700.0; var dx = realWidth/horBorderSitesCnt/resolution; var dy = realHeight/vertBorderSitesCnt/resolution; var borderSites = SitesGridGenerator.GenerateTileBorder(horBorderSitesCnt, vertBorderSitesCnt, resolution); var internalSites = SitesGridGenerator.GenerateInternalSites(horBorderSitesCnt, vertBorderSitesCnt, resolution, internalSitesCnt); foreach (var site in borderSites.Concat(internalSites)) { var x = site.X*dx; var y = site.Y*dy; var v = new FortuneVoronoi.Common.Point(x, y); if (cells.ContainsKey(v)) { continue; } cells.Add(v, new VoronoiCell{ IsVisible = !site.IsBorder, Site = new FortuneVoronoi.Common.Point(x, y)}); } var graph = Fortune.ComputeVoronoiGraph(cells); foreach (var cell in cells.Values.Where(c => c.IsVisible && c.IsClosed)) { var rgbFill = new byte[3]; _rnd.NextBytes(rgbFill); var fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(rgbFill[0], rgbFill[1], rgbFill[2])); var triangles = cell.CreateTriangles(); foreach (var triangle in triangles) { triangle.Fill = fill; Canvas.SetLeft(triangle, MainCanvas.ActualWidth * 0.5); Canvas.SetTop(triangle, MainCanvas.ActualHeight * 0.5); MainCanvas.Children.Add(triangle); } } }
public VoronoiCell[] GenerateTileCells(int internalCellsCount) { var mapWidth = TilesSize.x; var mapHeight = TilesSize.y; var cells = new Dictionary <FortuneVoronoi.Common.Point, VoronoiCell>(); var dx = mapWidth / HorBorderSitesCnt / Resolution; var dy = mapHeight / VertBorderSitesCnt / Resolution; var internalSites = SitesGridGenerator.GenerateInternalSites(HorBorderSitesCnt, VertBorderSitesCnt, Resolution, internalCellsCount, (min, max) => new IntPoint(Random.Range(min, max), Random.Range(min, max))); if (!CachedBorders.ContainsKey(TilesSize)) { var borderSites = SitesGridGenerator.GenerateTileBorder(HorBorderSitesCnt, VertBorderSitesCnt, Resolution, (min, max) => new IntPoint(Random.Range(min, max), Random.Range(min, max))); // (int)((min + max) * 0.5f) CachedBorders.Add(TilesSize, borderSites); } foreach (var site in internalSites.Concat(CachedBorders[TilesSize]).Distinct()) { var x = site.X * dx; var y = site.Y * dy; var v = new FortuneVoronoi.Common.Point(x, y); cells.Add(v, new VoronoiCell { IsVisible = !site.IsBorder, Site = v }); } var graph = Fortune.ComputeVoronoiGraph(cells); return(cells.Values.Where(c => c.IsVisible && c.IsClosed).ToArray()); }
public VoronoiCell[] GenerateTileCells(int internalCellsCount) { var mapWidth = TilesSize.x; var mapHeight = TilesSize.y; var cells = new Dictionary<FortuneVoronoi.Common.Point, VoronoiCell>(); var dx = mapWidth / HorBorderSitesCnt / Resolution; var dy = mapHeight / VertBorderSitesCnt / Resolution; var internalSites = SitesGridGenerator.GenerateInternalSites(HorBorderSitesCnt, VertBorderSitesCnt, Resolution, internalCellsCount, (min, max) => new IntPoint(Random.Range(min,max), Random.Range(min,max))); if (!CachedBorders.ContainsKey(TilesSize)) { var borderSites = SitesGridGenerator.GenerateTileBorder(HorBorderSitesCnt, VertBorderSitesCnt, Resolution, (min, max) => new IntPoint(Random.Range(min, max), Random.Range(min, max))); // (int)((min + max) * 0.5f) CachedBorders.Add(TilesSize, borderSites); } foreach (var site in internalSites.Concat(CachedBorders[TilesSize]).Distinct()) { var x = site.X * dx; var y = site.Y * dy; var v = new FortuneVoronoi.Common.Point(x,y); cells.Add(v, new VoronoiCell{IsVisible = !site.IsBorder, Site = v}); } var graph = Fortune.ComputeVoronoiGraph(cells); return cells.Values.Where(c => c.IsVisible && c.IsClosed).ToArray(); }