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); }
public void VoronoiFactory_MakeVoronoiSFTest_TwoPolygon2_Passes() { //Arrange List <Vector2> Seeds = new List <Vector2>(); Seeds.Add(new Vector2(28.7f, 8.47f)); Seeds.Add(new Vector2(38.3f, 16.12f)); Seeds.Add(new Vector2(30.56f, 10.78f)); Seeds.Add(new Vector2(29f, 5.2f)); Seeds.Add(new Vector2(25.2f, 16.16f)); Vector2 Boundry = new Vector2(15f, 15f); Vector2[] expectedVerticies = { new Vector2(31.754f, 17.329f), new Vector2(26.528f, 12.123f), new Vector2(32.661f, 7.185f), new Vector2(11.390f, 5.233f), new Vector2(40.208f, 5.075f) }; //Act VoronoiDiagram actual = VorannoyedFactory.MakeVoronoiSF(Seeds, Boundry); //Assert Vector2[] actualVerticies = actual.Verticies; for (int i = 0; i < actualVerticies.Length; i++) { Assert.AreEqual(expectedVerticies[i].X, actualVerticies[i].X, 0.05); Assert.AreEqual(expectedVerticies[i].Y, actualVerticies[i].Y, 0.05); } }
public void SetUp() { linearSiteCount = 6; linearSites = new List <Point>(); List <Point> sites = new List <Point>(); for (int i = 0; i < linearSiteCount; i++) { float x = 2 * i + 1 - linearSiteCount; linearSites.Add(new Point(x, 0)); sites.Add(new Point(x, 0)); } // Leading point, to make sure that our linear sites are not added as leaders before the algorithm can get into full swing. leadingPoint = new Point(0, 3); sites.Add(leadingPoint); voronoi = new VoronoiDiagram(sites); siteToBordersMap = new Dictionary <Point, List <Point> >(); List <VoronoiCellUnorganised> cells = voronoi.UnorganisedCells().ToList(); foreach (VoronoiCellUnorganised cell in cells) { siteToBordersMap[cell.Site()] = cell.Borders().ToList(); } }
public void SetUp() { site = new Point(4, -5); voronoi = new VoronoiDiagram(new List <Point> { site, site }); }
public void VoronoiFactory_MakeVoronoiSFTest_TwoPolygon_Passes() { //Arrange List <Vector2> Seeds = new List <Vector2>(); Seeds.Add(new Vector2(10.96f, 7.53f)); Seeds.Add(new Vector2(16.16f, 16.4f)); Seeds.Add(new Vector2(10.43f, 10.2f)); Seeds.Add(new Vector2(10.6f, 4.46f)); Seeds.Add(new Vector2(4.16f, 16.67f)); Vector2 Boundry = new Vector2(15f, 15f); Vector2[] expectedVerticies = { new Vector2(10.153f, 16.204f), new Vector2(16.786f, 10.074f), new Vector2(1.637f, 7.067f), new Vector2(0.695f, 7.039f), new Vector2(26.985f, 4.095f) }; //Act VoronoiDiagram actual = VorannoyedFactory.MakeVoronoiSF(Seeds, Boundry); //Assert Vector2[] actualVerticies = actual.Verticies; for (int i = 0; i < actualVerticies.Length; i++) { Assert.AreEqual(expectedVerticies[i].X, actualVerticies[i].X, 0.05); Assert.AreEqual(expectedVerticies[i].Y, actualVerticies[i].Y, 0.05); } }
public void VoronoiFactory_MakeVoronoiSFTest_TrapazoidResult_Passes() { //Arrange List <Vector2> Seeds = new List <Vector2>(); Seeds.Add(new Vector2(9.67f, 10.75f)); Seeds.Add(new Vector2(13.38f, 9.17f)); Seeds.Add(new Vector2(5.62f, 8.1f)); Seeds.Add(new Vector2(11f, 6.07f)); Seeds.Add(new Vector2(11f, 3.06f)); Vector2 Boundry = new Vector2(15f, 15f); Vector2[] expectedVerticies = { new Vector2(10.938f, 8.581f), new Vector2(8.627f, 7.924f), new Vector2(7.359f, 4.565f), new Vector2(16.169f, 4.565f) }; //Act VoronoiDiagram actual = VorannoyedFactory.MakeVoronoiSF(Seeds, Boundry); //Assert Vector2[] actualVerticies = actual.Verticies; for (int i = 0; i < actualVerticies.Length; i++) { Assert.AreEqual(expectedVerticies[i].X, actualVerticies[i].X, 0.05); Assert.AreEqual(expectedVerticies[i].Y, actualVerticies[i].Y, 0.05); } }
/// <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 <VoronoiDiagram.GraphEdge> SetVoronoiDiagram() { InitBioms(world); VoronoiDiagram voronoi = new VoronoiDiagram(world.WorldAttributes.SitesMinDistance); double[] xVal = new double[world.WorldAttributes.SitesNumber]; double[] yVal = new double[world.WorldAttributes.SitesNumber]; for (int i = 0; i < world.WorldAttributes.SitesNumber; ++i) { xVal[i] = UnityEngine.Random.Range(0, world.WorldAttributes.WorldSizeInBlocks); yVal[i] = UnityEngine.Random.Range(0, world.WorldAttributes.WorldSizeInBlocks); } List <VoronoiDiagram.GraphEdge> Edges = voronoi.GenerateDiagram(xVal, yVal, 0, world.WorldAttributes.WorldSizeInBlocks, 0, world.WorldAttributes.WorldSizeInBlocks); List <VoronoiDiagram.GraphEdge> ClearedEdges = new List <VoronoiDiagram.GraphEdge>(); for (int i = 0; i < Edges.Count; ++i) { if (Edges[i].x1 != Edges[i].x2 || Edges[i].y1 != Edges[i].y2) { ClearedEdges.Add(Edges[i]); } } foreach (var edge in ClearedEdges) { DrawLine(edge, -1); } return(ClearedEdges); }
public SimpleVoronoiIsland(MatrixRange matrixRange, int voronoiNum, double probabilityValue, int landValue, int seaValue) { voronoiDiagram = new VoronoiDiagram(matrixRange, voronoiNum); this.probability = probabilityValue; this.landValue = landValue; this.seaValue = seaValue; }
public static void DrawDiagram(VoronoiDiagram v, RenderConfig config, string filename) { // Initialize surface: Bitmap image = CreateBitmap(config); Graphics g = Graphics.FromImage(image); g.Clear(Color.White); // Style for site centers: SolidBrush pointBrush = new SolidBrush(Color.Black); // Style for lines: Pen linePen = new Pen(Brushes.SlateGray) { Width = 1.0F }; // Draw edges: foreach (Edge e in v.edges) { System.Drawing.Point a = TransposePoint(e.a, config); System.Drawing.Point b = TransposePoint(e.b, config); g.DrawLine(linePen, a, b); } // Draw site centers: foreach (Models.Point point in v.sites) { g.DrawPoint(point, pointBrush, config); } image.Save(filename); linePen.Dispose(); pointBrush.Dispose(); }
public RandomVoronoi(int drawValue, double probabilityValue, int trueColor, int falseColor) { voronoiDiagram = new VoronoiDiagram(drawValue); this.probabilityValue = probabilityValue; this.trueColor = trueColor; this.falseColor = falseColor; }
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); }
//Generate the voronoi diagram using an external library public VoronoiDiagram GetVoronoi(List<Point> points) { _siteCells = new Dictionary<Point, Cell>(); var nrPoints = points.Count; var dataPoints = new Vector[nrPoints]; for (int i = 0; i < nrPoints; i++) { var point = points[i]; if (_siteCells.ContainsKey(point)) continue; dataPoints[i] = new Vector(point.X,point.Y); var cell = new Cell {SitePoint = point}; _siteCells.Add(point, cell); } //Create Voronoi Data using library var data = Fortune.ComputeVoronoiGraph(dataPoints); //data = BenTools.Mathematics.Fortune.FilterVG(data, 15); //Create Diagram _voronoi = new VoronoiDiagram(); _voronoi.HalfEdges = GenerateLines(data); _voronoi.VoronoiCells = GenerateCells(data); _voronoi.Sites = points; return _voronoi; }
public void VoronoiFactory_MakeVoronoiSFTest_FourWayIntersect_Passes() { //Arrange List <Vector2> Seeds = new List <Vector2>(); Seeds.Add(new Vector2(2.5f, 1.5f)); Seeds.Add(new Vector2(1f, 2f)); Seeds.Add(new Vector2(2f, 2f)); Seeds.Add(new Vector2(1f, 1f)); Seeds.Add(new Vector2(2f, 1f)); Vector2 Boundry = new Vector2(15f, 15f); Vector2[] expectedVerticies = { new Vector2(2f, 1.5f), new Vector2(1.5f, 1.5f) }; //Act VoronoiDiagram actual = VorannoyedFactory.MakeVoronoiSF(Seeds, Boundry); //Assert Vector2[] actualVerticies = actual.Verticies; for (int i = 0; i < actualVerticies.Length; i++) { Assert.AreEqual(expectedVerticies[i].X, actualVerticies[i].X, 0.05); Assert.AreEqual(expectedVerticies[i].Y, actualVerticies[i].Y, 0.05); } }
public void VoronoiFactory_MakeVoronoiSFTest_SeedSpreadDefault_Passes() { //Arrange List <Vector2> Seeds = new List <Vector2>(); Seeds.Add(new Vector2(13.9f, 6.76f)); Seeds.Add(new Vector2(12.7f, 10.6f)); Seeds.Add(new Vector2(8.7f, 7.7f)); Seeds.Add(new Vector2(7.1f, 4.24f)); Seeds.Add(new Vector2(4.6f, 11.44f)); Vector2 Boundry = new Vector2(15f, 15f); Vector2[] expectedVerticies = { new Vector2(8.737f, 11.858f), new Vector2(11.458f, 8.104f), new Vector2(4.711f, 7.445f), new Vector2(10.828f, 4.616f) }; //Act VoronoiDiagram actual = VorannoyedFactory.MakeVoronoiSF(Seeds, Boundry); //Assert Vector2[] actualVerticies = actual.Verticies; for (int i = 0; i < actualVerticies.Length; i++) { Assert.AreEqual(expectedVerticies[i].X, actualVerticies[i].X, 0.05); Assert.AreEqual(expectedVerticies[i].Y, actualVerticies[i].Y, 0.05); } }
public void SetUp() { siteA = new Point(4, -5); siteB = new Point(3, 2); voronoi = new VoronoiDiagram(new List <Point> { siteA, siteB }); }
public float SqrLength() { if (begin == null || end == null) { throw new Exception("This is not edge"); } return(VoronoiDiagram.SqrLength(begin.Value, end.Value)); }
public VoronoiDiagramDrawer(VoronoiDiagram diagram) { LatLongGridDrawer.DrawGrid(); DrawSites(diagram.SiteEvents); _sweeplineDrawer = new SweeplineDrawer(diagram.Beachline.Sweepline); _beachlineDrawer = new BeachlineDrawer(diagram.Beachline); _circleEventDrawer = new CircleEventsDrawer(diagram.CircleEventQueue); _edgeDrawer = new EdgeDrawer(diagram.Edges); }
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()); }
protected override void Initialize() { base.Initialize(); _voronoi = new VoronoiDiagram(MeshFilter, transform); _pointColors.Clear(); ClearTris(); ClearLines(); ClearPoints(); UpdateTexture(); }
// Use this for initialization. void Awake() { this.Site = null; if (Cell.map == null) { GameObject go = GameObject.Find("Map"); Cell.map = go.GetComponent <VoronoiDiagram>(); } this.rend = gameObject.GetComponent <Renderer>(); }
private void GenerateVoronoiDiagram() { if (m_inputPoints == null || m_inputPoints.Length == 0) { return; } m_voronoiDiagram = new VoronoiDiagram(m_inputPoints, new VBorder(m_width, m_height)); m_sweepLine = -HalfHeight * HalfHeight; #if UNITY_EDITOR UnityEditor.SceneView.RepaintAll(); #endif }
public void SetUp() { SetPoints(); voronoi = new VoronoiDiagram(points); siteToBordersMap = new Dictionary <Point, List <Point> >(); List <VoronoiCellUnorganised> cells = voronoi.UnorganisedCells().ToList(); foreach (VoronoiCellUnorganised cell in cells) { siteToBordersMap[cell.Site()] = cell.Borders().ToList(); } }
private void btnSet_Click(object sender, EventArgs e) { int imgSize, numSeeds; if (int.TryParse(tbImgSize.Text, out imgSize) && int.TryParse(tbSeedCount.Text, out numSeeds)) { Diagram = new VoronoiDiagram(imgSize, numSeeds, 1); pbVoronoiDiagram.Image = Diagram.CreateImage(); } else { tbSeedCount.Text = ""; tbImgSize.Text = ""; } }
public VoronoiDiagram GetVoronoi(List <Point> points) { _voronoi = new VoronoiDiagram(); _voronoi.Sites = points; //Triangulate points based on Delaunay Triangulation _voronoi.Triangulation = DelaunayTriangulation(points); //connect centroid points of all adjacent triangles _voronoi.HalfEdges = CreateVoronoiLines(_voronoi.Triangulation); _voronoi.VoronoiCells = CreateVoronoiCells(_voronoi.HalfEdges); return(_voronoi); }
static void BorderConditionTests() { foreach (string filePath in InputFiles()) { string fileNameCore = Path.GetFileNameWithoutExtension(filePath); if (specificTestName != "" && fileNameCore != specificTestName) { continue;//todo delete } var inputRepr = InputRepr.FromFile(filePath); var boundingBox = new VoronoiDiagram.BoundingBox() { x = inputRepr.boundingBox.leftX, y = inputRepr.boundingBox.bottomY, sizeX = inputRepr.boundingBox.width, sizeY = inputRepr.boundingBox.height }; DiagramBuildLogger logger = null; if (drawDebug) { logger = new DiagramBuildLogger("", fileNameCore + "_debug", boundingBox); } var dots = inputRepr.dots.Select(d => new VoronoiDiagram.Dot() { x = d[0], y = d[1] }); var borders = VoronoiDiagram.CreateEdges(dots, logger); CanvasCreator canvasCreator; canvasCreator = GetSimpleCanvas(dots, borders); SetupCanvas(canvasCreator, boundingBox); File.WriteAllText($"..\\..\\..\\tests\\out\\{fileNameCore}.html", canvasCreator.GetHtmlContent()); borders = VoronoiDiagram.ApplyBoundingBox(borders, boundingBox); canvasCreator = GetPolygonalCanvas(dots, borders); SetupCanvas(canvasCreator, boundingBox); File.WriteAllText($"..\\..\\..\\tests\\out\\{fileNameCore}_p.html", canvasCreator.GetHtmlContent()); } }
public void SetUp() { siteA = new Point(4, -5); siteB = new Point(3, 2); siteC = new Point(-1, -1); voronoi = new VoronoiDiagram(new List <Point> { siteA, siteB, siteC }); siteToBordersMap = new Dictionary <Point, List <Point> >(); List <VoronoiCellUnorganised> cells = voronoi.UnorganisedCells().ToList(); foreach (VoronoiCellUnorganised cell in cells) { siteToBordersMap[cell.Site()] = cell.Borders().ToList(); } }
public void SetUp() { siteCount = 6; sites = new List <Point>(); for (int i = 0; i < siteCount; i++) { float x = 2 * i + 1 - siteCount; sites.Add(new Point(x, 0)); } voronoi = new VoronoiDiagram(sites); siteToBordersMap = new Dictionary <Point, List <Point> >(); List <VoronoiCellUnorganised> cells = voronoi.UnorganisedCells().ToList(); foreach (VoronoiCellUnorganised cell in cells) { siteToBordersMap[cell.Site()] = cell.Borders().ToList(); } }
/// <summary> /// Find all cells within the original generation bounds of a voronoi diagram /// </summary> /// <param name="voronoi"></param> /// <returns></returns> public static List <Cell> GetCellsInBounds(this VoronoiDiagram voronoi) { var cells = new List <Cell>(); foreach (var cell in voronoi.VoronoiCells) { var cellCenter = cell.Center(); //check if zone is in bounds (avoid generating infinitely large zones) if (cellCenter.X > voronoi.Bounds.Right || cellCenter.Y > voronoi.Bounds.Bottom || cellCenter.X < voronoi.Bounds.Left || cellCenter.Y < voronoi.Bounds.Top) { continue; } cells.Add(cell); } return(cells); }
//Generate the voronoi diagram using an external library public VoronoiDiagram GetVoronoi(List <Point> points) { _siteCells = new Dictionary <Point, Cell>(); var nrPoints = points.Count; var dataPoints = new Vector[nrPoints]; for (int i = 0; i < nrPoints; i++) { var point = points[i]; if (_siteCells.ContainsKey(point)) { continue; } dataPoints[i] = new Vector(point.X, point.Y); var cell = new Cell { SitePoint = point }; _siteCells.Add(point, cell); } //Create Voronoi Data using library var data = Fortune.ComputeVoronoiGraph(dataPoints); //data = BenTools.Mathematics.Fortune.FilterVG(data, 15); //Create Diagram _voronoi = new VoronoiDiagram(); _voronoi.HalfEdges = GenerateLines(data); _voronoi.VoronoiCells = GenerateCells(data); _voronoi.Sites = points; return(_voronoi); }
// Use this for initialization void Start() { //var options = new Options {Radius = 1, Resolution = 2}; //var vertices = new GeodesicGrid(options).Faces.SelectMany(face => face.Vertices).Distinct(); //var positions = vertices.Select(vertex => UnityVectorToDoubleArray(vertex.Position)).ToArray(); //var positions = new List<double[]> //{ // VectorAt(0, 0), // VectorAt(90, 45), // VectorAt(90, -45), // VectorAt(45, -45), // VectorAt(45, 45) //}; var positions = Enumerable.Range(0, 100).Select(i => CreateSphericalVector()).ToArray(); //var positions1 = Enumerable.Range(0, 2).Select(i => VectorAt(10, 360 * i / 10.0f)).ToList(); //var positions2 = Enumerable.Range(0, 2).Select(i => VectorAt(40, 360 * i / 10.0f)).ToList(); //var positions = positions1.Concat(positions2).ToList(); _diagram = new VoronoiDiagram(positions); _drawer = new VoronoiDiagramDrawer(_diagram); }
public VoronoiDiagram GetVoronoi(List<Point> points) { _voronoi = new VoronoiDiagram(); _voronoi.Sites = points; //Triangulate points based on Delaunay Triangulation _voronoi.Triangulation = DelaunayTriangulation(points); //connect centroid points of all adjacent triangles _voronoi.HalfEdges = CreateVoronoiLines(_voronoi.Triangulation); _voronoi.VoronoiCells = CreateVoronoiCells(_voronoi.HalfEdges); return _voronoi; }
private void Init(NormalOptions normalOpt, CloudOptions cloudOpt, VoronoiOptions voronoiOpt) { _normalOpt = normalOpt; _cloudOpt = cloudOpt; _voronoiOpt = voronoiOpt; _cfGen = new CloudFractal(_normalOpt.size, _normalOpt.seed, _cloudOpt); _vnGen = new VoronoiDiagram(_normalOpt.size, _normalOpt.seed, _voronoiOpt); _heightMap = new float[_normalOpt.size * _normalOpt.size]; }