public List<District> CreateCityDistricts(CitySettings settings,VoronoiDiagram voronoi) { _citySettings = settings; //create districts cells from the voronoi cells _districtCells = GenerateDistrictCells(voronoi); //create districts from the corresponding district cells return settings.DistrictSettings.Select(CreateDistrict).ToList(); }
public static CityData GenerateCity(CitySettings settings, VoronoiDiagram voronoi) { if (voronoi.VoronoiCells.Count < 1) return null; //Create helpers if none are created. if (_districtBuilder == null) { _districtBuilder = new DistrictBuilder(); } //Generate the city var cityData = new CityData(); voronoi.RefreshVoronoi(); //divide the city into districts cityData.Districts = _districtBuilder.CreateCityDistricts(settings,voronoi); cityData.Bounds = MathHelpers.GetCityBounds(cityData); return cityData; }
/// <summary> /// Generate a Voronoi Diagram from the generated points /// </summary> private void GenerateVoronoi() { //check if there are enough points if (_points == null || _points.Count <= 2 || _voronoiDiagram.Triangulation == null) { Console.WriteLine("No points available!"); GeneratePoints(PointsToGenerate); //return; } _baseColor = Extensions.Extensions.RandomColor(); _voronoiSettings.VoronoiAlgorithm = VoronoiAlgorithm; //create voronoi using specified algorithm var timer = Stopwatch.StartNew(); _voronoiDiagram = VoronoiGenerator.CreateVoronoi(_points, _voronoiSettings); timer.Stop(); if (_voronoiDiagram == null) return; //update info var time = timer.ElapsedMilliseconds/1000.0; GenerationTimeText = $"For {_points.Count} points using {VoronoiAlgorithm.ToString()}: {time} seconds."; //Update canvas RefreshCanvas(); }
/// <summary> /// Initialize this viewModel /// </summary> public void Initialize(DrawService drawService) { //initialize draw service _drawService = drawService; //_drawService.OnClick += OnClick; //create empty voronoi Diagram _points = new List<Point>(); _voronoiDiagram = new VoronoiDiagram(); _cityData = new CityData(); _citySettings = new CitySettings(); //seed for random generation Seed = DateTime.Now.GetHashCode(); //store default settings foreach (var districtType in _districtTypes) { DistrictSettings.Add(new DistrictSettings(districtType)); break; } RaisePropertyChanged("DistrictSettings"); //debug for math test and drawing MathTesting(); }
private List<DistrictCell> GenerateDistrictCells(VoronoiDiagram voronoi) { //Create a district cell from the voronoi cells var districtCells = new List<DistrictCell>(); foreach (var cell in voronoi.GetCellsInBounds()) { //ignore cells that are not valid if (cell.Edges.Count < 2) { continue; } districtCells.Add(DistrictCell.FromCell(cell,_citySettings.DistrictSettings[0].Type)); } //tag random cells based on the settings for each district foreach (var setting in _citySettings.DistrictSettings) { for (int i = 0; i < setting.Frequency; ++i) { //Get a random start cell from the voronoi var startCell = districtCells.GetRandomValue(); //size is a ratio of the width and length of the plane var size = setting.Size * ((voronoi.Bounds.Right + voronoi.Bounds.Bottom)/8); //tag cell districtCells.TagCells(startCell, size, setting.Type); } } return districtCells; }