static BorderPoint GetNextPoint(BorderPoint last, List <BorderPoint> points) { Vector2[] poses = new Vector2[8] { new Vector2(0, -1), new Vector2(1, -1), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(-1, 1), new Vector2(-1, 0), new Vector2(-1, -1), }; for (int i = 0; i < poses.Length; i++) { var pos = points.Where(t => t.position == last.position + poses[i]); if (pos.Any()) { return(pos.FirstOrDefault()); } } return(new BorderPoint() { position = new Vector2(-1, -1) }); }
public static BorderPoint GetPoint(int x, int y, Bitmap bitmap) { BorderPoint result = new BorderPoint(); result.position = new Vector2(x, y); result.fixedPosition = result.position; Color pixel = bitmap.GetPixel(x, y); bool bo = true; for (int i = 0; i < neighbours.Length; i++) { Color b = bitmap.GetPixel(x + (int)neighbours[i].X, y + (int)neighbours[i].Y); if (pixel != b) { result.fixedPosition += neighbours[i] * 0.5f; result.borderProvince = b.R.ToString("X2") + b.G.ToString("X2") + b.B.ToString("X2"); return(result); } } return(new BorderPoint() { position = new Vector2(-1, -1) }); }
static List <Border> GetBorders() { Dictionary <string, List <BorderPoint> > points = new Dictionary <string, List <BorderPoint> >(); for (int x = 1; x < bitmap.Width - 1; x++) { for (int y = 1; y < bitmap.Height - 1; y++) { Color pixel = bitmap.GetPixel(x, y); string spixel = pixel.R.ToString("X2") + pixel.G.ToString("X2") + pixel.B.ToString("X2"); BorderPoint point = BorderPoint.GetPoint(x, y, bitmap); if (point.position != new Vector2(-1, -1)) { if (!points.ContainsKey(spixel)) { points.Add(spixel, new List <BorderPoint>()); } points[spixel].Add(point); } } } List <Border> result = new List <Border>(); int i = 0; foreach (var item in points) { i++; Console.WriteLine($"Generating borders... ({i}/{points.Count()})"); while (item.Value.Count > 0) { BorderPoint start = item.Value[0]; BorderPoint last = start; item.Value.Remove(start); BorderPoint current = GetNextPoint(last, item.Value); item.Value.Remove(current); Border lastB = null; Border b = null; if (current.position == new Vector2(-1, -1)) { continue; } do { b = result.Where(t => t.firstProvince == item.Key && t.secondProvince == current.borderProvince).FirstOrDefault(); if (b == null) { var n = result.Where(t => t.firstProvince == current.borderProvince && t.secondProvince == item.Key); if (n.Any()) { b = n.FirstOrDefault(); lastB?.vertices?.Last().Add(current.fixedPosition); goto Skip; } else { Border border = new Border() { firstProvince = item.Key, secondProvince = current.borderProvince, vertices = new List <List <Vector2> >() }; border.vertices.Add(new List <Vector2>()); border.vertices.Last().Add(last.fixedPosition); result.Add(border); b = border; } } lastB = b; if (Vector2.Distance(b.vertices.Last().Last(), current.fixedPosition) >= 1.7f) { b.vertices.Add(new List <Vector2>()); b.vertices.Last().Add(last.fixedPosition); } b.vertices.Last().Add(current.fixedPosition); last = current; Skip: current = GetNextPoint(last, item.Value); item.Value.Remove(current); } while (current.position != new Vector2(-1, -1) && current.position != start.position); } } Console.WriteLine("Optimalizing borders..."); result = OptimalizeBorders(result); return(result); }