void performLloyd() { VoronoiStruct.Voronoi newMap = new VoronoiStruct.Voronoi(vmap.width, vmap.height); Rectangle regin = new Rectangle(0, 0, vmap.width, vmap.height); foreach (var poly in vmap.polygons) { List <VoronoiStruct.Point> points = new List <VoronoiStruct.Point>(); foreach (var edge in poly.edges) { VoronoiStruct.Point a = edge.line.a; VoronoiStruct.Point b = edge.line.b; if (!points.Contains(a)) { points.Add(a); } if (!points.Contains(b)) { points.Add(b); } } int cx = points.Sum((point) => { return(point.x); }) / points.Count; int cy = points.Sum((point) => { return(point.y); }) / points.Count; newMap.polygons.Add(new VoronoiStruct.Polygon(new VoronoiStruct.Point(cx, cy))); } vmap = newMap; sweepLine = null; drawVoronoi(vmap); ++mapIterations; label_iterations.Text = string.Format("{0}-th iterations", mapIterations); }
private void readMap(string path, out VoronoiStruct.Voronoi map) { StreamReader sr = new StreamReader(path); string json = sr.ReadToEnd(); map = JsonConvert.DeserializeObject <VoronoiStruct.Voronoi>(json); }
void give_moisture(ref VoronoiStruct.Voronoi map) //找出所有polygon的溼度 { float x = 2000, y = 2000; foreach (var item1 in map.polygons) { foreach (var item3 in allRiver) { x = (Math.Abs(item1.centerX - item3.x) <= x) ? Math.Abs(item1.centerX - item3.x) : x; y = (Math.Abs(item1.centerY - item3.y) <= y) ? Math.Abs(item1.centerY - item3.y) : y; } foreach (var item4 in map.polygons) { if (item4.isSea) { foreach (var item5 in item4.edges) { x = (Math.Abs(item1.centerX - item5.line.a.x) <= x) ? Math.Abs(item1.centerX - item5.line.a.x) : x; y = (Math.Abs(item1.centerY - item5.line.a.y) <= y) ? Math.Abs(item1.centerY - item5.line.a.y) : y; } } } //x = (float)Math.Pow(0.95, x); //y = (float)Math.Pow(0.95, y); item1.moisture = (float)Math.Sqrt(x * x + y * y); item1.moisture = (float)Math.Pow(0.9, item1.moisture); //System.Diagnostics.Debug.WriteLine("Moisture: " + item1.moisture); x = 2000; y = 2000; } }
void decide_biome(ref VoronoiStruct.Voronoi map) //決定出生態系 { foreach (var item in map.polygons) { if (item.elevation <= 0.3 && item.moisture <= 0.35) { item.bio = VoronoiStruct.Biome.Lava; } else if (item.elevation >= 0.3 && item.elevation <= 0.8 && item.moisture <= 0.35) { item.bio = VoronoiStruct.Biome.Desert; } else if (item.elevation >= 0.8 && item.moisture <= 0.35) { item.bio = VoronoiStruct.Biome.Volcano; } else if (item.elevation <= 0.3 && item.moisture >= 0.35 && item.moisture <= 0.55) { item.bio = VoronoiStruct.Biome.Desert; } else if (item.elevation >= 0.3 && item.elevation <= 0.8 && item.moisture >= 0.35 && item.moisture <= 0.55) { item.bio = VoronoiStruct.Biome.Grassland; } else if (item.elevation >= 0.8 && item.moisture >= 0.35 && item.moisture <= 0.55) { item.bio = VoronoiStruct.Biome.Grassland; } else if (item.elevation <= 0.5 && item.moisture >= 0.55 && item.moisture <= 0.8) { item.bio = VoronoiStruct.Biome.Grassland; } else if (item.elevation >= 0.5 && item.elevation <= 0.8 && item.moisture >= 0.55 && item.moisture <= 0.8) { item.bio = VoronoiStruct.Biome.Forest; } else if (item.elevation >= 0.8 && item.moisture >= 0.55 && item.moisture <= 0.8) { item.bio = VoronoiStruct.Biome.Snow; } else if (item.elevation <= 0.8 && item.moisture >= 0.8) { item.bio = VoronoiStruct.Biome.Forest; } else if (item.elevation >= 0.8 && item.moisture >= 0.8) { item.bio = VoronoiStruct.Biome.Snow; } } foreach (var item in map.polygons) { if (item.isSea) { item.bio = VoronoiStruct.Biome.Ocean; } } }
void find_sea(ref VoronoiStruct.Voronoi map) //找出所有的海 { foreach (var item1 in map.polygons) { foreach (var item2 in item1.edges) { if (item2.line.a.x == 0 || item2.line.a.x == 799) { item1.isSea = true; } else if (item2.line.a.y == 0 || item2.line.a.y == 799) { item1.isSea = true; } else if (item2.line.b.x == 0 || item2.line.b.x == 799) { item1.isSea = true; } else if (item2.line.b.y == 0 || item2.line.b.y == 799) { item1.isSea = true; } } } }
void drawVoronoi(VoronoiStruct.Voronoi map) { if (map == null) { return; } bmp = new Bitmap(map.width, map.height); g = Graphics.FromImage(bmp); g.Clear(Color.White); pictureBox.Image = bmp; foreach (var item in map.polygons) { drawPoint(blueBrush, item.focus); } foreach (var poly in map.polygons) { foreach (var edge in poly.edges) { if (!edge.isAbstract() && (edge.line.a.x != -1 || edge.line.a.y != -1) && (edge.line.b.x != -1 || edge.line.b.y != -1)) { drawLine(blackPen, edge.line); } } } pictureBox.Invalidate(); }
void readMap(string filePath, out VoronoiStruct.Voronoi vmap) { StreamReader sr = new StreamReader(filePath); string json = sr.ReadToEnd(); initMap(json, out vmap); sr.Close(); }
public SweepLine(ref VoronoiStruct.Voronoi vmap) : this() { this.vmap = vmap; for (int i = 0; i < this.vmap.polygons.Count; i++) { var bar = this.vmap.polygons[i]; bar.id = i; addSite(new Event(bar)); } }
void draw_river(VoronoiStruct.Voronoi map) //畫出河流 { foreach (var item in map.polygons) { foreach (var item2 in item.edges) { if (item2.line.isRiver) { drawLine(bluepen, item2.line); } } } }
void ratio_elevation_of_polygon(ref VoronoiStruct.Voronoi map) //將所有polygon的elevation轉為0~1之間 { float max = 0, temp = 0; foreach (var item in map.polygons) { max = (item.elevation >= max) ? item.elevation : max; } foreach (var item in map.polygons) { temp = (float)Math.Pow((item.elevation / max), 3); item.elevation = temp; //System.Diagnostics.Debug.WriteLine("Ratio elevation: " + item.elevation); } }
public Form1() { InitializeComponent(); vmap = new VoronoiStruct.Voronoi(); blueBrush = new SolidBrush(Color.Blue); circleSize = new Size(3, 3); bmp = new Bitmap(1, 1); g = Graphics.FromImage(bmp); numericUpDown_width_ValueChanged(null, null); numericUpDown_height_ValueChanged(null, null); numericUpDown_count_ValueChanged(null, null); numericUpDown_margin_ValueChanged(null, null); numericUpDown_border_ValueChanged(null, null); map = new bool[1, 1]; }
void drawing_biome(VoronoiStruct.Voronoi map) //畫出生態系 { foreach (var item in map.polygons) { switch (item.bio) { case VoronoiStruct.Biome.Grassland: drawPoint(grasslandBrush, item.focus); break; case VoronoiStruct.Biome.Forest: drawPoint(forestBrush, item.focus); break; case VoronoiStruct.Biome.Desert: drawPoint(desertBrush, item.focus); break; case VoronoiStruct.Biome.Snow: drawPoint(snowBrush, item.focus); break; case VoronoiStruct.Biome.Lava: drawPoint(lavaBrush, item.focus); break; case VoronoiStruct.Biome.Volcano: drawPoint(volcanoBrush, item.focus); break; case VoronoiStruct.Biome.Ocean: drawPoint(oceanBrush, item.focus); break; case VoronoiStruct.Biome.River: drawPoint(new SolidBrush(Color.SkyBlue), item.focus); break; case VoronoiStruct.Biome.Riverbank: drawPoint(new SolidBrush(Color.Pink), item.focus); break; case VoronoiStruct.Biome.Coastline: drawPoint(new SolidBrush(Color.SaddleBrown), item.focus); break; } } }
void initMap(string json, out VoronoiStruct.Voronoi vmap) { var map = JsonConvert.DeserializeObject <VoronoiStruct.Voronoi>(json); foreach (var poly in map.polygons) { foreach (var edge in poly.edges) { edge.deAbstract(); } } vmap = map; sweepLine = null; mapIterations = 0; label_iterations.Text = string.Format("{0}-th iterations", mapIterations); }
void elevation_for_polygon(ref VoronoiStruct.Voronoi map) //算出所有polygon的elevation { float ele = 0; foreach (var item in map.polygons) { foreach (var item1 in item.edges) { ele += item1.line.a.elevation; ele += item1.line.b.elevation; } ele /= (2 * item.edges.Count); item.elevation = ele; //System.Diagnostics.Debug.WriteLine("elevation of polygon: " + ele); ele = 0; } }
void makeVoronoi(bool[,] map, ref VoronoiStruct.Voronoi vmap) { vmap = new VoronoiStruct.Voronoi(); int n = map.GetLength(0); int m = map.GetLength(1); vmap.width = n; vmap.height = m; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (map[i, j]) { vmap.polygons.Add(new VoronoiStruct.Polygon(new VoronoiStruct.Point(i, j))); } } } }
void calculate_polygoncenter(ref VoronoiStruct.Voronoi map) //算所有polygon的中心 { float x = 0, y = 0; foreach (var item1 in map.polygons) { foreach (var item in item1.edges) { x += (item.line.a.x + item.line.b.x); y += (item.line.a.y + item.line.b.y); } x /= (2 * item1.edges.Count); y /= (2 * item1.edges.Count); item1.centerX = x; item1.centerY = y; //System.Diagnostics.Debug.WriteLine("center x:" + x + " y: " + y); x = 0; y = 0; } }
private void 貼上PToolStripMenuItem_Click(object sender, EventArgs e) { string json = Clipboard.GetText(); try { vmap = JsonConvert.DeserializeObject <VoronoiStruct.Voronoi>(json); } catch { System.Diagnostics.Debug.WriteLine("Error deserializing json"); return; } map = new bool[vmap.width, vmap.height]; foreach (var item in vmap.polygons) { map[item.focus.x, item.focus.y] = true; } drawMap(map); }
void calelevation(ref VoronoiStruct.Voronoi map) //算所有點的elevation { float x1 = 0, y1 = 0; float x2 = 0, y2 = 0; foreach (var item1 in map.polygons) { /*x = Math.Abs(item.centerX - map.polygons[Toppolygon].centerX); * y = Math.Abs(item.centerY - map.polygons[Toppolygon].centerY);*/ foreach (var item2 in item1.edges) { x1 = map.width - Math.Abs(item2.line.a.x - map.polygons[Toppolygon].centerX); y1 = map.height - Math.Abs(item2.line.a.y - map.polygons[Toppolygon].centerY); x2 = map.width - Math.Abs(item2.line.b.x - map.polygons[Toppolygon].centerX); y2 = map.height - Math.Abs(item2.line.b.y - map.polygons[Toppolygon].centerY); item2.line.a.elevation = (float)(Math.Sqrt(x1 * x1 + y1 * y1)); item2.line.b.elevation = (float)(Math.Sqrt(x2 * x2 + y2 * y2)); //System.Diagnostics.Debug.WriteLine("elevation: " + item2.line.a.elevation + "\t" + item2.line.b.elevation); x1 = 0; y1 = 0; x2 = 0; y2 = 0; } } }
private void 開啟OToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName); string json = sr.ReadToEnd(); try { vmap = JsonConvert.DeserializeObject <VoronoiStruct.Voronoi>(json); } catch { System.Diagnostics.Debug.WriteLine("Error deserializing json"); return; } map = new bool[vmap.width, vmap.height]; foreach (var item in vmap.polygons) { map[item.focus.x, item.focus.y] = true; } drawMap(map); } }
void drawVoronoi(VoronoiStruct.Voronoi map) { if (map == null) { return; } bmp = new Bitmap(map.width, map.height); g = Graphics.FromImage(bmp); g.Clear(Color.White); pictureBox1.Image = bmp; foreach (var item in map.polygons) { drawPoint(redBrush, item.focus); } foreach (var item in map.polygons) { foreach (var item2 in item.edges) { drawLine(blackpen, item2.line); } } pictureBox1.Invalidate(); }
void decide_coastline(ref VoronoiStruct.Voronoi map) //決定海岸線 { foreach (var item1 in map.polygons) { if (item1.bio == VoronoiStruct.Biome.Ocean) { foreach (var item2 in item1.edges) { if (item2.parentID == null) { continue; } if (map.polygons[item2.parentID[0]].bio != VoronoiStruct.Biome.Ocean) { map.polygons[item2.parentID[0]].bio = VoronoiStruct.Biome.Coastline; } if (map.polygons[item2.parentID[1]].bio != VoronoiStruct.Biome.Ocean) { map.polygons[item2.parentID[1]].bio = VoronoiStruct.Biome.Coastline; } } } } }
void cast_to_world(VoronoiStruct.Voronoi map) //將地圖投影到unit上 { float x1 = 0, y1 = 0, x2 = 0, y2 = 0; float result; bool inside = true; bool river_so_skip = true; for (int i = 0; i < world.unit.GetLength(0); ++i) { for (int j = 0; j < world.unit.GetLength(1); ++j) { foreach (var item1 in map.polygons) { foreach (var item2 in item1.edges) { x1 = item2.line.a.x - i; y1 = item2.line.a.y - j; x2 = item2.line.b.x - i; y2 = item2.line.b.y - j; result = x1 * y2 - y1 * x2; if (result < 0f) { inside = false; break; } } if (inside) { world.unit[i, j] = item1.bio; break; } inside = true; } } } }